Skip to content

Commit

Permalink
removed tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
artyshko committed Mar 8, 2019
1 parent 5874006 commit 219f379
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 26 deletions.
Binary file added .spotify_data.secret
Binary file not shown.
Binary file added .telegram_data.secret
Binary file not shown.
116 changes: 91 additions & 25 deletions spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import webbrowser
#flask server
from flask import Flask, request
import pickle


class Spotify(object):
Expand Down Expand Up @@ -43,44 +44,90 @@ def code():

class User(object):

def __init__(
self,
client_id = '83e4430b4700434baa21228a9c7d11c5',
client_secret = '9bb9e131ff28424cb6a420afdf41d44a'
):
def __init__(self):

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

self.__getRefreshToken()

self.__client = spotipy.Spotify(auth=self.__access_token)


def __getAccessToken(self):
#start server
#handling the code
webbrowser.open_new(self.__urlCode)
Spotify.Server.run()
self.__code = Spotify.Server.code


self.__body_params = {
'grant_type': self.__grant_type,
'code': self.__code,
'redirect_uri': self.__redirect,
}

#getting access_token by POST request to Spotify API
self.__access_token = requests.post(
response = requests.post(
self.__url,
data=self.__body_params,
auth=(
self.__client_id,
self.__client_secret
)
).json()['access_token']
).json()

self.__client = spotipy.Spotify(auth=self.__access_token)
self.__access_token = response['access_token']
self.__refresh_token = response['refresh_token']

data = {'refresh_token' : self.__refresh_token}

with open('.spotify_refresh_token.secret', 'wb') as f:
pickle.dump(data, f)


def __getAccessTokenByRefreshToken(self, refresh_token):
response = requests.post('https://accounts.spotify.com/api/token?',
{
'grant_type': 'refresh_token',
'refresh_token': str(refresh_token),
'client_id': self.__client_id,
'client_secret': self.__client_secret
}
).json()
self.__access_token = response['access_token']


def __getRefreshToken(self):
try:

with open('.spotify_refresh_token.secret', 'rb') as f:
data = pickle.load(f)
self.__getAccessTokenByRefreshToken(data['refresh_token'])

except:
self.__getAccessToken()


def __getData(self):
try:

with open('.spotify_data.secret', 'rb') as f:
data = pickle.load(f)

self.__client_id = data['client_id']
self.__client_secret = data['client_secret']

except:
print('''
A new version is available on GitHub.\n
Download: https://github.com/artyshko/smd
''')
sys.exit()


def getPlaylistTracks(self, playlist_uri):
Expand Down Expand Up @@ -110,47 +157,64 @@ def getPlaylistTracks(self, playlist_uri):
'name' : data['name'],
'artist' : [ artist['name'] for artist in data['artists']],
'album' : data['album']['name'],
'image' : data['album']['images'][0]['url']
'image' : data['album']['images'][0]['url'],
'duration_ms':data['duration_ms']
})

return tracks


def __init__(
self,
client_id = '83e4430b4700434baa21228a9c7d11c5',
client_secret = '9bb9e131ff28424cb6a420afdf41d44a'
):
def __init__(self):

'''
Init function
Creating spotify object with access_token
:param client_id: spotify client_id parametr
:param client_secret: spotify client_secret parametr
:return: None
'''

self.__url = 'https://accounts.spotify.com/api/token'
self.__client_id = client_id
self.__client_secret = client_secret
self.__grant_type = 'client_credentials'
self.__body_params = {
'grant_type': self.__grant_type
}

self.__getData()
self.__getAccessToken()

#initialization of spotify client
self.client = spotipy.Spotify(self.__access_token)


def __getData(self):
try:

with open('.spotify_data.secret', 'rb') as f:
data = pickle.load(f)

self.__client_id = data['client_id']
self.__client_secret = data['client_secret']

except:
print('''
A new version is available on GitHub.\n
Download: https://github.com/artyshko/smd
''')
sys.exit()


def __getAccessToken(self):
#getting access_token by POST request to Spotify API
self.__access_token = requests.post(
response = requests.post(
self.__url,
data=self.__body_params,
auth=(
self.__client_id,
self.__client_secret
)
).json()['access_token']
).json()

#initialization of spotify client
self.client = spotipy.Spotify(self.__access_token)
self.__access_token = response['access_token']


def getSongInfo(self, uri):
Expand Down Expand Up @@ -185,11 +249,13 @@ def search(self, query):
except:
return False


def getDuration(self, uri):

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


def getAlbum(self, uri):
try:

Expand Down
18 changes: 17 additions & 1 deletion telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import apple
import random
import urllib.request
import pickle

import logging

Expand All @@ -22,9 +23,24 @@
class BotHandler(object):

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

def __getData(self):
try:

with open('.telegram_data.secret', 'rb') as f:
data = pickle.load(f)

self.token = data['token']

except:
print('''
A new version is available on GitHub.\n
Download: https://github.com/artyshko/smd
''')
sys.exit()

def getUpdates(self, offset=None, timeout=30):

method = 'getUpdates'
Expand Down

0 comments on commit 219f379

Please sign in to comment.