-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (57 loc) · 2.03 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
from decouple import config
from json import dumps as json_dumps
from datetime import datetime
from os import path, makedirs
import functions
acctoken_path = config("ACCESS_TOKEN_PATH")
my_client_id = config("CLIENT_ID")
my_client_secret = config("CLIENT_SECRET")
api = functions.get_api(acctoken_path, my_client_id, my_client_secret)
def get_default_output_path(ext:str) -> str:
makedirs(config("OUTPUT_PATH"), exist_ok=True)
now = datetime.now().strftime("%Y-%m-%d_%H%M")
path = config("OUTPUT_PATH") + f"\{now}"
return path + ext
def get_videos_by_playlist_id(playlist_id:str):
return functions.get_videos_by_playlist_id(api, playlist_id)
def run(command:str, args):
if command == "playlists":
if args.id is None:
result = functions.get_my_playlists(api)
else:
result = functions.get_playlist_by_id(api, args.id)
elif command == "pls_export":
if args.id is None:
result = functions.get_my_playlists(api)
else:
result = functions.get_playlist_by_id(api, args.id)
if args.format == "json":
args.output = get_default_output_path(".json")
elif args.format == "csv":
args.output = get_default_output_path(".csv")
elif command == "videos":
if args.playlist_id is None:
result = {}
else:
result = functions.get_videos_by_playlist_id(api, args.playlist_id)
elif command == "vid_export":
if args.playlist_id is None:
result = {}
else:
result = functions.get_videos_by_playlist_id(api, args.playlist_id)
if args.format == "json":
args.output = get_default_output_path(".json")
elif args.format == "csv":
args.output = get_default_output_path(".csv")
if args.output is None:
print( json_dumps(result, indent=2, default=str) )
else:
if args.format == "json":
functions.save_json(args.output, result)
saved = True
elif args.format == "csv":
saved = functions.save_csv(args.output, result)
if saved is True:
print("Saved to " + args.output)
else:
print("error saving output")