Aggiornare 'upload.py'
parent
40b79a9602
commit
c1b30e5087
131
upload.py
131
upload.py
|
@ -1,72 +1,80 @@
|
||||||
import os
|
import os
|
||||||
import requests
|
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 = []
|
fileinDirectory = []
|
||||||
|
MAX_UPLOAD_GB = 750
|
||||||
# Headers per BunnyCDN
|
UPLOADED_GB = 0
|
||||||
headers = {"Accept": "application/json", "AccessKey": API_KEY}
|
|
||||||
|
|
||||||
|
|
||||||
def getCollectionList():
|
def visit_list(l):
|
||||||
response = requests.get(endpoint_listCollection, headers=headers)
|
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 = {}
|
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"]:
|
||||||
|
collectionList[folder["name"]] = folder["fld_id"]
|
||||||
listOfPossibleCollection = " - ".join(
|
listOfPossibleCollection = " - ".join(
|
||||||
["{0}".format(k) for k in collectionList.keys()]
|
["{0}".format(k) for k in collectionList.keys()]
|
||||||
)
|
)
|
||||||
print(listOfPossibleCollection)
|
print(listOfPossibleCollection)
|
||||||
select_folder = input("Seleziona la cartella dove caricare i file : ")
|
select_folder = input("Seleziona la cartella dove caricare i file : ")
|
||||||
if select_folder in collectionList.keys():
|
if select_folder in collectionList.keys():
|
||||||
guid = collectionList[select_folder]
|
fld_id = collectionList[select_folder]
|
||||||
return guid
|
return fld_id
|
||||||
else:
|
else:
|
||||||
print("Errore! Cartella non esistente!")
|
print("Errore! Cartella non esistente!")
|
||||||
else:
|
else:
|
||||||
print("Errore : ", response.status_code)
|
pass
|
||||||
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"]
|
|
||||||
else:
|
else:
|
||||||
print("Errore : ", response.status_code)
|
print("Errore : ", response.status_code)
|
||||||
print(response.content)
|
print(response.content)
|
||||||
|
|
||||||
|
|
||||||
def checkIfFileExistsOnBunnyCDN(title_file):
|
def checkIfFileExists(title_file, fld_id):
|
||||||
url = f"{endpoint_listVideoForCollection}?collection={collectionId}&orderBy=date&itemsPerPage=10000"
|
url = f"{endpoint_fileList}&fld_id={fld_id}&per_page=2000"
|
||||||
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 +89,40 @@ 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
|
||||||
|
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
|
count += 1
|
||||||
if videoId == None:
|
videoexists = checkIfFileExists(fileNoExtension, fld_id)
|
||||||
|
if videoexists or UPLOADED_GB == MAX_UPLOAD_GB or file_size_gb >= 10:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
headers = {
|
pass
|
||||||
"AccessKey": API_KEY,
|
url = "https://marlin2.vtube.to/upload/01"
|
||||||
"Accept": "application/json",
|
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.post(
|
||||||
response = requests.request("PUT", url, data=file, headers=headers)
|
url, data=m, headers={"Content-Type": m.content_type}
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except:
|
except:
|
||||||
|
@ -110,6 +130,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 = {
|
||||||
|
|
Loading…
Reference in New Issue