-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
121 lines (101 loc) · 4.32 KB
/
scan.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
import glob
import json
from pathlib import Path
import re
import shutil
import zipfile
import configparser
def findEncoreRoot(listf):
for path in listf:
if 'info.json' in path:
return (Path(path).parent, 0)
elif 'song.ini' in path:
return (Path(path).parent, 1)
db = {
"encore": True,
"RAW_path": "https://raw.githubusercontent.com/Encore-Developers/songs/main/",
"songs": []
}
def saveScan(data, fpath = 'encore.json'):
open(fpath, 'w').write(json.dumps(data, indent=4))
def scan():
zips = glob.glob('Songs/*.zip')
#print(zips)
try:
shutil.rmtree('covers/')
except Exception as e:
print('Covers folder does not exist!', e)
for zipa in zips:
with zipfile.ZipFile(zipa, "r") as zip_ref:
file_list = zip_ref.namelist()
encoreRoot, fileFormat = findEncoreRoot(file_list)
encoreRt = str(encoreRoot)
diffs = {}
artist = None
title = None
album = None
charters = []
length = 0
isRootFirstDir = encoreRt == '.'
readableRoot = '' if isRootFirstDir else encoreRt + '/'
cover = None
if fileFormat == 1:
with zip_ref.open(readableRoot + 'song.ini') as info:
infod = configparser.ConfigParser()
infotext = info.read().decode('utf-8')
infod.read_string(infotext)
artist = infod.get('song', 'artist')
title = infod.get('song', 'name')
diffs = {
"drums": infod.getint('song', 'diff_drums_pad', fallback=-1),
"bass": infod.getint('song', 'diff_bass_pad', fallback=-1),
"guitar": infod.getint('song', 'diff_guitar_pad', fallback=-1),
"vocals": infod.getint('song', 'diff_vocals_pad', fallback=-1),
"plastic_drums": infod.getint('song', 'diff_drums', fallback=-1),
"plastic_bass": infod.getint('song', 'diff_bass', fallback=-1),
"plastic_guitar": infod.getint('song', 'diff_guitar', fallback=-1),
"pitched_vocals": infod.getint('song', 'diff_vocals', fallback=-1),
}
charters = [infod.get('song', 'charter')]
length = int(infod.getint('song', 'song_length') / 1000)
album = infod.get('song', 'album')
if 'album.png' in zip_ref.namelist():
albumfile = 'album.png'
else:
albumfile = 'album.jpg'
cover = albumfile
else:
with zip_ref.open(readableRoot + 'info.json') as info:
infod = json.load(info)
artist = infod['artist']
title = infod['title']
diffs = infod['diff']
charters = infod.get('charters', [])
length = infod.get('length', 0)
album = infod.get('album', None)
cover = infod['art']
#print(infod)
songid = re.sub('-+', '-', re.sub('[^a-zA-Z0-9]', '-', f'{artist.replace(' ', '-')}-{title.replace(' ', '-')}'.lower())).rstrip('-')
with zip_ref.open(readableRoot + cover) as coverart:
Path('covers/').mkdir(exist_ok=True)
Path('covers/'+songid + '/').mkdir(exist_ok=True)
with open('covers/' + songid + '/' + cover, 'wb') as coverwrite:
coverwrite.write(coverart.read())
#print(file_list)
db['songs'].append(
{
"zip": zipa.replace('Songs\\', '').replace('Songs/', ''),
"root": encoreRt,
"isRootFirstDir": isRootFirstDir,
"artist": artist,
"title": title,
"album": album,
"diffs": diffs,
"charters": charters,
"secs": length,
"id": songid,
"art": cover
})
saveScan(db)
if __name__ == "__main__":
scan()