def full_analysis(playlist_id): # Get access token from the headers and init Spotify objectg access_token = request.headers['access_token'] spotify = Spotify(access_token) tracks, analysis_list, artist_list, last_update_iso = spotify.get_playlist_items(playlist_id) # get most recently added date last_update = parser.isoparse(last_update_iso) key_data, feel_data, genre_data, tempo_data, duration_data = utils.analyze_playlist(analysis_list,tracks,artist_list) payload = {} payload['keys'] = key_data payload['feel'] = feel_data payload['genres'] = genre_data payload['tempo'] = tempo_data payload['duration'] = duration_data payload['last_update'] = last_update del spotify return jsonify(payload)
def tracks_get(playlist_id): access_token = request.headers['access_token'] spotify = Spotify(access_token) tracks = spotify._get_playlist_tracks(playlist_id)['items'] del spotify return jsonify(tracks)
def playlist_recs(playlist_id): # Get access token from the headers and generate spotify's required header access_token = request.headers['access_token'] spotify = Spotify(access_token) recs = spotify.get_recommendations() del spotify return jsonify(recs)
def get_spotify_tracks(): # print(request.headers, flush=True) access_token = request.headers['access_token'] data = request.get_json()['data'] track_ids = data['track_ids'] sp = Spotify(access_token) tracks = sp.get_tracks(track_ids) return_package = { 'message': 'tracks recieved', 'tracks': tracks } return jsonify(tracks)
def playlists_get(username): access_token = request.headers['access_token'] spotify = Spotify(access_token) playlists = spotify.get_playlists() playlists_filtered = [] for playlist in playlists['items']: if playlist['tracks']['total'] == 0: continue else: playlists_filtered.append(playlist) del spotify return jsonify(playlists_filtered)
def lyrics_analysis(playlist_id): # Get access token from the headers and generate spotify's required header access_token = request.headers['access_token'] spotify = Spotify(access_token) tracks = spotify._get_playlist_tracks(playlist_id)['items'] del spotify tracks = [x['track'] for x in tracks] cnt = 0 # select random sample of songs max_songs=20 tracks = random.sample(tracks,k=min(max_songs, len(tracks))) lyrics_count = {} for track in tracks: if cnt >= max_songs: break #print('Track {}/{}'.format(cnt,len(tracks))) lyrics = get_lyrics(track) if not lyrics: continue words = parse_lyrics(lyrics) for word in words: #word = word.capitalize() if word in lyrics_count: lyrics_count[word] += 1 else: lyrics_count[word] = 1 cnt += 1 lyrics_count_sorted = {k: v for k, v in sorted(lyrics_count.items(), key=lambda item: item[1], reverse=True)} # format payload for React library usage lyrics_analysis = [] for key in lyrics_count_sorted: lyrics_analysis.append({'text':key,'value':lyrics_count_sorted[key]}) return jsonify(lyrics_analysis[0:200])
def make_playlist(): access_token = request.headers['access_token'] data = request.get_json()['data'] track_ids = data['track_ids'] sp = Spotify(access_token) playlist_name = 'heartbreakers' href = sp.create_playlist(playlist_name,'a playlist made for u by the ones who probably ghosted you') sp.add_songs(track_ids) sp.add_cover_art('imgs/cover_art.png') return_package = { 'message': 'playlist {} successfully created'.format(playlist_name), 'link': href } return jsonify(return_package)
import sys sys.path.append('../') from lib.Tinder import Tinder from lib.TinderSMSAuth import TinderSMSAuth from lib.Spotify import Spotify if __name__ == '__main__': tndrAuth = TinderSMSAuth() tndr = Tinder(auth=tndrAuth) sp = Spotify() sp.check_playlist() matches = tndr.get_matches() # print(matches) track_list = [] i = 1 print('Scanning matches...') for match in matches: print('{} / {} matches scanned \r'.format(i, len(matches)), end='') user = tndr.get_user(match['person']['_id'])['results'] if 'spotify_theme_track' in user: track_id = user['spotify_theme_track']['uri'] track_list.append(track_id) i += 1 print('') print('Adding songs to playlist') sp.create_playlist( 'heartbreakers', 'a playlist made for you by the ones who probably ghosted you') sp.add_songs(track_list)
def playlist_get(playlist_id): access_token = request.headers['access_token'] spotify = Spotify(access_token) playlist = spotify.get_playlist(playlist_id) return jsonify(playlist)
def get_access_token(self, access_token): self.spotify = Spotify(access_token) self.categories = self.spotify.getCategories() self.layout = self.window.findChild(QGridLayout, "content") self.setup_category_view()
class Ui(QMainWindow): def __init__(self, app): super(Ui, self).__init__() self.categories = [] self.current_view = StepViews.CATEGORY_VIEW self.app = app ui_file_name = "view.ui" ui_file = QFile(ui_file_name) if not ui_file.open(QIODevice.ReadOnly): print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString())) sys.exit(-1) loader = QUiLoader() self.window = loader.load(ui_file) ui_file.close() if not self.window: print(loader.errorString()) sys.exit(-1) self.auth_manager = AuthManager(self.app) self.auth_manager.access_token_signal.connect(self.get_access_token) self.button = self.window.findChild(QPushButton, "loginToSpotify") self.button.clicked.connect(self.authenticate) self.back_button = self.window.findChild(QPushButton, "back") self.back_button.clicked.connect(self.setup_back_view) @Slot(str) def get_access_token(self, access_token): self.spotify = Spotify(access_token) self.categories = self.spotify.getCategories() self.layout = self.window.findChild(QGridLayout, "content") self.setup_category_view() def setup_back_view(self): if self.current_view == StepViews.CATEGORY_PLAYLISTS_VIEW: self.current_view = StepViews.CATEGORY_VIEW self.setup_category_view() def setup_category_view(self): self.current_view = StepViews.CATEGORY_VIEW i, j = 0, 0 for category in self.categories: data = urllib.request.urlopen(category["icons"][0]["url"]).read() label = ClickableLabel(self) label.setScaledContents(True) label.setFixedSize(190, 190) label.dataId = category["id"] label.clicked.connect(self.category_click) image = QImage(32, 32, QImage.Format_RGB32) image.loadFromData(data) painter = QPainter(image) painter.setPen(QPen(QColor("white"))) painter.setFont(QFont("Roboto", 22, QFont.Bold)) painter.drawText(QRect(0, 0, image.width(), image.height() - 25), Qt.AlignCenter | Qt.AlignBottom, category["name"]) painter.end() pixmap = QPixmap(image) label.setPixmap(pixmap) self.layout.addWidget(label, i, j) j += 1 if j % 4 == 0: i += 1 j = 0 def setup_category_playlists_view(self): self.current_view = StepViews.CATEGORY_PLAYLISTS_VIEW i, j = 0, 0 for playlist in self.categoryPlaylists: data = urllib.request.urlopen(playlist["images"][0]["url"]).read() label = ClickableLabel(self) label.setScaledContents(True) label.setFixedSize(190, 190) image = QImage(32, 32, QImage.Format_RGB32) image.loadFromData(data) pixmap = QPixmap(image) label.setPixmap(pixmap) self.layout.addWidget(label, i, j) j += 1 if j % 4 == 0: i += 1 j = 0 def show(self): self.window.show() def clear_layout(self): while self.layout.count(): child = self.layout.takeAt(0) if child.widget(): child.widget().deleteLater() @Slot(str) def category_click(self, id): self.categoryPlaylists = self.spotify.getCategoryPlaylists(id) self.clear_layout() self.setup_category_playlists_view() def authenticate(self): self.auth_manager.authenticate()