BunnyCDN_Gdrive/upload.py

141 lines
4.1 KiB
Python

import os
import requests
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
fileinDirectory = []
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()
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():
fld_id = collectionList[select_folder]
return fld_id
else:
print("Errore! Cartella non esistente!")
else:
pass
else:
print("Errore : ", response.status_code)
print(response.content)
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["result"]["files"]:
global fileinDirectory
fileinDirectory.append(files["title"])
if title_file in fileinDirectory:
print(f"File doppio : {title_file} - Non verrá caricato... ")
return True
else:
return False
else:
print("Errore : ", response.status_code)
print(response.content)
def upload(file_obj):
"""
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! ")
if count == 1:
fld_id = getFolderList()
for fileslist in os.listdir(file_obj):
print(count)
file_path = os.path.join(file_obj, fileslist)
print(file_path)
content_name = str(fileslist)
fileNoExtension = Path(file_path).stem
count += 1
videoexists = checkIfFileExists(fileNoExtension, fld_id)
if videoexists:
pass
else:
url = "https://marlin2.vtube.to/upload/01"
files = {
"file": (
content_name,
open(
file_path,
"rb",
),
),
"api_key": (None, "473u3rs97it2ge3hk77"),
}
response = requests.post(url, files=files)
try:
response.raise_for_status()
except:
status_msg = {
"status": "error",
"HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured",
"debug": response.content,
}
else:
status_msg = {
"status": "success",
"HTTP": response.status_code,
"msg": "The File Upload was Successful",
}
print(status_msg)
if __name__ == "__main__":
folderUpload = input("Inserisci la cartella dove sono presenti i file : ")
upload(folderUpload)