-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
206 lines (160 loc) · 6.34 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import ffmpeg
import ctypes.wintypes
force_clean = False # Default to false
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
# Random crap to obtain Documents folder
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(
None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
documents_path = buf.value.replace("\\", "/")
remote_db = list()
known_formats = [".mp3", ".flac", ".m4a", ".ogg"]
music_directories = [] # Your directories here as a list ["X:/Example", "X:/Example2"]
# Unsure about m4a but it's usually AAC stuff
# Rage should support OGG but I'd rather keep it to a "safe format"
# FLAC is unsupported
unsupported_formats = [".ogg", ".flac", ".m4a"]
music_path = documents_path + "/Rockstar Games/GTA V/User Music/"
# Util function to remove the "Database", unused
def clean():
if os.path.exists(music_path + "trackdb.txt"):
os.remove(music_path + "trackdb.txt")
# Sanitize inputs from files, removing escape sequences
# Useful for lists
def sanitize_file_input(f_input):
output = list()
for entry in f_input:
entry = entry.strip('\n')
output.append(entry)
return output
# Check whether the "Database" is in sync with the actual files
def sync_db(musicdir_state, db_state):
if musicdir_state == db_state:
return True
else:
return False
# Create a symlink for supported formats
def create_symlink(path, filename):
dest_path = music_path + filename
if not os.path.exists(dest_path):
os.symlink(path, dest_path)
# Convert unsupported formats
def convert_format(path, filename):
for fmat in unsupported_formats:
if fmat in filename:
# I tried strip but it did eat some track's names
dest_path = music_path + filename.replace(fmat, "") + ".mp3"
if not os.path.exists(dest_path):
try:
stream = ffmpeg.input(path)
stream = ffmpeg.output(stream, dest_path, f='mp3', acodec='libmp3lame',
audio_bitrate=320000)
ffmpeg.run(stream)
except Exception as e:
print("Could not convert due to: {}".format(e))
# Get a list of the tracks stored in the "Database"
def get_stored_db_list():
with open(music_path + "trackdb.txt", "r") as f:
music_list = f.readlines()
music_list_fixed = sanitize_file_input(music_list)
return music_list_fixed
# Do the main tasks of conversion and symlinking
def process_db(music_list_fixed):
for x in range(len(music_list_fixed)):
cur_track = music_list_fixed[x]
try:
track_name = music_list_fixed[x + 1]
except Exception as e:
break
if not ":/" in cur_track: # Look for drive path, shitty hack
continue
# TODO: Remove hardcoding
if ".flac" or ".m4a" or ".ogg" in cur_track: # Is an unsupported file?
convert_format(cur_track, track_name)
else:
try:
if os.path.exists(cur_track):
create_symlink(cur_track, track_name)
except Exception as e:
print("Failed due to {}".format(e))
# Verify whether the "Database" exists or not
def check_db_existence():
if os.path.exists(music_path + "trackdb.txt"):
return True
else:
return False
# Walk over the source directories and return a list of the files and things
def seek_source_files():
music_list = list()
for directory in music_directories:
for root, _, files in os.walk(directory):
for file in files:
# Fixes slashes, windows does support forward slash
root = root.replace("\\", "/")
for fmat in known_formats:
if file.endswith(fmat):
music_list.append(root + '/' + file)
music_list.append('\n' + file + '\n')
else:
continue
return music_list
# Write the "Database" file with the list of tracks and files and things
def write_db_file(music_list):
with open("X:/Documents/Rockstar Games/GTA V/User Music/trackdb.txt", "w") as f:
for track in music_list:
f.write(track)
# Verify the status of the database to write or not the "Database" file
def check_db_status(music_list):
if check_db_existence():
print("WARN: DB already exists")
db_status = sync_db(sanitize_file_input(
music_list), get_stored_db_list())
if db_status:
print("WARN: DB is in sync")
else:
print("WARN: DB is out of sync")
write_db_file(music_list)
else:
print("WARN: Track DB does not exist.")
write_db_file(music_list)
# Clean stale files
def clean_stale_files():
stored_db = get_stored_db_list()
# Cleanup for comparison
for ext in known_formats:
stored_db = [track.replace(ext, ".mp3") for track in stored_db]
# Extra cleanup for comparison (this is starting to smell)
for x in range(len(stored_db) // 2): # Prevents OOB
if ":/" in stored_db[x]:
# Pop paths so the lists match
stored_db.pop(x)
file_list = list()
# Optimization nightmare
for _, _, files in os.walk(music_path):
for file in files:
if ".txt" not in file: # A bit hardcoded
file_list.append(file)
# Bad GC? How are these variables still existing after the for loop execution? It's out of scope.
del files, file
# Sort lists for comparison
file_list.sort()
stored_db.sort()
if file_list == stored_db:
print("No stale files! :D")
else:
print("Stale files detected. Removing...")
for file in file_list:
if file not in stored_db:
print(file, "is being removed")
os.remove(
music_path + file)
def main():
if force_clean:
clean()
check_db_status(seek_source_files())
process_db(get_stored_db_list())
clean_stale_files()
if __name__ == "__main__":
main()