Aggiunto supporto alla paginazione delle API

master
MickSlash 2022-12-14 22:11:41 +00:00
parent 70b135bb4a
commit 4e498f3c3b
1 changed files with 107 additions and 91 deletions

View File

@ -1,91 +1,107 @@
import re import re
from functools import reduce from functools import reduce
import mysql.connector import mysql.connector
import requests import requests
LC_URL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/?path=/" 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=/" LC_FILEURL = "https://onedrive-vercel-index-3lgqsp5f2-tartamafajak22.vercel.app/api/raw/?path=/"
session = requests.Session() session = requests.Session()
# CONNESSIONE AL DB # CONNESSIONE AL DB
mydb = mysql.connector.connect( mydb = mysql.connector.connect(
host="localhost", host="localhost",
user="root", user="root",
password="Lost66tmv", password="Lost66tmv",
database="lordb" database="lordb"
) )
def DBSearchAndUpdate(link,value): def DBSearchAndUpdate(link,value):
mycursor = mydb.cursor() mycursor = mydb.cursor(buffered=True)
# sql = "SELECT * FROM `tbl_episode` WHERE titolo LIKE %s" sql = "UPDATE tbl_episode SET `link embedded` = %s WHERE `titolo` LIKE %s LIMIT 1"
sql = "UPDATE tbl_episode SET `link embedded` = %s WHERE titolo LIKE %s" val = (f'{link}',f'%{value}%')
val = (f'{link}',f'%{value}%') mycursor.execute(sql,val)
mycursor.execute(sql,val) print(f"{mycursor.rowcount} - {value} - {link}")
myresult = mycursor.fetchone() if mycursor.rowcount == 1:
print(myresult) text = f"Modificato : {value}"
if mycursor.rowcount == 1: writeToText(text)
print(myresult) mydb.commit()
mydb.commit() else:
else: text = f"Non Modificato : {value}"
print("Impossibile trovare il file desiderato!") writeToText(text)
def replaceString(string): def replaceString(string):
repls = (".", " "), ("mp4", ""), ("_", " "),("SUB"," "),("ITA"," "),("XX","xx") repls = (".", " "), ("mp4", ""), ("_", " "),("SUB"," "),("ITA"," "),("XX","xx")
return reduce(lambda a, kv: a.replace(*kv), repls, string) return reduce(lambda a, kv: a.replace(*kv), repls, string)
def removeYearString(string): def removeYearString(string):
pattern = r"\d{4}" pattern = r"\d{4}"
str_data = re.sub(pattern, "", string) str_data = re.sub(pattern, "", string)
return str_data return str_data
def findOneDriveLinks(str): def findOneDriveLinks(str):
str = replaceString(str) str = replaceString(str)
str = removeYearString(str) str = removeYearString(str)
match_str = re.findall( match_str = re.findall(
r"([a-zA-Z0-9]+)", str 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 ) # 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: if len(match_str)>0:
title = separateUpperString(match_str[0]) title = separateUpperString(match_str[0])
del(match_str[0]) del(match_str[0])
match_str = title + match_str match_str = title + match_str
new_string = "" new_string = ""
for x in match_str: for x in match_str:
new_string += x + " " new_string += x + " "
new_string = new_string.rstrip() new_string = new_string.rstrip()
print(new_string) return new_string
return new_string else:
else: print(match_str)
print(match_str)
def separateUpperString(str):
def separateUpperString(str): title = re.findall('[a-zA-Z][^A-Z]*', str)
title = re.findall('[a-zA-Z][^A-Z]*', str) return title
return title
def apiOneDrive(folderType=None,nameFile=None):
def apiOneDrive(folderType=None,nameFile=None): apiURL = f"{LC_URL}{folderType}"
apiURL = f"{LC_URL}{folderType}" rawURL = f"{LC_FILEURL}{folderType}"
rawURL = f"{LC_FILEURL}{folderType}" counter = 1
r = session.get(apiURL) r = session.get(apiURL)
if r.status_code == 200: if r.status_code == 200:
data = r.json() r = r.json()
counter = 1 data = r["folder"]["value"]
fileCount = len(data["folder"]["value"]) if r["next"]:
for file in data["folder"]["value"]: while "next" in r:
mp4File = file["name"] nextLink = r["next"]
if nameFile: r = session.get(f"{apiURL}&next={nextLink}").json()
if counter<10: data.extend(r["folder"]["value"])
fileName = f"{nameFile} Ep 0{counter}" fileCount = len(data)
else: fileFinderDB(nameFile, rawURL, data, counter)
fileName = f"{nameFile} Ep {counter}" print(f"Trovati {fileCount} file in totale!")
else: else:
fileName = findOneDriveLinks(file["name"]) print(r.status_code)
link = f"{rawURL}{mp4File}" print(r.content)
counter+=1
DBSearchAndUpdate(link,fileName) def fileFinderDB(nameFile, rawURL, data, counter):
print(f"Modificati con successo {fileCount} file in totale!") for file in data:
else: mp4File = file["name"]
print(r.status_code) if nameFile:
print(r.content) 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/")