BunnyCDN_Gdrive/upload.py

126 lines
4.2 KiB
Python

import os
import requests
import json
# 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 getCollectionList():
response = requests.get(endpoint_listCollection, headers=headers)
collectionList = {}
if response.ok:
data = response.json()
for dirlist in data["items"]:
collectionList[dirlist["name"]] = dirlist["guid"]
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
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"]
else:
print("Errore : ", response.status_code)
print(response.content)
def checkIfFileExistsOnBunnyCDN(title_file):
url = f"{endpoint_listVideoForCollection}?collection={collectionId}&orderBy=date&itemsPerPage=10000"
response = requests.get(url, headers=headers)
if response.ok:
data = response.json()
for files in data["items"]:
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 BunnyCDN Stream (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):
print(count)
file_path = os.path.join(file_obj, files)
content_name = str(files)
videoId = getVideoID(content_name, count)
count += 1
if videoId == None:
pass
else:
headers = {
"AccessKey": API_KEY,
"Accept": "application/json",
}
url = endpoint_uploadVideo + videoId
with open(file_path, "rb") as file:
response = requests.request("PUT", url, data=file, headers=headers)
try:
response.raise_for_status()
except:
status_msg = {
"status": "error",
"HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured",
}
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)