Aggiunta funzione che permette di non caricare i file doppi
parent
75231952dd
commit
c2304ac4c0
75
upload.py
75
upload.py
|
@ -3,28 +3,33 @@ 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"
|
||||
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
|
||||
}
|
||||
headers = {"Accept": "application/json", "AccessKey": API_KEY}
|
||||
|
||||
|
||||
def getCollectionList():
|
||||
response = requests.get(endpoint_listCollection,headers=headers)
|
||||
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()])
|
||||
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():
|
||||
|
@ -33,42 +38,71 @@ def getCollectionList():
|
|||
else:
|
||||
print("Errore! Cartella non esistente!")
|
||||
else:
|
||||
print("Errore : ",response.status_code)
|
||||
print("Errore : ", response.status_code)
|
||||
print(response.content)
|
||||
|
||||
def getVideoID(title_obj,count):
|
||||
|
||||
def getVideoID(title_obj, count):
|
||||
headers["Content-Type"] = "application/*+json"
|
||||
if(count==1):
|
||||
if count == 1:
|
||||
global collectionId
|
||||
collectionId = getCollectionList()
|
||||
payload = {"title":title_obj,"collectionId":collectionId}
|
||||
payload = {"title": title_obj, "collectionId": collectionId}
|
||||
payload = json.dumps(payload)
|
||||
response = requests.post(endpoint_createVideoID,headers=headers,data=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("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
|
||||
# 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! ")
|
||||
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
|
||||
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)
|
||||
response = requests.request("PUT", url, data=file, headers=headers)
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except:
|
||||
|
@ -86,7 +120,6 @@ def upload(file_obj):
|
|||
print(status_msg)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
folderUpload = input("Inserisci la cartella dove sono presenti i file : ")
|
||||
upload(folderUpload)
|
Loading…
Reference in New Issue