LC_OneDriveMp4Links/onedriveLinks.py

113 lines
3.4 KiB
Python
Raw Normal View History

import re
from functools import reduce
import mysql.connector
import requests
2022-12-17 10:11:15 +00:00
import emoji
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(
2022-12-17 10:11:15 +00:00
host="lordns.ddns.net",
user="onedrivelink",
password="Dinosauroincatenato98",
database="lordb_backup"
)
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)
2022-12-17 10:11:15 +00:00
emojitype = ""
if mycursor.rowcount == 1:
emojitype = emoji.emojize(":check_mark_button:")
else:
emojitype = emoji.emojize(":cross_mark:")
print(f"{emojitype} - {value} - {link}\n")
if mycursor.rowcount == 1:
text = f"Modificato : {value}"
writeToText(text)
mydb.commit()
else:
text = f"Non Modificato : {value}"
writeToText(text)
def replaceString(string):
2022-12-17 10:11:15 +00:00
repls = (".", " "), ("mp4", ""), ("_", " "),("SUB"," "),("ITA"," "),("XX","xx"),("Full HD","")
return reduce(lambda a, kv: a.replace(*kv), repls, string)
def removeYearString(string):
pattern = r"\d{4}"
2022-12-15 16:55:38 +00:00
return re.sub(pattern, "", string)
def findOneDriveLinks(str):
str = replaceString(str)
str = removeYearString(str)
match_str = re.findall(
r"([a-zA-Z0-9]+)", str
2022-12-15 16:55:38 +00:00
)
if len(match_str)>0:
title = separateUpperString(match_str[0])
del(match_str[0])
2022-12-15 16:55:38 +00:00
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):
2022-12-15 16:55:38 +00:00
return re.findall('[a-zA-Z][^A-Z]*', str)
2022-12-15 16:55:38 +00:00
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"]
2022-12-14 22:41:34 +00:00
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)
2022-12-15 16:55:38 +00:00
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)
2022-12-15 16:55:38 +00:00
def fileFinderDB(nameFile, rawURL, data, counter,epstyle):
for file in data:
mp4File = file["name"]
if nameFile:
2022-12-15 16:55:38 +00:00
if counter<10 and epstyle:
fileName = f"{nameFile}E0{counter}"
elif counter < 10:
fileName = f"{nameFile} Ep 0{counter}"
2022-12-17 10:11:15 +00:00
elif counter>=10 and epstyle:
2022-12-15 16:55:38 +00:00
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):
2022-12-17 10:11:15 +00:00
with open('link_modificati.txt', 'a', encoding='utf-8', errors="ignore") as f:
2022-12-15 16:55:38 +00:00
f.write(f"{text}\n")
if __name__ == "__main__":
apiOneDrive("MLIF/") # Esempio