/
animedl.py
118 lines (105 loc) · 3.38 KB
/
animedl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from ast import arg
import urllib.request
from webbrowser import open as wbopen
from urllib.error import HTTPError as HTTPError
from time import sleep
import os
import argparse
import sqlite3
import ssl
def findAnimeLinkNameLength(animeName, index):
i=0
while True:
if(animeName[index+i].isnumeric() == False):
return i
i+=1
def downloadIsNotCompleted():
for fname in os.listdir("C:\\Users\\manuf\\Downloads"):
if fname.endswith(".part") or fname.endswith(".crdownload"):
return 1
return 0
def findEpNum(animeName):
cur.execute("SELECT num FROM anime WHERE name=?", (animeName,))
return int(cur.fetchall()[0][0])
def animeInsert(name):
num=input("Inserire il numero dell'ultimo episodio scaricato\n")
link=input("Inserire il link\n")
cur.execute("INSERT INTO anime (name, num, link) VALUES (?, ?, ?)", (name, num, link))
db.commit()
db=sqlite3.connect("C:\\Users\\manuf\\anime.db")
cur=db.cursor()
parser=argparse.ArgumentParser()
parser.add_argument('animeName', type=str, help='Nome dell\'anime')
parser.add_argument('epNumber', type=int, help='Numero di episodi')
args=parser.parse_args()
flagDownloadEpNum = False
flagUpdateEpNum = False
if(args.animeName[0] == '.'):
flagDownloadEpNum = True
args.animeName = str(args.animeName[1:])
elif(args.animeName[0] == '!'):
flagDownloadEpNum = True
flagUpdateEpNum = True
args.animeName = str(args.animeName[1:])
animeName=args.animeName
numEp=args.epNumber
animeName=animeName.lower()
try:
cur.execute("SELECT link FROM anime WHERE name=?", (animeName,))
string=cur.fetchall()[0][0]
except IndexError:
print("Anime non presente nel database")
answer=input("Si desidera inserirlo? Y/n ")
if answer.lower()=="y":
animeInsert(animeName)
quit()
epNum=findEpNum(animeName)
if flagDownloadEpNum:
epNum = numEp
index=string.find("Ep_")+3
animeLinkNameLenght=findAnimeLinkNameLength(string, index)
epNumStr=str(epNum)
if(len(epNumStr)==1):
epNumStr='0'+epNumStr
string=string[:index] + epNumStr + string[index+animeLinkNameLenght:]
try:
context = ssl._create_unverified_context()
urllib.request.urlopen(string, context=context)
wbopen(string)
sleep(5)
if flagUpdateEpNum:
cur.execute("UPDATE anime SET num=? WHERE name=?", (epNum, animeName))
db.commit()
except HTTPError:
print("Episodio non esistente o link anime modificato")
pass
else:
while(downloadIsNotCompleted()):
pass
print(str(animeName)+"-"+str(epNum), "scaricato correttamente")
else:
desEpNum=epNum+numEp
index=string.find("Ep_")+3
animeLinkNameLenght=findAnimeLinkNameLength(string, index)
while(epNum<desEpNum):
epNum+=1
epNumStr=str(epNum)
if(len(epNumStr)==1):
epNumStr='0'+epNumStr
string=string[:index] + epNumStr + string[index+animeLinkNameLenght:]
try:
context = ssl._create_unverified_context()
urllib.request.urlopen(string, context=context)
wbopen(string)
sleep(5)
cur.execute("UPDATE anime SET num=? WHERE name=?", (epNum, animeName))
db.commit()
except HTTPError:
print("Episodio non esistente o link anime modificato")
pass
else:
while(downloadIsNotCompleted()):
pass
print(str(animeName)+"-"+str(epNum), "scaricato correttamente")
if db:
db.close()