get_track.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from ast import literal_eval
  2. from bs4 import BeautifulSoup
  3. from .type import Track
  4. from .get_html import get_html
  5. from .utils import parse_json, remove_brackets
  6. def parse_scripts(html):
  7. for script in html.select("script"):
  8. str_script = str(script)
  9. prefix = "window.__PRELOADED_STATE__ = JSON.parse("
  10. if prefix in str_script:
  11. for line in str_script.splitlines():
  12. if prefix not in line:
  13. continue
  14. line = line.strip()
  15. line = line.removeprefix(prefix)
  16. line = line.removesuffix(");")
  17. json_string = literal_eval(line)
  18. parsed_track = parse_json(json_string)
  19. break
  20. song_page = parsed_track.songPage
  21. datas = {}
  22. for data in song_page.trackingData:
  23. value = data.value
  24. key = data.key
  25. datas[key] = value
  26. lyrics_html = BeautifulSoup(song_page.lyricsData.body.html, "html.parser")
  27. lyrics = lyrics_html.text.strip()
  28. return datas, lyrics
  29. async def get_track(url) -> Track:
  30. html = await get_html(url)
  31. datas, lyrics = parse_scripts(html)
  32. cover_meta = html.select_one('meta[property="og:image"]')
  33. cover = None
  34. if cover_meta:
  35. cover = cover_meta["content"]
  36. return Track(
  37. artist=remove_brackets(datas["Primary Artist"]),
  38. title=remove_brackets(datas["Title"]),
  39. id=f"/songs/{datas['Song ID']}",
  40. url=url,
  41. cover=cover,
  42. lyrics=lyrics,
  43. release=datas["Release Date"],
  44. album=None,
  45. number=0
  46. )