From 75231952dd0e7e308112f9e50f907cf70c81866b Mon Sep 17 00:00:00 2001 From: MickSlash Date: Mon, 21 Feb 2022 14:28:50 +0000 Subject: [PATCH] Carica file su '' --- upload.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 upload.py diff --git a/upload.py b/upload.py new file mode 100644 index 0000000..7b8d5f6 --- /dev/null +++ b/upload.py @@ -0,0 +1,92 @@ +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/" +collectionId = "" + +# 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) + 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 upload(file_obj): + count = 1 + # Carica i file da Gdrive a BunnyCDN Stream (Conviene Montare i file localmente con Rclone) + if os.path.isdir(file_obj)!=True: + print(" La cartella selezionata non esiste! ") + for files in os.listdir(file_obj): + file_path = os.path.join(file_obj, files) + content_name = str(files) + videoId = getVideoID(content_name,count) + count +=1 + 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) \ No newline at end of file