Upload mirror.py
Browse files
mirror.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import pickle
|
| 5 |
+
from googleapiclient.discovery import build
|
| 6 |
+
from googleapiclient.http import MediaFileUpload
|
| 7 |
+
from google.auth.transport.requests import Request
|
| 8 |
+
|
| 9 |
+
TOKEN_FILE = '/app/token.pickle'
|
| 10 |
+
VIDEO_PATH = '/app/mirror_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_from_youtube(video_id, output_path):
|
| 24 |
+
url = f"https://www.youtube.com/watch?v={video_id}"
|
| 25 |
+
print(f"Downloading: {url}")
|
| 26 |
+
|
| 27 |
+
if os.path.exists(output_path):
|
| 28 |
+
os.remove(output_path)
|
| 29 |
+
|
| 30 |
+
result = subprocess.run([
|
| 31 |
+
'yt-dlp',
|
| 32 |
+
'-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 33 |
+
'--merge-output-format', 'mp4',
|
| 34 |
+
'-o', output_path,
|
| 35 |
+
'--no-playlist',
|
| 36 |
+
url
|
| 37 |
+
], capture_output=True, text=True)
|
| 38 |
+
|
| 39 |
+
if result.returncode != 0:
|
| 40 |
+
raise Exception(f"yt-dlp failed: {result.stderr}")
|
| 41 |
+
|
| 42 |
+
print("Download complete!")
|
| 43 |
+
|
| 44 |
+
def upload_to_channel_b(title, description):
|
| 45 |
+
creds = get_credentials()
|
| 46 |
+
youtube = build('youtube', 'v3', credentials=creds)
|
| 47 |
+
|
| 48 |
+
body = {
|
| 49 |
+
'snippet': {
|
| 50 |
+
'title': title,
|
| 51 |
+
'description': description,
|
| 52 |
+
'categoryId': '22',
|
| 53 |
+
'tags': ['shorts', 'viral', 'trending']
|
| 54 |
+
},
|
| 55 |
+
'status': {
|
| 56 |
+
'privacyStatus': 'public',
|
| 57 |
+
'selfDeclaredMadeForKids': False
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
media = MediaFileUpload(VIDEO_PATH, mimetype='video/mp4', resumable=True)
|
| 62 |
+
request = youtube.videos().insert(part='snippet,status', body=body, media_body=media)
|
| 63 |
+
|
| 64 |
+
response = None
|
| 65 |
+
while response is None:
|
| 66 |
+
status, response = request.next_chunk()
|
| 67 |
+
if status:
|
| 68 |
+
print(f"Uploading... {int(status.progress() * 100)}%")
|
| 69 |
+
|
| 70 |
+
new_id = response['id']
|
| 71 |
+
print(f"Uploaded! New Video ID: {new_id}")
|
| 72 |
+
|
| 73 |
+
if os.path.exists(VIDEO_PATH):
|
| 74 |
+
os.remove(VIDEO_PATH)
|
| 75 |
+
|
| 76 |
+
return new_id
|
| 77 |
+
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
video_id = sys.argv[1]
|
| 80 |
+
title = sys.argv[2] if len(sys.argv) > 2 else 'Mirrored Video'
|
| 81 |
+
description = sys.argv[3] if len(sys.argv) > 3 else ''
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
download_from_youtube(video_id, VIDEO_PATH)
|
| 85 |
+
new_video_id = upload_to_channel_b(title, description)
|
| 86 |
+
print(f"SUCCESS:{new_video_id}")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"FAILED: {e}")
|
| 89 |
+
sys.exit(1)
|