deezer.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import requests
  2. from urllib.parse import urlencode
  3. import demixed.utils
  4. import demixed.track
  5. import demixed.artist
  6. import demixed.album
  7. class Deezer:
  8. def __init__(self):
  9. self.session = requests.Session()
  10. self.http_headers = {
  11. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
  12. "Content-Language": "en-US",
  13. "Cache-Control": "max-age=0",
  14. "Accept": "*/*",
  15. "Accept-Charset": "utf-8,ISO-8859-1;q=0.7,*;q=0.3",
  16. "Accept-Language": "en-US,en;q=0.9,en-US;q=0.8,en;q=0.7",
  17. "Connection": 'keep-alive'
  18. }
  19. self.user = None
  20. def call_private_api(self, method, args = {}):
  21. query_string = {
  22. "api_version": "1.0",
  23. "api_token": "null",
  24. "input": "3",
  25. "method": method
  26. }
  27. if method != "deezer.getUserData":
  28. query_string["api_token"] = self.get_api_token()
  29. return self.session.post(
  30. demixed.utils.PRIVATE_API_URL % urlencode(query_string),
  31. data = args,
  32. headers = self.http_headers
  33. ).json()
  34. def get_api_token(self):
  35. json = self.call_private_api("deezer.getUserData")
  36. if "checkFormLogin" in json["results"]:
  37. return json["results"]["checkFormLogin"]
  38. elif "checkForm" in json["results"]:
  39. return json["results"]["checkForm"]
  40. else:
  41. raise "couldn't get api_token"
  42. def _post_login(self):
  43. """The function to call after login cookies have been set.
  44. It will make sure all cookies are set, and returns the user profile.
  45. It's not supposed to be called on normal usage."""
  46. result = self.session.get("https://www.deezer.com/", headers=self.http_headers)
  47. if result.status_code != 200:
  48. raise "couldn't load deezer.com"
  49. result = self.call_private_api('deezer.getUserData')
  50. user_data = result["results"]["USER"]
  51. self.user = {
  52. "id": user_data["USER_ID"],
  53. "name": user_data["BLOG_NAME"],
  54. }
  55. if "USER_PICTURE" in user_data:
  56. self.user["picture_url"] = demixed.utils.USER_PICTURES_URL % user_data["USER_PICTURE"]
  57. return self.user
  58. def login_with_cookie(self, cookie_string):
  59. cookie = {"arl": cookie_string}
  60. self.session.cookies.update(cookie)
  61. return self._post_login()
  62. def login(self, mail, password):
  63. login_token = self.get_api_token()
  64. json = {
  65. "type": "login",
  66. "mail": mail,
  67. "password": password,
  68. "checkFormLogin": login_token,
  69. "reCaptchaToken": "",
  70. "reCaptchaDisabled": 1
  71. }
  72. headers = {
  73. **self.http_headers,
  74. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  75. }
  76. result = self.session.post(
  77. 'https://www.deezer.com//ajax/action.php?',
  78. data = json,
  79. headers = headers,
  80. )
  81. if result.text != "success":
  82. raise 'wrong email or password'
  83. return self._post_login()
  84. def get_track_data(self, track_id):
  85. if track_id < 0:
  86. return self.call_private_api(
  87. 'song.getData',
  88. args = '{"SNG_ID": %i}' % track_id
  89. )
  90. else:
  91. return self.call_private_api(
  92. 'deezer.pageTrack',
  93. args = '{"SNG_ID": %i}' % track_id
  94. )
  95. def get_track(self, track_id):
  96. return demixed.track.DeezerTrack.grab_or_create(
  97. track_id,
  98. raw_data = self.get_track_data(track_id)["results"]["DATA"]
  99. )
  100. def get_artist_data(self, artist_id):
  101. return self.call_private_api(
  102. 'deezer.pageArtist',
  103. args = '{"ART_ID": %i, "LANG": "EN"}' % artist_id
  104. )
  105. def get_artist(self, artist_id):
  106. return demixed.artist.DeezerArtist.grab_or_create(
  107. artist_id,
  108. raw_data = self.get_artist_data(artist_id)["results"]
  109. )
  110. def get_album_data(self, album_id):
  111. return self.call_private_api(
  112. 'deezer.pageAlbum',
  113. args = '{"ALB_ID": %i, "LANG": "EN"}' % album_id
  114. )
  115. def get_album(self, album_id):
  116. return demixed.album.DeezerAlbum.grab_or_create(
  117. album_id,
  118. raw_data = self.get_album_data(album_id)["results"]
  119. )