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}"
return re.sub(pattern, "", string)
def findOneDriveLinks(str):
str = replaceString(str)
str = removeYearString(str)
match_str = re.findall(
r"([a-zA-Z0-9]+)", str
)
if len(match_str)>0:
title = separateUpperString(match_str[0])
del(match_str[0])
match_str = title + match_str
new_string = "".join(f"{x} " for x in match_str)
return new_string.rstrip()
else:
print(match_str)
def separateUpperString(str):
return re.findall('[a-zA-Z][^A-Z]*', str)
def apiOneDrive(folderType=None,nameFile=None,epstyle=True):
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 "next" in r:
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,epstyle)
print(f"Modifica provata su {fileCount} file in totale!")
mydb.close()
else:
print(r.status_code)
print(r.content)
def fileFinderDB(nameFile, rawURL, data, counter,epstyle):
for file in data:
mp4File = file["name"]
if nameFile:
if counter<10 and epstyle:
fileName = f"{nameFile}E0{counter}"
elif counter < 10:
fileName = f"{nameFile} Ep 0{counter}"
elif counter>10 and epstyle:
fileName = f"{nameFile}E{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")
if __name__ == "__main__":
apiOneDrive("MLIF/") # Esempio