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

123
upload.py
View File

@ -1,72 +1,77 @@
import os
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 = []
# Headers per BunnyCDN
headers = {"Accept": "application/json", "AccessKey": API_KEY}
def visit_list(l):
return [visit(item) for item in l]
def getCollectionList():
response = requests.get(endpoint_listCollection, headers=headers)
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}"
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 +86,37 @@ 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
count += 1
if videoId == None:
videoexists = checkIfFileExists(fileNoExtension, fld_id)
if videoexists:
pass
else:
headers = {
"AccessKey": API_KEY,
"Accept": "application/json",
url = "https://marlin2.vtube.to/upload/01"
files = {
"file": (
content_name,
open(
file_path,
"rb",
),
),
"api_key": (None, "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, files=files)
try:
response.raise_for_status()
except:
@ -110,6 +124,7 @@ def upload(file_obj):
"status": "error",
"HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured",
"debug": response.content,
}
else:
status_msg = {