-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
219 lines (184 loc) · 6.53 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import datetime
import json
import time
import click
from spotify_client import SpotifyClient
from sync_manager import SyncManager
from youtube_client import YouTubeClient
manager = SyncManager()
@click.group()
def cli():
"""
Tune2Tube \n
From Spotify's Groove to YouTube's Show: Seamless Conversion! \n
GitHub: https://github.com/yogeshwaran01/spotify-playlist-to-youtube-playlist
"""
@click.command()
@click.argument("spotify_playlist_id")
@click.option("--public", is_flag=True, help="Create a public playlist")
@click.option("--private", is_flag=True, help="Create a public playlist")
@click.option("--name", "-n", help="Name of the YouTube playlist to be created")
@click.option("--description", "-d", help="Description of the playlist")
@click.option(
"--only-link",
"-l",
default=False,
help="just only link of playlist, logs not appear",
is_flag=True,
)
@click.option(
"--save-to-sync", "-s", is_flag=True, help="Save to list of playlist to sync"
)
def create(
spotify_playlist_id: str,
public: bool,
private: bool,
name: str,
description: str,
only_link: bool,
save_to_sync: bool,
):
"""Create a YouTube Playlist from Spotify Playlist"""
spotify = SpotifyClient()
youtube = YouTubeClient()
spotify_playlist = spotify.get_playlist(spotify_playlist_id)
if public:
privacy_status = "public"
elif private:
privacy_status = "private"
else:
privacy_status = "private"
if name and description:
youtube_playlist_id = youtube.create_playlist(
name,
description,
privacy_status=privacy_status,
)["id"]
elif description:
youtube_playlist_id = youtube.create_playlist(
spotify_playlist.name,
description,
privacy_status=privacy_status,
)["id"]
elif name:
youtube_playlist_id = youtube.create_playlist(
name,
spotify_playlist.description,
privacy_status=privacy_status,
)["id"]
else:
youtube_playlist_id = youtube.create_playlist(
spotify_playlist.name,
spotify_playlist.description,
privacy_status=privacy_status,
)["id"]
for track in spotify_playlist.tracks:
if not only_link:
click.echo(f"Searching for {track}")
video = youtube.search_video(track)
if not only_link:
click.echo(f"Song found: {video.title}")
youtube.add_song_playlist(youtube_playlist_id, video.video_id)
if not only_link:
click.echo("Song added")
time.sleep(1)
if not only_link:
click.echo(f"Playlit {privacy_status} playlist created")
click.echo(
f"Playlist found at https://www.youtube.com/playlist?list={youtube_playlist_id}"
)
click.echo(f"Playlist ID: {youtube_playlist_id}")
else:
click.echo(f"https://www.youtube.com/playlist?list={youtube_playlist_id}")
if save_to_sync:
manager.add_playlist(spotify_playlist_id, youtube_playlist_id, spotify_playlist.name, name, f"https://open.spotify.com/playlist/{spotify_playlist_id}", f"https://www.youtube.com/playlist?list={youtube_playlist_id}")
manager.commit()
@click.command()
@click.option("-s", "--spotify_playlist_id", help="Spotify playlist ID")
@click.option("-y", "--youtube_playlist_id", help="YouTube playlist ID")
@click.option(
"--only-link",
"-l",
default=False,
help="just only link of playlist, logs not appear",
is_flag=True,
)
def sync(
spotify_playlist_id: str,
youtube_playlist_id: str,
only_link: bool,
):
"""Sync your YouTube playlist with Spotify Playlist"""
playlists_to_be_synced = []
if spotify_playlist_id is None and youtube_playlist_id is None:
click.echo("Syncing Playlists ..")
playlists_to_be_synced = manager.playlists_to_be_synced
else:
playlists_to_be_synced.append(
{
"spotify_playlist_id": spotify_playlist_id,
"youtube_playlist_id": youtube_playlist_id,
}
)
spotify = SpotifyClient()
youtube = YouTubeClient()
for playlist in playlists_to_be_synced:
if not only_link:
click.echo(
f"Syncing between Spotify: {playlist['spotify_playlist_id']} and YouTube: {playlist['youtube_playlist_id']}"
)
spotify_playlist = spotify.get_playlist(playlist["spotify_playlist_id"])
youtube_playlist = youtube.get_playlist(playlist["youtube_playlist_id"])
youtube_playlist_ids = list(
map(
lambda x: {
"id": x["snippet"]["resourceId"]["videoId"],
"item": x["id"],
},
youtube_playlist,
)
)
yt_p_ids = list(
map(lambda x: x["snippet"]["resourceId"]["videoId"], youtube_playlist)
)
searched_playlist = []
for track in spotify_playlist.tracks:
video = youtube.search_video(track)
searched_playlist.append(video.video_id)
songs_to_be_added = []
songs_to_be_removed = []
for song in youtube_playlist_ids:
if song["id"] not in searched_playlist:
songs_to_be_removed.append(song["item"])
for song in searched_playlist:
if song not in yt_p_ids:
songs_to_be_added.append(song)
if not only_link:
click.echo("Adding songs ...")
for song in songs_to_be_added:
youtube.add_song_playlist(playlist["youtube_playlist_id"], song)
if not only_link:
click.echo("Removing songs ...")
for song in songs_to_be_removed:
youtube.remove_song_playlist(playlist["youtube_playlist_id"], song)
t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if not only_link:
click.echo(f"Spotify playlist {spotify_playlist.name} Synced on {t}")
click.echo(
f"Playlist found at https://www.youtube.com/playlist?list={playlist['youtube_playlist_id']}"
)
else:
click.echo(
f"https://www.youtube.com/playlist?list={playlist['youtube_playlist_id']}"
)
@click.command()
def clear():
"""Clear all the playlists thats to be synced"""
with open(manager.playlist_file, "w") as file:
json.dump([], file)
click.echo("cleared")
cli.add_command(create)
cli.add_command(sync)
cli.add_command(clear)
if __name__ == "__main__":
cli()