-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-video.py
126 lines (108 loc) · 5.08 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
import json
import os
import time
from b2sdk.v2 import *
from PIL import Image
import googleapiclient.discovery
import google.oauth2.credentials
from googleapiclient.http import MediaFileUpload
from add_timestamp_comment import add_timestamp_comment
def main():
# get case number from case_number.txt
case_number = open("case_number.txt", "r").read().strip()
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = os.environ["YT_DEV_KEY"]
with open(f"json/{case_number}-interactions.json", "r") as file:
interactions = json.load(file)
img = Image.open(f"thumbnails/{case_number}.png")
resized = img.resize((1280, 720))
resized = resized.convert('RGB')
resized.save(f'thumbnails/{case_number}.jpg', optimize=True, quality=90)
# check if size exceeds 2097152 bytes, if so, compress more
if os.path.getsize(f"thumbnails/{case_number}.jpg") > 2097152:
resized.save(f'thumbnails/{case_number}.jpg', optimize=True, quality=80)
if os.path.getsize(f"thumbnails/{case_number}.jpg") > 2097152:
resized.save(f'thumbnails/{case_number}.jpg', optimize=True, quality=60)
if os.path.getsize(f"thumbnails/{case_number}.jpg") > 2097152:
raise Exception("Thumbnail too large")
with open("credentials.json", "r") as file:
credentials_json = file.read()
credentials = google.oauth2.credentials.Credentials.from_authorized_user_info(json.loads(credentials_json))
try:
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials, developerKey = DEVELOPER_KEY)
print("Uploading video...")
request = youtube.videos().insert(
part="snippet,status",
body={
"snippet": {
"categoryId": "25", # News & Politics
"description": interactions["youtube_description"],
"title": interactions["youtube_title"]
},
"status": {
"privacyStatus": "private",
"selfDeclaredMadeForKids": False
}
},
media_body=MediaFileUpload(f"videos/{case_number}.mp4", chunksize=-1, resumable=True)
)
response = request.execute()
video_id = response["id"]
print(json.dumps(response, indent=4))
if response and "id" in response:
print("")
print(f"Video id {response['id']} was successfully uploaded.")
time.sleep(1)
print("")
print("Uploading thumbnail...")
request = youtube.thumbnails().set(
videoId=video_id,
media_body=MediaFileUpload(f"thumbnails/{case_number}.jpg")
)
response = request.execute()
print(json.dumps(response, indent=4))
playlists = json.load(open("playlist_ids.json", "r"))
playlist_id = playlists[interactions["term"]]
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlist_id,
"resourceId": {
"kind": "youtube#video",
"videoId": video_id
}
}
}
)
response = request.execute()
print(f"Added video {video['docket_number']} to playlist OT {video['term']}")
add_timestamp_comment(youtube, video_id, interactions)
print("")
with open("comment.txt", "w") as file:
file.write("Uploaded to YouTube\n\n")
file.write(f"https://www.youtube.com/watch?v={video_id}")
except Exception as e:
print(e)
print("Possible authentication error, uploading to B2")
info = InMemoryAccountInfo()
b2_api = B2Api(info)
application_key = os.environ["B2_APP_KEY"]
application_key_id = os.environ["B2_APP_KEY_ID"]
b2_api.authorize_account("production", application_key_id, application_key)
bucket = b2_api.get_bucket_by_name("scotus-videos")
bucket.upload_local_file(local_file=f"videos/{case_number}.mp4", file_name=f"{case_number}.mp4")
bucket.upload_local_file(local_file=f"thumbnails/{case_number}.jpg", file_name=f"{case_number}.jpg")
bucket.upload_local_file(local_file=f"json/{case_number}-interactions.json", file_name=f"{case_number}-interactions.json")
with open("comment.txt", "w") as file:
file.write("Uploaded to B2 because upload to YouTube failed\n\n")
file.write(f"https://scotus-videos.s3.us-west-002.backblazeb2.com/{case_number}.mp4")
file.write(f"https://scotus-videos.s3.us-west-002.backblazeb2.com/{case_number}.jpg")
file.write(f"https://scotus-videos.s3.us-west-002.backblazeb2.com/{case_number}-interactions.json")
if __name__ == "__main__":
main()