-
Notifications
You must be signed in to change notification settings - Fork 150
/
reverse_playlist.py
63 lines (50 loc) · 1.77 KB
/
reverse_playlist.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
#!/usr/bin/env python3
import json
import os
import shutil
from argparse import ArgumentParser
def reverse_playlist(input_file="playlists.json", verbose=True, replace=False) -> int:
if os.path.exists(input_file) and not replace:
if verbose:
print(
"Output file already exists and no replace argument detected, exiting..."
)
return 1
print("Backing up file...")
shutil.copyfile(input_file, input_file.split(".")[0] + "_backup.json")
# Load the JSON file
with open(input_file, "r") as file:
if verbose:
print("Loading initial JSON file...")
data = json.load(file)
# Copy the data to a new dictionary
data2 = data.copy()
if verbose:
print("Reversing playlists...")
# Reverse the order of items in the "tracks" list
for i in range(len(data2["playlists"])):
# Reverse the tracks in the playlist
data2["playlists"][i]["tracks"] = data2["playlists"][i]["tracks"][::-1]
if verbose:
print("Writing to file... (this can take a while)")
# Write the modified JSON back to the file
with open(input_file, "w") as file:
json.dump(data2, file)
if verbose:
print("Done!")
print(f"File can be found at {input_file}")
return 0
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("input_file", type=str, help="Path to the input file")
parser.add_argument(
"-v", "--verbose", action="store_false", help="Enable verbose mode"
)
parser.add_argument(
"-r",
"--replace",
action="store_true",
help="Replace the output file if already existing",
)
args = parser.parse_args()
reverse_playlist(args.input_file, args.verbose, args.replace)