-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_video.py
executable file
·139 lines (106 loc) · 3.95 KB
/
upload_video.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
#!/usr/bin/env python3
import json
import copy
import os
import sys
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import google_auth_oauthlib.flow
CLIENT_SECRETS_FILE = 'client_secrets.json'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
UPLOAD_BODY_TEMPLATE = {
'snippet': {
'categoryId': 28,
'title': '',
'description': '',
'tags': ['linkmeup', 'networking', 'technology', 'podcast', 'подкасты'],
},
'status': {
'privacyStatus': 'private',
'publishAt': None,
'selfDeclaredadeForKids': False,
},
'recordingDetails': {
'recordingDate': None
}
}
PLAYLIST_BODY_TEMPLATE = {
'snippet': {
'playlistId': '',
'position': 0,
'resourceId': {
'kind': 'youtube#video',
'videoId': ''
}
}
}
PLAYLIS_IDS = {
'telecom': 'PLHN9m7XN8U8HPjkJ-0PpZ493xNvQoFyFc',
'sysadmins': 'PLHN9m7XN8U8HM90YNcLRc8-_MBI4lCsSO',
'lte': 'PLHN9m7XN8U8HUvXi0bB6lGTJ5K8yOPR6q',
'poccielki': 'PLHN9m7XN8U8H22Xpmd-sMjTiS0woNLKaA',
'emigration': 'PLHN9m7XN8U8EqpFFiQoFJ9NVJvi2VU7Hj',
'irl': 'PLHN9m7XN8U8EKXAoSlpbgmdjbOYd78BM0',
'shorts': 'PLHN9m7XN8U8EfgSMi9Es6TuLCLJgjL9g5',
'other': 'PLHN9m7XN8U8G2bDGE66-JVsV3fezZnZtC'
}
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def main():
service = get_authenticated_service()
if os.path.exists('all_podcasts_w_ytid.json'):
all_podcasts = 'all_podcasts_w_ytid.json'
elif os.path.exists('all_podcasts_w_pd.json'):
all_podcasts = 'all_podcasts_w_pd.json'
else:
print('Files all_podcasts_w_ytid.json and all_podcasts_w_pd.json do not exist.')
print('Exit')
sys.exit(1)
with open(all_podcasts) as f:
podcasts = json.load(f)
for podcast in podcasts[::-1]:
print('-' * 100)
print(podcast['title'])
if podcast.get('youtube_id'):
print(' Video has already been uploaded.')
continue
mp4 = podcast.get('mp4')
if not mp4:
print(" OH, MY! No MP4!")
input()
continue
media_file = MediaFileUpload(mp4)
upload_body = copy.deepcopy(UPLOAD_BODY_TEMPLATE)
upload_body['snippet']['title'] = podcast['title']
upload_body['snippet']['description'] = podcast['body']
upload_body['snippet']['tags'].append(podcast['feed'])
upload_body['status']['publishAt'] = podcast['publishAt']
upload_body['recordingDetails']['recordingDate'] = podcast['recordingDate']
response_upload = service.videos().insert(
part='snippet,status,recordingDetails',
body = upload_body,
media_body = media_file
).execute()
print(f' Video is uploaded: https://www.youtube.com/watch?v={response_upload["id"]}')
podcast.update({"youtube_id": response_upload["id"]})
with open('all_podcasts_w_ytid.json', 'w') as f:
json.dump(podcasts, f)
print(' all_podcasts_w_ytid.json updated')
if podcast['feed'] in PLAYLIS_IDS:
playlist_id = PLAYLIS_IDS[podcast['feed']]
else:
playlist_id = PLAYLIS_IDS['other']
playlist_body = copy.deepcopy(PLAYLIST_BODY_TEMPLATE)
playlist_body['snippet']['playlistId'] = playlist_id
playlist_body['snippet']['resourceId']['videoId'] = response_upload['id']
request = service.playlistItems().insert(part='snippet', body=playlist_body).execute()
print(' Added to playlist')
print('=' * 100)
print('Finished')
if __name__ == '__main__':
main()