Skip to content

Commit 219f379

Browse files
committed
removed tokens
1 parent 5874006 commit 219f379

File tree

4 files changed

+108
-26
lines changed

4 files changed

+108
-26
lines changed

.spotify_data.secret

122 Bytes
Binary file not shown.

.telegram_data.secret

71 Bytes
Binary file not shown.

spotify.py

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import webbrowser
1010
#flask server
1111
from flask import Flask, request
12+
import pickle
1213

1314

1415
class Spotify(object):
@@ -43,44 +44,90 @@ def code():
4344

4445
class User(object):
4546

46-
def __init__(
47-
self,
48-
client_id = '83e4430b4700434baa21228a9c7d11c5',
49-
client_secret = '9bb9e131ff28424cb6a420afdf41d44a'
50-
):
47+
def __init__(self):
5148

52-
self.__client_id = client_id
53-
self.__client_secret = client_secret
5449
self.__grant_type = 'authorization_code'
5550
self.__scope = 'user-library-read'
51+
self.__getData()
5652
self.__redirect = 'http://localhost:5000/'
5753
self.__urlCode = f'https://accounts.spotify.com/authorize?client_id={self.__client_id}&response_type=code&redirect_uri={self.__redirect}&scope={self.__scope}'
5854
self.__url = 'https://accounts.spotify.com/api/token'
5955

56+
self.__getRefreshToken()
6057

58+
self.__client = spotipy.Spotify(auth=self.__access_token)
59+
60+
61+
def __getAccessToken(self):
62+
#start server
6163
#handling the code
6264
webbrowser.open_new(self.__urlCode)
6365
Spotify.Server.run()
6466
self.__code = Spotify.Server.code
6567

66-
6768
self.__body_params = {
6869
'grant_type': self.__grant_type,
6970
'code': self.__code,
7071
'redirect_uri': self.__redirect,
7172
}
7273

7374
#getting access_token by POST request to Spotify API
74-
self.__access_token = requests.post(
75+
response = requests.post(
7576
self.__url,
7677
data=self.__body_params,
7778
auth=(
7879
self.__client_id,
7980
self.__client_secret
8081
)
81-
).json()['access_token']
82+
).json()
8283

83-
self.__client = spotipy.Spotify(auth=self.__access_token)
84+
self.__access_token = response['access_token']
85+
self.__refresh_token = response['refresh_token']
86+
87+
data = {'refresh_token' : self.__refresh_token}
88+
89+
with open('.spotify_refresh_token.secret', 'wb') as f:
90+
pickle.dump(data, f)
91+
92+
93+
def __getAccessTokenByRefreshToken(self, refresh_token):
94+
response = requests.post('https://accounts.spotify.com/api/token?',
95+
{
96+
'grant_type': 'refresh_token',
97+
'refresh_token': str(refresh_token),
98+
'client_id': self.__client_id,
99+
'client_secret': self.__client_secret
100+
}
101+
).json()
102+
self.__access_token = response['access_token']
103+
104+
105+
def __getRefreshToken(self):
106+
try:
107+
108+
with open('.spotify_refresh_token.secret', 'rb') as f:
109+
data = pickle.load(f)
110+
self.__getAccessTokenByRefreshToken(data['refresh_token'])
111+
112+
except:
113+
self.__getAccessToken()
114+
115+
116+
def __getData(self):
117+
try:
118+
119+
with open('.spotify_data.secret', 'rb') as f:
120+
data = pickle.load(f)
121+
122+
self.__client_id = data['client_id']
123+
self.__client_secret = data['client_secret']
124+
125+
except:
126+
print('''
127+
A new version is available on GitHub.\n
128+
Download: https://github.com/artyshko/smd
129+
''')
130+
sys.exit()
84131

85132

86133
def getPlaylistTracks(self, playlist_uri):
@@ -110,47 +157,64 @@ def getPlaylistTracks(self, playlist_uri):
110157
'name' : data['name'],
111158
'artist' : [ artist['name'] for artist in data['artists']],
112159
'album' : data['album']['name'],
113-
'image' : data['album']['images'][0]['url']
160+
'image' : data['album']['images'][0]['url'],
161+
'duration_ms':data['duration_ms']
114162
})
115163

116164
return tracks
117165

118166

119-
def __init__(
120-
self,
121-
client_id = '83e4430b4700434baa21228a9c7d11c5',
122-
client_secret = '9bb9e131ff28424cb6a420afdf41d44a'
123-
):
167+
def __init__(self):
124168

125169
'''
126170
Init function
127171
Creating spotify object with access_token
128172
129-
:param client_id: spotify client_id parametr
130-
:param client_secret: spotify client_secret parametr
131173
:return: None
132174
'''
133175

134176
self.__url = 'https://accounts.spotify.com/api/token'
135-
self.__client_id = client_id
136-
self.__client_secret = client_secret
137177
self.__grant_type = 'client_credentials'
138178
self.__body_params = {
139179
'grant_type': self.__grant_type
140180
}
141181

182+
self.__getData()
183+
self.__getAccessToken()
184+
185+
#initialization of spotify client
186+
self.client = spotipy.Spotify(self.__access_token)
187+
188+
189+
def __getData(self):
190+
try:
191+
192+
with open('.spotify_data.secret', 'rb') as f:
193+
data = pickle.load(f)
194+
195+
self.__client_id = data['client_id']
196+
self.__client_secret = data['client_secret']
197+
198+
except:
199+
print('''
200+
A new version is available on GitHub.\n
201+
Download: https://github.com/artyshko/smd
202+
''')
203+
sys.exit()
204+
205+
206+
def __getAccessToken(self):
142207
#getting access_token by POST request to Spotify API
143-
self.__access_token = requests.post(
208+
response = requests.post(
144209
self.__url,
145210
data=self.__body_params,
146211
auth=(
147212
self.__client_id,
148213
self.__client_secret
149214
)
150-
).json()['access_token']
215+
).json()
151216

152-
#initialization of spotify client
153-
self.client = spotipy.Spotify(self.__access_token)
217+
self.__access_token = response['access_token']
154218

155219

156220
def getSongInfo(self, uri):
@@ -185,11 +249,13 @@ def search(self, query):
185249
except:
186250
return False
187251

252+
188253
def getDuration(self, uri):
189254

190255
data = self.client.track(uri)
191256
return data['duration_ms']
192257

258+
193259
def getAlbum(self, uri):
194260
try:
195261

telegram.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import apple
99
import random
1010
import urllib.request
11+
import pickle
1112

1213
import logging
1314

@@ -22,9 +23,24 @@
2223
class BotHandler(object):
2324

2425
def __init__(self):
25-
self.token = '752979930:AAFhdyGx0CSOJ-m17wLGN0NhrxvpwCqCPoQ'
26+
self.__getData()
2627
self.api_url = "https://api.telegram.org/bot{}/".format(self.token)
2728

29+
def __getData(self):
30+
try:
31+
32+
with open('.telegram_data.secret', 'rb') as f:
33+
data = pickle.load(f)
34+
35+
self.token = data['token']
36+
37+
except:
38+
print('''
39+
A new version is available on GitHub.\n
40+
Download: https://github.com/artyshko/smd
41+
''')
42+
sys.exit()
43+
2844
def getUpdates(self, offset=None, timeout=30):
2945

3046
method = 'getUpdates'

0 commit comments

Comments
 (0)