Aggiornare 'upload.py'

master
MickSlash 2022-04-15 15:20:15 +00:00
parent 40b79a9602
commit c1b30e5087
1 changed files with 85 additions and 64 deletions

131
upload.py
View File

@ -1,72 +1,80 @@
import os
import requests
import json
from pathlib import Path
from requests_toolbelt import MultipartEncoder
# 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 = []
# Headers per BunnyCDN
headers = {"Accept": "application/json", "AccessKey": API_KEY}
MAX_UPLOAD_GB = 750
UPLOADED_GB = 0
def getCollectionList():
response = requests.get(endpoint_listCollection, headers=headers)
def visit_list(l):
return [visit(item) for item in l]
def visit_dict(d):
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 = {}
if response.ok:
data = response.json()
for dirlist in data["items"]:
collectionList[dirlist["name"]] = dirlist["guid"]
if data["msg"] == "OK" and data["status"] == 200:
for folder in data["result"]["folders"]:
collectionList[folder["name"]] = folder["fld_id"]
listOfPossibleCollection = " - ".join(
["{0}".format(k) for k in collectionList.keys()]
)
print(listOfPossibleCollection)
select_folder = input("Seleziona la cartella dove caricare i file : ")
if select_folder in collectionList.keys():
guid = collectionList[select_folder]
return guid
fld_id = collectionList[select_folder]
return fld_id
else:
print("Errore! Cartella non esistente!")
else:
print("Errore : ", response.status_code)
print(response.content)
def getVideoID(title_obj, count):
headers["Content-Type"] = "application/*+json"
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"]
pass
else:
print("Errore : ", response.status_code)
print(response.content)
def checkIfFileExistsOnBunnyCDN(title_file):
url = f"{endpoint_listVideoForCollection}?collection={collectionId}&orderBy=date&itemsPerPage=10000"
def checkIfFileExists(title_file, fld_id):
url = f"{endpoint_fileList}&fld_id={fld_id}&per_page=2000"
response = requests.get(url, headers=headers)
if response.ok:
data = response.json()
for files in data["items"]:
for files in data["result"]["files"]:
global fileinDirectory
fileinDirectory.append(files["title"])
if title_file in fileinDirectory:
@ -81,28 +89,40 @@ def checkIfFileExistsOnBunnyCDN(title_file):
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
"""
count = 1
if os.path.isdir(file_obj) != True:
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)
file_path = os.path.join(file_obj, files)
content_name = str(files)
videoId = getVideoID(content_name, count)
file_path = os.path.join(file_obj, fileslist)
print(file_path)
content_name = str(fileslist)
fileNoExtension = Path(file_path).stem
file_size = os.path.getsize(file_path)
file_size_gb = round(file_size / (1024 * 1024 * 1024), 3)
global UPLOADED_GB
UPLOADED_GB += file_size_gb
count += 1
if videoId == None:
videoexists = checkIfFileExists(fileNoExtension, fld_id)
if videoexists or UPLOADED_GB == MAX_UPLOAD_GB or file_size_gb >= 10:
pass
else:
headers = {
"AccessKey": API_KEY,
"Accept": "application/json",
pass
url = "https://marlin2.vtube.to/upload/01"
m = MultipartEncoder(
fields={
"file": (content_name, open(file_path, "rb")),
"api_key": "473u3rs97it2ge3hk77",
}
url = endpoint_uploadVideo + videoId
with open(file_path, "rb") as file:
response = requests.request("PUT", url, data=file, headers=headers)
)
response = requests.post(
url, data=m, headers={"Content-Type": m.content_type}
)
try:
response.raise_for_status()
except:
@ -110,6 +130,7 @@ def upload(file_obj):
"status": "error",
"HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured",
"debug": response.content,
}
else:
status_msg = {