LC_OneDriveMp4Links/onedriveLinks.py

107 lines
3.1 KiB
Python

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/")