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)