Skip to content

Commit

Permalink
use format string and refactor spotify search loop to show progress.
Browse files Browse the repository at this point in the history
This makes it easier to visually confirm  that the all playlist tracks are being processed.

```
$  python YouTube.py https://open.spotify.com/playlist/0yasezcPZp9GvDpiIonDPv
Spotify tracks: 100/1587
Spotify tracks: 200/1587
...
Spotify tracks: 1400/1587
Spotify tracks: 1500/1587
Spotify tracks: 1587/1587
YouTube tracks: 10/1587
YouTube tracks: 20/1587
YouTube tracks: 30/1587
YouTube tracks: 40/1587
...
```

Format strings required Python 3.6+ so update the README.md.

Also added a newline to the "noresults" file so that the shell prompt is on a new line after `cat`ing the file.
  • Loading branch information
lmagasweran-sc committed May 28, 2021
1 parent b9c1b7f commit a296565
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Transfer a Spotify Playlist to YouTube Music

A simple command line script to clone a Spotify playlist to YouTube Music.
A simple command line script to clone a Spotify playlist to YouTube Music.

- Transfer a single Spotify playlist
- Transfer all playlists for a Spotify user


## Requirements

- Python 3 - https://www.python.org
- Python 3.6+ - https://www.python.org
- Python extensions: `pip install -U -r requirements`

## Setup

1. Initially you should create a new `settings.ini` containing your Spotify credentials.
1. Initially you should create a new `settings.ini` containing your Spotify credentials.

Simply copy `settings.ini.example` to a new file `settings.ini`:

Expand All @@ -25,13 +25,13 @@ $ cp settings.ini.example settings.ini

3. Fill in your `client_id` and `client_secret` from your Spotify app

4. For YouTube Music, open a console in the source code folder and run
4. For YouTube Music, open a console in the source code folder and run

`python Setup.py youtube`

Then, open your browser and copy your request headers according to the instructions at https://ytmusicapi.readthedocs.io/en/latest/setup.html
Then, open your browser and copy your request headers according to the instructions at https://ytmusicapi.readthedocs.io/en/latest/setup.html

All credentials are stored locally in the file `settings.ini`.
All credentials are stored locally in the file `settings.ini`.

## Transfer a playlist

Expand Down
17 changes: 8 additions & 9 deletions SpotifyExport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ def getSpotifyPlaylist(self, url):

results = self.api.playlist(playlistId)
name = results['name']
tracks = build_results(results['tracks']['items'])
total = int(results['tracks']['total'])
tracks = list()

count = 1
more = len(results['tracks']['items']) == 100
while more:
more_tracks = self.api.playlist_items(playlistId, offset = count * 100, limit=100)
print('requested from ' + str(count * 100))
tracks += build_results(more_tracks['items'])
more = len(more_tracks["items"]) == 100
count = count + 1
count = 0
while count < total:
req = self.api.playlist_items(playlistId, offset=count, limit=100)
tracks += build_results(req['items'])
print(f"Spotify tracks: {len(tracks)}/{total}")
count = count + 100

return {'tracks': tracks, 'name': name, 'description': html.unescape(results['description'])}

Expand Down
3 changes: 2 additions & 1 deletion YouTube.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ def search_songs(self, tracks):
videoIds.append(targetSong)

if i > 0 and i % 10 == 0:
print(str(i) + ' searched')
print(f"YouTube tracks: {i}/{len(songs)}")

with open(path + 'noresults_youtube.txt', 'w', encoding="utf-8") as f:
f.write("\n".join(notFound))
f.write("\n")
f.close()

return videoIds
Expand Down

0 comments on commit a296565

Please sign in to comment.