From 4e498f3c3b26dc1e0f11e95843eb0c822d14cf8f Mon Sep 17 00:00:00 2001 From: MickSlash Date: Wed, 14 Dec 2022 22:11:41 +0000 Subject: [PATCH] Aggiunto supporto alla paginazione delle API --- onedriveLinks.py | 198 +++++++++++++++++++++++++---------------------- 1 file changed, 107 insertions(+), 91 deletions(-) diff --git a/onedriveLinks.py b/onedriveLinks.py index 8a17edd..43b10bc 100644 --- a/onedriveLinks.py +++ b/onedriveLinks.py @@ -1,91 +1,107 @@ -import re -from functools import reduce -import mysql.connector -import requests - -LC_URL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/?path=/" -LC_FILEURL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/raw/?path=/" -session = requests.Session() - -# CONNESSIONE AL DB -mydb = mysql.connector.connect( - host="localhost", - user="root", - password="Lost66tmv", - database="lordb" -) - -def DBSearchAndUpdate(link,value): - mycursor = mydb.cursor() - # sql = "SELECT * FROM `tbl_episode` WHERE titolo LIKE %s" - sql = "UPDATE tbl_episode SET `link embedded` = %s WHERE titolo LIKE %s" - val = (f'{link}',f'%{value}%') - mycursor.execute(sql,val) - myresult = mycursor.fetchone() - print(myresult) - if mycursor.rowcount == 1: - print(myresult) - mydb.commit() - else: - print("Impossibile trovare il file desiderato!") - - -def replaceString(string): - repls = (".", " "), ("mp4", ""), ("_", " "),("SUB"," "),("ITA"," "),("XX","xx") - return reduce(lambda a, kv: a.replace(*kv), repls, string) - - -def removeYearString(string): - pattern = r"\d{4}" - str_data = re.sub(pattern, "", string) - return str_data - - -def findOneDriveLinks(str): - str = replaceString(str) - str = removeYearString(str) - match_str = re.findall( - r"([a-zA-Z0-9]+)", str - ) # In caso la stringa manipolata non dovesse dare risultati sul DB, viene suddiva in gruppi e testati fino a trovare un match - if len(match_str)>0: - title = separateUpperString(match_str[0]) - del(match_str[0]) - match_str = title + match_str - new_string = "" - for x in match_str: - new_string += x + " " - new_string = new_string.rstrip() - print(new_string) - return new_string - else: - print(match_str) - - -def separateUpperString(str): - title = re.findall('[a-zA-Z][^A-Z]*', str) - return title - -def apiOneDrive(folderType=None,nameFile=None): - apiURL = f"{LC_URL}{folderType}" - rawURL = f"{LC_FILEURL}{folderType}" - r = session.get(apiURL) - if r.status_code == 200: - data = r.json() - counter = 1 - fileCount = len(data["folder"]["value"]) - for file in data["folder"]["value"]: - mp4File = file["name"] - if nameFile: - if counter<10: - fileName = f"{nameFile} Ep 0{counter}" - else: - fileName = f"{nameFile} Ep {counter}" - else: - fileName = findOneDriveLinks(file["name"]) - link = f"{rawURL}{mp4File}" - counter+=1 - DBSearchAndUpdate(link,fileName) - print(f"Modificati con successo {fileCount} file in totale!") - else: - print(r.status_code) - print(r.content) +import re +from functools import reduce +import mysql.connector +import requests + +LC_URL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/?path=/" +LC_FILEURL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/raw/?path=/" +session = requests.Session() + +# CONNESSIONE AL DB +mydb = mysql.connector.connect( + host="localhost", + user="root", + password="Lost66tmv", + database="lordb" +) + +def DBSearchAndUpdate(link,value): + mycursor = mydb.cursor(buffered=True) + sql = "UPDATE tbl_episode SET `link embedded` = %s WHERE `titolo` LIKE %s LIMIT 1" + val = (f'{link}',f'%{value}%') + mycursor.execute(sql,val) + print(f"{mycursor.rowcount} - {value} - {link}") + if mycursor.rowcount == 1: + text = f"Modificato : {value}" + writeToText(text) + mydb.commit() + else: + text = f"Non Modificato : {value}" + writeToText(text) + + +def replaceString(string): + repls = (".", " "), ("mp4", ""), ("_", " "),("SUB"," "),("ITA"," "),("XX","xx") + return reduce(lambda a, kv: a.replace(*kv), repls, string) + + +def removeYearString(string): + pattern = r"\d{4}" + str_data = re.sub(pattern, "", string) + return str_data + + +def findOneDriveLinks(str): + str = replaceString(str) + str = removeYearString(str) + match_str = re.findall( + r"([a-zA-Z0-9]+)", str + ) # In caso la stringa manipolata non dovesse dare risultati sul DB, viene suddiva in gruppi e testati fino a trovare un match + if len(match_str)>0: + title = separateUpperString(match_str[0]) + del(match_str[0]) + match_str = title + match_str + new_string = "" + for x in match_str: + new_string += x + " " + new_string = new_string.rstrip() + return new_string + else: + print(match_str) + + +def separateUpperString(str): + title = re.findall('[a-zA-Z][^A-Z]*', str) + return title + +def apiOneDrive(folderType=None,nameFile=None): + apiURL = f"{LC_URL}{folderType}" + rawURL = f"{LC_FILEURL}{folderType}" + counter = 1 + r = session.get(apiURL) + if r.status_code == 200: + r = r.json() + data = r["folder"]["value"] + if r["next"]: + while "next" in r: + nextLink = r["next"] + r = session.get(f"{apiURL}&next={nextLink}").json() + data.extend(r["folder"]["value"]) + fileCount = len(data) + fileFinderDB(nameFile, rawURL, data, counter) + print(f"Trovati {fileCount} file in totale!") + else: + print(r.status_code) + print(r.content) + +def fileFinderDB(nameFile, rawURL, data, counter): + for file in data: + mp4File = file["name"] + if nameFile: + if counter<10: + fileName = f"{nameFile} Ep 0{counter}" + else: + fileName = f"{nameFile} Ep {counter}" + else: + fileName = findOneDriveLinks(file["name"]) + link = f"{rawURL}{mp4File}" + counter+=1 + DBSearchAndUpdate(link,fileName) + +def writeToText(text): + with open('link_modificati.txt', 'a', encoding='utf-8') as f: + f.write(f"{text}\n") + + + +apiOneDrive("MLIF/") \ No newline at end of file