· 5 years ago · Nov 07, 2020, 07:24 PM
1# -*-coding:UTF-8 -*
2# Auteur : AlzoxX76
3# Date : 12/11/2018
4# Version : 0.5
5
6import requests
7import json
8
9
10URLcookies = "http://www.deezer.com"
11URLlogin = "http://www.deezer.com//ajax/action.php"
12
13
14
15def getMethodUrlWithToken(method, token=""):
16 return "http://www.deezer.com/ajax/gw-light.php?method=" + method + "&input=3&api_version=1.0&api_token=" + token
17
18def loginTest(email, mdp, proxy=dict()):
19 r = requests.post(URLlogin, data=dict(type="login", mail=email, password=mdp), proxies=proxy, timeout=5)
20 if r.text != "error" and r.text != "success":
21 raise Exception("Unknow return value for login")
22 if r.text == "success":
23 return True
24 return False
25
26class DeezerClient:
27
28 def __init__(self):
29 return
30
31
32 def login(self, email, mdp, proxy=dict()):
33 self.reset()
34 r = self.client.post(getMethodUrlWithToken("deezer.getUserData"), proxies=proxy, timeout=5) # Get user data (API token)
35 try:
36 self.checkFormLogin = json.loads(r.text)['results']["checkFormLogin"]
37 except:
38 pass
39 r = self.client.post(URLlogin, data=dict(type="login", mail=email, password=mdp, checkFormLogin=self.checkFormLogin), cookies=self.client.cookies, proxies=proxy, timeout=2)
40
41 if r.text != "error" and r.text != "success":
42 raise Exception("Unknow return value for login")
43 if r.text == "success":
44 self.getDeezerApiToken()
45 self.password = mdp
46 return True
47 return False
48
49 def getDeezerApiToken(self):
50 r = self.client.post(getMethodUrlWithToken("deezer.getUserData"), cookies=self.client.cookies) # Get user data (API token)
51
52 userInfo = json.loads(r.text)['results']
53
54
55 self.deezerAPITOKEN = userInfo['checkForm'] # checkForm key is the API key
56
57 def reset(self):
58
59 self.client = requests.session() # Create session
60
61 self.password = "" # Reset password
62
63 self.client.get(URLcookies) # Get cookies for session
64
65 r = self.client.post(getMethodUrlWithToken("deezer.getUserData"), cookies=self.client.cookies) # Get user data (API token)
66
67 userInfo = json.loads(r.text)['results']
68
69
70 self.deezerAPITOKEN = userInfo['checkForm'] # checkForm key is the API key
71
72 def getMethodDeezerAPI(self, method, dataRequest=str(), proxy=dict()):
73 return self.client.post(getMethodUrlWithToken(method, self.deezerAPITOKEN), data=dataRequest, cookies=self.client.cookies, proxies=proxy, timeout=2) # Deezer API request
74
75 def changePassword(self, newpassword):
76 response = self.getMethodDeezerAPI("deezer.updatePassword", json.dumps({"old_password":self.password, "password":newpassword}))
77 if json.loads(response.text)["results"]:
78 self.password = newpassword
79 return True
80 return False
81
82
83
84if __name__ == "__main__":
85 test = DeezerClient()
86 email = input("EMAIL : ")
87 mdp = input("MDP : ")
88 if test.login(email, mdp):
89 print("success")
90 print(test.changePassword(input("new password : ")))
91 else:
92 print("error")
93 input()