-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
111 lines (98 loc) · 3.84 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
from tinytag import TinyTag
from pathlib import Path
from spotipy.oauth2 import SpotifyOAuth
import json
import os
import spotipy
import re
# Input
print("Enter your username:")
username = input()
print("Enter path eg. \"D:/Music/\":")
path_input = input()
print("Enter playlist name: ")
playlist_name = input()
print("Enter description: ")
description = input()
# Data
fileList = Path(path_input).rglob('*.mp3')
data = {'music': []}
data_unable = {'music': []}
song_list = []
scope = "playlist-modify-public"
# Add these to environment variables on PC
client_id = os.environ.get('SPOTIPY_CLIENT_ID')
client_secret = os.environ.get('SPOTIPY_CLIENT_SECRET')
redirect_uri = os.environ.get('SPOTIPY_REDIRECT_URI')
count = 0
loop_count = 0
total_count = 0
# Authorization
token = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope,
username=username)
spotify_object = spotipy.Spotify(auth_manager=token)
# Playlist creation
spotify_object.user_playlist_create(user=username, name=playlist_name, public=True, description=f"{description}. "
f"Check out my GitHub"
f" page: "
f"https://github.com"
f"/agb2k")
path, dirs, files = next(os.walk(path_input))
num_files = len(files)
# Loop through files
print("Please wait till program is complete:")
for x in fileList:
count += 1
total_count += 1
file = str(x)
tag = TinyTag.get(file)
# Account for songs/albums with brackets in their title
modified_title = re.sub(r"\([^()]*\)", "", f"{tag.title}")
modified_album = re.sub(r"\([^()]*\)", "", f"{tag.album}")
song = f"{modified_title} {tag.artist} {modified_album}"
result = spotify_object.search(q=song)
try:
song_list.append(result['tracks']['items'][0]['uri'])
except:
try:
file_details = os.path.basename(x).split(' - ')
song_new = f"{modified_title} {file_details[0]}"
result_new = spotify_object.search(q=song_new)
song_list.append(result_new['tracks']['items'][0]['uri'])
except:
try:
song_new = f"{modified_title}"
result_new = spotify_object.search(q=song_new)
song_list.append(result_new['tracks']['items'][0]['uri'])
print(f"{song_new} may not be accurate search query")
except:
print(f"Unable to search query: {song_new}")
data_unable['music'].append({
'Title': tag.title,
'Artist': tag.artist,
'Album': tag.album
})
with open("Error.json", 'w') as fp_new:
json.dump(data_unable, fp_new, indent=4)
continue
data['music'].append({
'Title': tag.title,
'Artist': tag.artist,
'Album': tag.album
})
with open("Music.json", 'w') as fp:
json.dump(data, fp, indent=4)
if count >= 100:
pre_playlist = spotify_object.user_playlists(user=username)
playlist = pre_playlist['items'][0]['id']
spotify_object.playlist_add_items(playlist_id=playlist, items=song_list)
song_list.clear()
count = 0
loop_count += 1
continue
elif total_count >= num_files:
pre_playlist = spotify_object.user_playlists(user=username)
playlist = pre_playlist['items'][0]['id']
spotify_object.playlist_add_items(playlist_id=playlist, items=song_list)
print("Your playlist is now ready!")
break