-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
127 lines (107 loc) · 4.72 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
# Initialization
import spotipy
from mutagen.flac import FLAC
from mutagen.mp3 import EasyMP3 as EMP3
from mutagen.oggvorbis import OggVorbis as OGGV
import os
import Stagger
import sys
# Setup variable(s)
clientID = "2531ad4b5e3c497ca0a9fce18d1280ab"
redirectURI = "http://127.0.0.1:8000/spotify/callback/"
cache = spotipy.CacheFileHandler(".cache_sp")
secret = spotipy.oauth2.SpotifyPKCE(clientID, redirectURI, cache_handler=cache, scope=["user-library-read"])
filesave = False
# Change cwd to the folder where this script is located
workDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(workDir)
# Check if folder `Audio` found - otherwise make one and exit
try:
fileList = os.listdir(".//Audio")
except FileNotFoundError:
os.mkdir(".//Audio")
print("\nPut audio files the folder \"Audio\" to scan them\n")
sys.exit()
# Check each file in folder `Audio`
count, success, fail = 0, 0, 0
for filename in fileList:
print("< ========================================\n")
# Find track and type
print(f"Item {count}\n--------------\nFile name: {filename}")
file = Stagger.findTypeFunc(".//Audio//"+filename)
# If file is supported
if file != "UNSUPPORTED":
# Assign variables in relation to file type
if (
type(file) is FLAC or
type(file) is OGGV or
type(file) is EMP3
):
tag = Stagger.tag.vorbis
titleTag = tag[0]
albumTag = tag[3]
albumArtistTag = tag[6]
# Build Query
## Check if title is already present - if not, ask for input
try:
trackTitleSearch = file[titleTag][0]
print(f"Track title: {trackTitleSearch}")
except KeyError:
trackTitleSearch = input("Track title not found - enter manually: ")
## Check if album name is already present - if not, ask for input
try:
trackAlbumSearch = file[albumTag][0]
print(f"Album: {trackAlbumSearch}")
except KeyError:
trackAlbumSearch = input("Album name not found - enter manually: ")
## Check if album artist is already present - if not, ask for input
try:
trackArtistSearch = file[albumArtistTag][0]
print(f"Album artist: {trackArtistSearch}")
except KeyError:
trackArtistSearch = input("Album artist not found - enter manually: ")
# Retrieve and write data
print("\n")
try:
x = 0
while True:
## Accumulate data
trackData = Stagger.trackMeta(trackTitleSearch + " " + trackArtistSearch, secret, x, tag)
## Verify data
if (
trackTitleSearch == trackData[titleTag][0] and
trackAlbumSearch == trackData[albumTag][0] and
trackArtistSearch == trackData[albumArtistTag][0]
):
### Clear previously existing tags
if filesave:
Stagger.initTags(".//Audio//"+filename)
file.save()
print("Previously existing tags removed\n")
### Output data to STDOUT and overwrite file's tags
indexCount = 0
for x, y in list(trackData.items()):
print(str(x) + ": " + str(y))
temp = y
Stagger.addTag(x, y, file)
indexCount += 1
### Save data to file and exit while loop (if {filesave} is True)
if filesave:
file.save()
print(f"\nSaved file {filename}")
else:
print("\nFile saving disabled")
success += 1
break
else: ### If current search item didn't match existing, report
print(f"Search item {str(x+1).zfill(2)} doesn't match")
x += 1
except (IndexError, TypeError): ### If data was not found on Spotify, report
print(f"\nMetadata for file {filename} not found on Spotify - maybe check its title, album and album artist tags?")
fail += 1
print("\n======================================== >\n\n\n")
count += 1
# A small detail thing
def countPrint(count: int): return f'{count} files' if count != 1 else f'{count} file'
# Print summary
print(f"Summary:\n{countPrint(success)} updated, {countPrint(fail)} failed, {countPrint(count - (success + fail))} ignored")