BunnyCDN_Gdrive/upload.py

146 lines
4.4 KiB
Python
Raw Normal View History

import os
import requests
2022-03-22 21:59:11 +00:00
from pathlib import Path
2022-04-15 15:21:42 +00:00
from requests_toolbelt import MultipartEncoder
2022-03-22 21:59:11 +00:00
# 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 = []
2022-04-15 15:21:42 +00:00
MAX_UPLOAD_GB = 750
UPLOADED_GB = 0
2022-03-22 21:59:11 +00:00
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")
2022-03-22 21:59:11 +00:00
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()
2022-03-22 21:59:11 +00:00
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:
2022-03-22 21:59:11 +00:00
pass
else:
print("Errore : ", response.status_code)
print(response.content)
2022-03-22 21:59:11 +00:00
def checkIfFileExists(title_file, fld_id):
2022-04-15 15:21:42 +00:00
url = f"{endpoint_fileList}&fld_id={fld_id}&per_page=2000"
response = requests.get(url, headers=headers)
if response.ok:
data = response.json()
2022-03-22 21:59:11 +00:00
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):
"""
2022-03-22 21:59:11 +00:00
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! ")
2022-03-22 21:59:11 +00:00
if count == 1:
fld_id = getFolderList()
for fileslist in os.listdir(file_obj):
print(count)
2022-03-22 21:59:11 +00:00
file_path = os.path.join(file_obj, fileslist)
print(file_path)
content_name = str(fileslist)
fileNoExtension = Path(file_path).stem
2022-04-15 15:21:42 +00:00
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
2022-03-22 21:59:11 +00:00
videoexists = checkIfFileExists(fileNoExtension, fld_id)
2022-04-15 15:21:42 +00:00
if videoexists or UPLOADED_GB == MAX_UPLOAD_GB or file_size_gb >= 10:
pass
else:
2022-04-15 15:21:42 +00:00
pass
2022-03-22 21:59:11 +00:00
url = "https://marlin2.vtube.to/upload/01"
2022-04-15 15:21:42 +00:00
m = MultipartEncoder(
fields={
"file": (content_name, open(file_path, "rb")),
"api_key": "473u3rs97it2ge3hk77",
}
)
response = requests.post(
url, data=m, headers={"Content-Type": m.content_type}
)
try:
response.raise_for_status()
except:
status_msg = {
"status": "error",
"HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured",
2022-03-22 21:59:11 +00:00
"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 : ")
2022-04-15 15:21:42 +00:00
upload(folderUpload)