Create upload.py
Browse files
upload.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
+
from googleapiclient.http import MediaFileUpload
|
| 6 |
+
from google.auth.transport.requests import Request
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
TOKEN_FILE = '/app/token.pickle'
|
| 10 |
+
VIDEO_PATH = '/app/video.mp4'
|
| 11 |
+
|
| 12 |
+
def get_credentials():
|
| 13 |
+
creds = None
|
| 14 |
+
if os.path.exists(TOKEN_FILE):
|
| 15 |
+
with open(TOKEN_FILE, 'rb') as f:
|
| 16 |
+
creds = pickle.load(f)
|
| 17 |
+
if creds and creds.expired and creds.refresh_token:
|
| 18 |
+
creds.refresh(Request())
|
| 19 |
+
with open(TOKEN_FILE, 'wb') as f:
|
| 20 |
+
pickle.dump(creds, f)
|
| 21 |
+
return creds
|
| 22 |
+
|
| 23 |
+
def download_video(url, path):
|
| 24 |
+
print(f"Downloading video...")
|
| 25 |
+
session = requests.Session()
|
| 26 |
+
for attempt in range(3):
|
| 27 |
+
try:
|
| 28 |
+
r = session.get(url, stream=True, timeout=60, headers={
|
| 29 |
+
'User-Agent': 'Mozilla/5.0',
|
| 30 |
+
'Referer': 'https://www.pexels.com/'
|
| 31 |
+
})
|
| 32 |
+
with open(path, 'wb') as f:
|
| 33 |
+
for chunk in r.iter_content(chunk_size=65536):
|
| 34 |
+
if chunk:
|
| 35 |
+
f.write(chunk)
|
| 36 |
+
print("Download complete!")
|
| 37 |
+
return
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Attempt {attempt+1} failed: {e}")
|
| 40 |
+
if attempt == 2:
|
| 41 |
+
raise
|
| 42 |
+
|
| 43 |
+
def upload_video(title, description):
|
| 44 |
+
creds = get_credentials()
|
| 45 |
+
youtube = build('youtube', 'v3', credentials=creds)
|
| 46 |
+
body = {
|
| 47 |
+
'snippet': {
|
| 48 |
+
'title': title,
|
| 49 |
+
'description': description,
|
| 50 |
+
'categoryId': '22',
|
| 51 |
+
'tags': ['shorts', 'viral', 'trending']
|
| 52 |
+
},
|
| 53 |
+
'status': {
|
| 54 |
+
'privacyStatus': 'public',
|
| 55 |
+
'selfDeclaredMadeForKids': False
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
media = MediaFileUpload(VIDEO_PATH, mimetype='video/mp4', resumable=True)
|
| 59 |
+
request = youtube.videos().insert(part='snippet,status', body=body, media_body=media)
|
| 60 |
+
response = None
|
| 61 |
+
while response is None:
|
| 62 |
+
status, response = request.next_chunk()
|
| 63 |
+
if status:
|
| 64 |
+
print(f"Uploading... {int(status.progress() * 100)}%")
|
| 65 |
+
print(f"Uploaded! Video ID: {response['id']}")
|
| 66 |
+
return response['id']
|
| 67 |
+
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
video_url = sys.argv[1]
|
| 70 |
+
title = sys.argv[2]
|
| 71 |
+
description = sys.argv[3]
|
| 72 |
+
download_video(video_url, VIDEO_PATH)
|
| 73 |
+
video_id = upload_video(title, description)
|
| 74 |
+
print(f"SUCCESS:{video_id}")
|