Upload to vTube

vtube
MickSlash 2022-03-22 21:59:11 +00:00
parent c2304ac4c0
commit df0b9e7b2f
1 changed files with 77 additions and 62 deletions

139
upload.py
View File

@ -1,72 +1,77 @@
import os import os
import requests import requests
import json from pathlib import Path
# Endpoints ottenuti tramite http://xvideosharing.com/api/folder/list?key=key&fld_id=fld_id
endpoint_folderList = "https://vtube.to/api/folder/list?key=473u3rs97it2ge3hk77"
endpoint_fileList = "https://vtube.to/api/file/list?key=473u3rs97it2ge3hk77"
# Headers
headers = {"Content-Type": "application/json"}
# Global Variables
# Video Library e Api Key ottenute tramite interfaccia di BunnyCDN https://panel.bunny.net/stream
VIDEO_LIBRARY: int = 23411
API_KEY: str = "2237b6ed-4454-4937-b5f870d55cc0-a148-4937"
endpoint_listCollection = (
f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/collections"
)
endpoint_createVideoID = f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/videos"
endpoint_uploadVideo = f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/videos/"
endpoint_listVideoForCollection = (
f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/videos"
)
collectionId = ""
fileinDirectory = [] fileinDirectory = []
# Headers per BunnyCDN
headers = {"Accept": "application/json", "AccessKey": API_KEY} def visit_list(l):
return [visit(item) for item in l]
def getCollectionList(): def visit_dict(d):
response = requests.get(endpoint_listCollection, headers=headers) return {visit(k): visit(v) for k, v in d.items()}
def visit_str(s):
return s.encode("latin1").decode("utf-8")
def visit(node):
funcs = {
list: visit_list,
dict: visit_dict,
str: visit_str,
}
func = funcs.get(type(node))
if func:
return func(node)
else:
return node
def getFolderList():
response = requests.get(endpoint_folderList, headers=headers)
collectionList = {} collectionList = {}
if response.ok: if response.ok:
data = response.json() data = response.json()
for dirlist in data["items"]: if data["msg"] == "OK" and data["status"] == 200:
collectionList[dirlist["name"]] = dirlist["guid"] for folder in data["result"]["folders"]:
listOfPossibleCollection = " - ".join( collectionList[folder["name"]] = folder["fld_id"]
["{0}".format(k) for k in collectionList.keys()] listOfPossibleCollection = " - ".join(
) ["{0}".format(k) for k in collectionList.keys()]
print(listOfPossibleCollection) )
select_folder = input("Seleziona la cartella dove caricare i file : ") print(listOfPossibleCollection)
if select_folder in collectionList.keys(): select_folder = input("Seleziona la cartella dove caricare i file : ")
guid = collectionList[select_folder] if select_folder in collectionList.keys():
return guid fld_id = collectionList[select_folder]
return fld_id
else:
print("Errore! Cartella non esistente!")
else: else:
print("Errore! Cartella non esistente!") pass
else: else:
print("Errore : ", response.status_code) print("Errore : ", response.status_code)
print(response.content) print(response.content)
def getVideoID(title_obj, count): def checkIfFileExists(title_file, fld_id):
headers["Content-Type"] = "application/*+json" url = f"{endpoint_fileList}&fld_id={fld_id}"
if count == 1:
global collectionId
collectionId = getCollectionList()
payload = {"title": title_obj, "collectionId": collectionId}
payload = json.dumps(payload)
if checkIfFileExistsOnBunnyCDN(title_obj) == True:
return
else:
response = requests.post(endpoint_createVideoID, headers=headers, data=payload)
if response.ok:
print(response.json()["guid"])
return response.json()["guid"]
else:
print("Errore : ", response.status_code)
print(response.content)
def checkIfFileExistsOnBunnyCDN(title_file):
url = f"{endpoint_listVideoForCollection}?collection={collectionId}&orderBy=date&itemsPerPage=10000"
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.ok: if response.ok:
data = response.json() data = response.json()
for files in data["items"]: for files in data["result"]["files"]:
global fileinDirectory global fileinDirectory
fileinDirectory.append(files["title"]) fileinDirectory.append(files["title"])
if title_file in fileinDirectory: if title_file in fileinDirectory:
@ -81,28 +86,37 @@ def checkIfFileExistsOnBunnyCDN(title_file):
def upload(file_obj): def upload(file_obj):
""" """
Carica i file da Gdrive a BunnyCDN Stream (Conviene Montare i file localmente con Rclone) Carica i file da Gdrive a vTube (Conviene Montare i file localmente con Rclone)
Possibile usarlo per caricare file da locale Possibile usarlo per caricare file da locale
""" """
count = 1 count = 1
if os.path.isdir(file_obj) != True: if os.path.isdir(file_obj) != True:
print(" La cartella selezionata non esiste! ") print(" La cartella selezionata non esiste! ")
for files in os.listdir(file_obj): if count == 1:
fld_id = getFolderList()
for fileslist in os.listdir(file_obj):
print(count) print(count)
file_path = os.path.join(file_obj, files) file_path = os.path.join(file_obj, fileslist)
content_name = str(files) print(file_path)
videoId = getVideoID(content_name, count) content_name = str(fileslist)
fileNoExtension = Path(file_path).stem
count += 1 count += 1
if videoId == None: videoexists = checkIfFileExists(fileNoExtension, fld_id)
if videoexists:
pass pass
else: else:
headers = { url = "https://marlin2.vtube.to/upload/01"
"AccessKey": API_KEY, files = {
"Accept": "application/json", "file": (
content_name,
open(
file_path,
"rb",
),
),
"api_key": (None, "473u3rs97it2ge3hk77"),
} }
url = endpoint_uploadVideo + videoId response = requests.post(url, files=files)
with open(file_path, "rb") as file:
response = requests.request("PUT", url, data=file, headers=headers)
try: try:
response.raise_for_status() response.raise_for_status()
except: except:
@ -110,6 +124,7 @@ def upload(file_obj):
"status": "error", "status": "error",
"HTTP": response.status_code, "HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured", "msg": "Upload Failed HTTP Error Occured",
"debug": response.content,
} }
else: else:
status_msg = { status_msg = {