Aggiunta funzione che permette di non caricare i file doppi

master
MickSlash 2022-02-22 13:52:37 +00:00
parent 75231952dd
commit c2304ac4c0
1 changed files with 125 additions and 92 deletions

115
upload.py
View File

@ -3,88 +3,121 @@ import requests
import json import json
# Video Library e Api Key ottenute tramite interfaccia di BunnyCDN https://panel.bunny.net/stream # Video Library e Api Key ottenute tramite interfaccia di BunnyCDN https://panel.bunny.net/stream
VIDEO_LIBRARY : int = 23411 VIDEO_LIBRARY: int = 23411
API_KEY : str = "2237b6ed-4454-4937-b5f870d55cc0-a148-4937" API_KEY: str = "2237b6ed-4454-4937-b5f870d55cc0-a148-4937"
endpoint_listCollection = f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/collections" endpoint_listCollection = (
f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/collections"
)
endpoint_createVideoID = f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/videos" endpoint_createVideoID = f"http://video.bunnycdn.com/library/{VIDEO_LIBRARY}/videos"
endpoint_uploadVideo = 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 = "" collectionId = ""
fileinDirectory = []
# Headers per BunnyCDN # Headers per BunnyCDN
headers = { headers = {"Accept": "application/json", "AccessKey": API_KEY}
"Accept": "application/json",
"AccessKey": API_KEY
}
def getCollectionList(): def getCollectionList():
response = requests.get(endpoint_listCollection,headers=headers) response = requests.get(endpoint_listCollection, headers=headers)
collectionList = {} collectionList = {}
if response.ok: if response.ok:
data = response.json() data = response.json()
for dirlist in data["items"]: for dirlist in data["items"]:
collectionList[dirlist["name"]] = dirlist["guid"] collectionList[dirlist["name"]] = dirlist["guid"]
listOfPossibleCollection = " - ".join(['{0}'.format(k) for k in collectionList.keys()]) listOfPossibleCollection = " - ".join(
["{0}".format(k) for k in collectionList.keys()]
)
print(listOfPossibleCollection) print(listOfPossibleCollection)
select_folder = input("Seleziona la cartella dove caricare i file : ") select_folder = input("Seleziona la cartella dove caricare i file : ")
if select_folder in collectionList.keys(): if select_folder in collectionList.keys():
guid = collectionList[select_folder] guid = collectionList[select_folder]
return guid return guid
else: else:
print("Errore! Cartella non esistente!") print("Errore! Cartella non esistente!")
else: else:
print("Errore : ",response.status_code) print("Errore : ", response.status_code)
print(response.content) print(response.content)
def getVideoID(title_obj,count):
def getVideoID(title_obj, count):
headers["Content-Type"] = "application/*+json" headers["Content-Type"] = "application/*+json"
if(count==1): if count == 1:
global collectionId global collectionId
collectionId = getCollectionList() collectionId = getCollectionList()
payload = {"title":title_obj,"collectionId":collectionId} payload = {"title": title_obj, "collectionId": collectionId}
payload = json.dumps(payload) payload = json.dumps(payload)
response = requests.post(endpoint_createVideoID,headers=headers,data=payload) if checkIfFileExistsOnBunnyCDN(title_obj) == True:
if response.ok: return
print(response.json()["guid"])
return response.json()["guid"]
else: else:
print("Errore : ",response.status_code) 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) print(response.content)
def upload(file_obj): 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 count = 1
# Carica i file da Gdrive a BunnyCDN Stream (Conviene Montare i file localmente con Rclone) if os.path.isdir(file_obj) != True:
if os.path.isdir(file_obj)!=True:
print(" La cartella selezionata non esiste! ") print(" La cartella selezionata non esiste! ")
for files in os.listdir(file_obj): for files in os.listdir(file_obj):
print(count)
file_path = os.path.join(file_obj, files) file_path = os.path.join(file_obj, files)
content_name = str(files) content_name = str(files)
videoId = getVideoID(content_name,count) videoId = getVideoID(content_name, count)
count +=1 count += 1
headers = { if videoId == None:
"AccessKey": API_KEY, pass
"Accept": "application/json", else:
} headers = {
url = endpoint_uploadVideo + videoId "AccessKey": API_KEY,
with open(file_path, "rb") as file: "Accept": "application/json",
response = requests.request("PUT",url, data=file, headers=headers) }
try: url = endpoint_uploadVideo + videoId
response.raise_for_status() with open(file_path, "rb") as file:
except: response = requests.request("PUT", url, data=file, headers=headers)
status_msg = { try:
response.raise_for_status()
except:
status_msg = {
"status": "error", "status": "error",
"HTTP": response.status_code, "HTTP": response.status_code,
"msg": "Upload Failed HTTP Error Occured", "msg": "Upload Failed HTTP Error Occured",
} }
else: else:
status_msg = { status_msg = {
"status": "success", "status": "success",
"HTTP": response.status_code, "HTTP": response.status_code,
"msg": "The File Upload was Successful", "msg": "The File Upload was Successful",
} }
print(status_msg) print(status_msg)
if __name__ == "__main__": if __name__ == "__main__":