-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_bridge.py
82 lines (57 loc) · 2.65 KB
/
app_bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from spotify.spotify_client import SpotifyClient
from youtube.youtube_client import YoutubeClient
from youtube.youtube_downloader import YoutubeDownloader
import json
class Bridge:
def __init__(self):
super().__init__()
self.youtube = YoutubeClient()
self.spotify = SpotifyClient()
self.downloader = YoutubeDownloader()
self.youtubePlaylistTitle = 'Spotify'
self.spotifyPlaylistTitle = 'Youtube'
def initCredentials(self):
self.youtube.getCredentials()
self.spotify.getToken()
self.areCredentialsSet = True
def downloadYoutubePlaylist(self):
if self.areCredentialsSet:
songs = []
playlistId = self.youtube.getPlaylistId(self.youtubePlaylistTitle)
if playlistId:
videoIds = self.youtube.getVideosUrlInPlaylist(playlistId)
if len(videoIds) > 0:
for id in videoIds:
self.downloader.download(id)
def fetchYoutubePlaylist(self):
if self.areCredentialsSet:
songs = []
with open('fetched_songs.json') as json_file:
data = json.load(json_file)
songs = data['songs']
playlistId = self.youtube.getPlaylistId(self.youtubePlaylistTitle)
if playlistId:
videos = self.youtube.getVideosInPlaylist(playlistId)
videosToFetch = [i for i in videos if i not in songs]
spotifyPlaylistId = self.spotify.getPlaylist(self.spotifyPlaylistTitle)
trackIds = []
if len(videosToFetch) > 0:
for videoTitle in videosToFetch:
trackId = self.getSpotifyTrackId(videoTitle)
if trackId:
trackIds.append(trackId)
if len(trackIds) > 0:
self.spotify.addTracksToPlaylist(spotifyPlaylistId, trackIds)
data = {}
data['songs'] = videos
with open('fetched_songs.json', 'w') as outfile:
json.dump(data, outfile)
def getSpotifyTrackId(self, title):
trackId = self.spotify.searchTrackId(title)
if not trackId and title.find("-") != -1:
titleParts = title.split("-")
trackId = self.spotify.searchTrackId(titleParts[1].strip(), titleParts[0].strip().lower())
if not trackId and titleParts[1].find("(") != -1:
title = titleParts[1].split("(")[0]
trackId = self.spotify.searchTrackId(title, titleParts[0].strip().lower())
return trackId