-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport.py
83 lines (63 loc) · 2.2 KB
/
import.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
import argparse
import json
from auth import get_authenticated_service
from subscriptions import import_subscriptions
from liked import like_videos
from playlist import create_playlist, add_videos_to_playlist
def wrong_operation_type(*arg):
print('Provided import type is not supported.')
quit(-1)
def import_playlist(service, videos, playlist):
playlist_id = create_playlist(service, playlist)
if not playlist_id:
return
add_videos_to_playlist(service, playlist_id, videos)
allowed_operations = {
'SUBSCRIPTIONS': 'subscriptions',
'LIKED': 'liked',
'PLAYLIST': 'playlist'
}
operation_map = {
allowed_operations['SUBSCRIPTIONS']: lambda service, channels, *arg: import_subscriptions(service, channels),
allowed_operations['LIKED']: lambda service, videos, *arg: like_videos(service, videos),
allowed_operations['PLAYLIST']: import_playlist
}
parser = argparse.ArgumentParser(
description = 'Import YouTube Takeout data.',
formatter_class = argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-c', '--client-secrets',
type = str,
default = 'client_secrets.json',
help = 'The client secrets file used for authorizing access to your YouTube account.'
)
parser.add_argument(
'-t', '--type',
choices = list(allowed_operations.values()),
default = allowed_operations['SUBSCRIPTIONS'],
help = 'Type of the data to import.'
)
parser.add_argument(
'-p', '--playlist',
type = str,
default = 'Playlist',
help = 'When import type is a playlist, this indicates the name of the playlist to create.'
)
parser.add_argument(
'file',
type = str,
nargs = '?',
default = 'takeout.json',
help = 'The takeout file in JSON format to import.'
)
args = parser.parse_args()
with open(args.file, encoding = 'utf-8', mode = 'r') as takeout_file:
parsed_takeout = json.load(takeout_file)
if not isinstance(parsed_takeout, list):
print('The Takeout file must contain an array of data.')
quit(-1)
operation = operation_map.get(args.type, wrong_operation_type)
print('Importing {}.'.format(args.type))
operation(get_authenticated_service(args.client_secrets), parsed_takeout, args.playlist)
print('Import complete.')