-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (90 loc) · 3.7 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
import os
import json
import lyricsgenius
def read_json_file(path):
with open(path, 'r') as f:
cache = f.read()
data = json.loads(cache)
return data
def write_json_file(path, data):
json_object = json.dumps(data, indent=4)
with open(path, 'w') as f:
f.write(json_object)
return data
def create_txt_file(path, data):
with open(path, 'w', encoding="utf-8") as f:
f.write(data)
print(f"File created at {path} !")
return data
def get_artist_album_from_path(start_path):
dir_path = os.path.dirname(start_path)
dir_name = dir_path.split('\\')[-1]
if '-' not in dir_name:
get_artist_album_from_path(dir_path)
# print("Oh no", dir_path)
else:
# print("Good", dir_path)
artist = dir_name.split(' - ')[0]
album = dir_name.split(' - ')[1]
return (artist, album)
def scan_directory(path):
content = os.scandir(path)
songs_data = read_json_file('songs.json')
for entry in content:
if entry.is_dir():
scan_directory(path + '/' + entry.name)
if '-' in entry.name:
artist = entry.name.split(' - ')[0]
album = entry.name.split(' - ')[1]
if artist not in songs_data.keys():
print("Create new artist")
songs_data[artist] = {}
if album not in songs_data[artist].keys():
print("Create new album")
songs_data[artist][album] = {}
if entry.is_file():
songs_data = read_json_file('songs.json')
name, ext = os.path.splitext(entry.name)
if ext == '.mp3':
before_name, real_name = name.split(' - ')
print(before_name, path)
real_path = os.path.realpath(path + '/' + entry.name)
dir_path = os.path.dirname(real_path)
if any(chr.isdigit() for chr in before_name):
print("In album")
artist, album = get_artist_album_from_path(dir_path)
else:
print("Single")
artist = before_name
album = 'single'
# Add artist and album to songs data
if artist not in songs_data.keys():
print("Create new artist")
songs_data[artist] = {}
songs_data[artist]['single'] = {}
if album not in songs_data[artist].keys():
print("Create new album")
songs_data[artist][album] = {}
songs_data[artist][album][real_name] = path + "/"
write_json_file('songs.json', songs_data)
# Scan the directory and get all data in songs.json
main_dir = input("Main directory : ")
write_json_file('songs.json', {})
scan_directory(
"C:/Users/Utilisateur/Documents/Celestin/Lab/AutoLRCDownloader/test_data")
genius = lyricsgenius.Genius(
"vbJ6FBsP92A5FXgvRbdTzR0325PZ5Xp1JY2B7tjzTzZqjSsmCaJeH1ITiFabGNqw")
songs_data = read_json_file('songs.json')
for artist in songs_data:
# Get artist
genius_artist = genius.search_artist(
artist, max_songs=1, sort="title", include_features=True)
for album in songs_data[artist]:
for track_name, path in songs_data[artist][album].items():
if not os.path.exists(path + track_name + '.lrc'):
# Get the song
song = genius.search_song(track_name, genius_artist.name)
# Create the .lrc file lyrics
if song is not None:
create_txt_file(
str(path + track_name + '.lrc'), str(song.lyrics))