__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import eventlet
  2. requests = eventlet.import_patched('requests')
  3. from deezer.gw import GW
  4. from deezer.api import API
  5. import json
  6. __version__ = "0.0.9"
  7. class TrackFormats():
  8. """Number associtation for formats"""
  9. FLAC = 9
  10. MP3_320 = 3
  11. MP3_128 = 1
  12. MP4_RA3 = 15
  13. MP4_RA2 = 14
  14. MP4_RA1 = 13
  15. DEFAULT = 8
  16. LOCAL = 0
  17. class Deezer:
  18. def __init__(self):
  19. self.http_headers = {
  20. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \
  21. "Chrome/79.0.3945.130 Safari/537.36",
  22. "Accept-Language": None
  23. }
  24. self.session = requests.Session()
  25. self.session.mount('http://', requests.adapters.HTTPAdapter(pool_maxsize=100))
  26. self.session.mount('https://', requests.adapters.HTTPAdapter(pool_maxsize=100))
  27. self.logged_in = False
  28. self.current_user = {}
  29. self.childs = []
  30. self.selected_account = 0
  31. self.api = API(self.session, self.http_headers)
  32. self.gw = GW(self.session, self.http_headers)
  33. def set_accept_language(self, lang):
  34. self.http_headers['Accept-Language'] = lang
  35. def get_accept_language(self):
  36. return self.http_headers['Accept-Language']
  37. def login(self, email, password, re_captcha_token, child=0):
  38. # Check if user already logged in
  39. user_data = self.gw.get_user_data()
  40. if user_data['USER']['USER_ID'] == 0:
  41. self.logged_in = False
  42. return False
  43. # Get the checkFormLogin
  44. check_form_login = user_data['checkFormLogin']
  45. login = self.session.post(
  46. "https://www.deezer.com/ajax/action.php",
  47. data={
  48. 'type': 'login',
  49. 'mail': email,
  50. 'password': password,
  51. 'checkFormLogin': check_form_login,
  52. 'reCaptchaToken': re_captcha_token
  53. },
  54. headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', **self.http_headers}
  55. )
  56. # Check if user logged in
  57. if 'success' not in login.text:
  58. self.logged_in = False
  59. return False
  60. user_data = self.gw.get_user_data()
  61. self._post_login(user_data)
  62. self.change_account(child)
  63. self.logged_in = True
  64. return True
  65. def login_via_arl(self, arl, child=0):
  66. arl = arl.strip()
  67. cookie_obj = requests.cookies.create_cookie(
  68. domain='.deezer.com',
  69. name='arl',
  70. value=arl,
  71. path="/",
  72. rest={'HttpOnly': True}
  73. )
  74. self.session.cookies.set_cookie(cookie_obj)
  75. user_data = self.gw.get_user_data()
  76. # Check if user logged in
  77. if user_data["USER"]["USER_ID"] == 0:
  78. self.logged_in = False
  79. return False
  80. self._post_login(user_data)
  81. self.change_account(child)
  82. self.logged_in = True
  83. return True
  84. def _post_login(self, user_data):
  85. self.childs = []
  86. family = user_data["USER"]["MULTI_ACCOUNT"]["ENABLED"]
  87. if family:
  88. childs = self.gw.get_child_accounts()
  89. for child in childs:
  90. self.childs.append({
  91. 'id': child["USER_ID"],
  92. 'name': child["BLOG_NAME"],
  93. 'picture': child.get("USER_PICTURE", "")
  94. })
  95. else:
  96. self.childs.append({
  97. 'id': user_data["USER"]["USER_ID"],
  98. 'name': user_data["USER"]["BLOG_NAME"],
  99. 'picture': user_data["USER"].get("USER_PICTURE", "")
  100. })
  101. def change_account(self, child_n):
  102. if len(self.childs)-1 >= child_n:
  103. self.current_user =self.childs[child_n]
  104. self.selected_account = child_n
  105. else:
  106. self.current_user = self.childs[0]
  107. self.selected_account = 0
  108. return (self.current_user, self.selected_account)