theaudiodb.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import urllib
  2. import json
  3. import logging
  4. import requests
  5. from django.conf import settings
  6. THEAUDIODB_API_KEY = getattr(settings, "THEAUDIODB_API_KEY")
  7. ARTIST_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/search.php?s="
  8. ARTIST_FETCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/artist.php?i="
  9. ALBUM_SEARCH_URL = f"https://www.theaudiodb.com/api/v1/json/{THEAUDIODB_API_KEY}/searchalbum.php?s="
  10. logger = logging.getLogger(__name__)
  11. def lookup_artist_from_tadb(name_or_id: str) -> dict:
  12. artist_info = {}
  13. response = None
  14. # Look for an ID as an integer
  15. try:
  16. tadb_id = int(name_or_id)
  17. except ValueError:
  18. tadb_id = None
  19. if tadb_id:
  20. response = requests.get(ARTIST_FETCH_URL + str(tadb_id))
  21. if response.status_code != 200:
  22. logger.warn(f"Bad response from TADB: {response.status_code}")
  23. return artist_info
  24. if not response.content:
  25. logger.warn(f"Bad content from TADB: {response.content}")
  26. return artist_info
  27. if not response:
  28. name = urllib.parse.quote(name_or_id)
  29. response = requests.get(ARTIST_SEARCH_URL + name)
  30. if response.status_code != 200:
  31. logger.warn(f"Bad response from TADB: {response.status_code}")
  32. return artist_info
  33. if not response.content:
  34. logger.warn(f"Bad content from TADB: {response.content}")
  35. return artist_info
  36. if '{"artists": null}' in str(response.content):
  37. logger.warn(f"Bad content from TADB: {response.content}")
  38. return artist_info
  39. results = json.loads(response.content)
  40. if results["artists"]:
  41. artist = results["artists"][0]
  42. artist_info["biography"] = artist.get("strBiographyEN")
  43. artist_info["genre"] = artist.get("strGenre")
  44. artist_info["mood"] = artist.get("strMood")
  45. artist_info["thumb_url"] = artist.get("strArtistThumb")
  46. return artist_info
  47. def lookup_album_from_tadb(name: str, artist: str) -> dict:
  48. album_info = {}
  49. artist = urllib.parse.quote(artist)
  50. name = urllib.parse.quote(name)
  51. response = requests.get("".join([ALBUM_SEARCH_URL, artist, "&a=", name]))
  52. if response.status_code != 200:
  53. logger.warn(f"Bad response from TADB: {response.status_code}")
  54. return {}
  55. if not response.content:
  56. logger.warn(f"Bad content from TADB: {response.content}")
  57. return {}
  58. results = json.loads(response.content)
  59. if results["album"]:
  60. album = results["album"][0]
  61. album_info["theaudiodb_id"] = album.get("idAlbum")
  62. album_info["theaudiodb_description"] = album.get("strDescriptionEN")
  63. album_info["theaudiodb_genre"] = album.get("strGenre")
  64. album_info["theaudiodb_style"] = album.get("strStyle")
  65. album_info["theaudiodb_mood"] = album.get("strMood")
  66. album_info["theaudiodb_speed"] = album.get("strSpeed")
  67. album_info["theaudiodb_theme"] = album.get("strTheme")
  68. album_info["allmusic_id"] = album.get("strAllMusicID")
  69. album_info["wikipedia_slug"] = album.get("strWikipediaID")
  70. album_info["discogs_id"] = album.get("strDiscogsID")
  71. album_info["wikidata_id"] = album.get("strWikidataID")
  72. album_info["rateyourmusic_id"] = album.get("strRateYourMusicID")
  73. if album.get("intYearReleased"):
  74. album_info["theaudiodb_year_released"] = float(
  75. album.get("intYearReleased")
  76. )
  77. if album.get("intScore"):
  78. album_info["theaudiodb_score"] = float(album.get("intScore"))
  79. if album.get("intScoreVotes"):
  80. album_info["theaudiodb_score_votes"] = int(
  81. album.get("intScoreVotes")
  82. )
  83. return album_info