-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
287 lines (278 loc) · 11.2 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Necro(ネクロ)
# sidmishra94540@gmail.com
import os, requests, time, webbrowser, PIL.Image
def anilist(type, data):
try:
if type == 'DETAILS':
variables = {
'id': data.get('part')
}
query = '''
query ($id: Int) {
Media (id: $id, type: ANIME) {
id
title {
english
romaji
}
coverImage{
extraLarge
}
}
}
'''
response = requests.post('https://graphql.anilist.co', json={'query': query, 'variables': variables}).json()['data']['Media']
if type == 'RELATIONS':
variables = {
'id': data.get('current')
}
query = '''
query ($id: Int) {
Media (id: $id, type: ANIME) {
relations{
edges{
node{
id
type
}
relationType
}
}
}
}
'''
response = requests.post('https://graphql.anilist.co', json={'query': query, 'variables': variables}).json()['data']['Media']['relations']['edges']
if type == 'LISTS':
variables = {
'userId': data.get('uid'),
'type': data.get('type')
}
query = '''
query ($userId: Int, $type: MediaType){
MediaListCollection (userId: $userId, type: $type){
lists{
name
status
entries{
mediaId
}
}
}
}
'''
response = requests.post('https://graphql.anilist.co', json={'query': query, 'variables': variables}).json()['data']['MediaListCollection']['lists']
except:
return None
return response
def isValid(name):
if name in ['!ndex.txt', '!restrict.txt', 'desktop.ini', '!con.ico', '$RECYCLE.BIN', 'Config.Msi', 'desktop.ini', 'msdownld.tmp', 'System Volume Information']:
return False
return True
def create(part, path):
os.mkdir(path+'/'+part)
response = anilist('DETAILS', {'part': part})
if not response:
print('Anime not found!')
title = response['title']['english'] if response['title']['english'] else response['title']['romaji']
title = title.replace('&', 'and').replace('/', '~').replace(':', '~').replace('*', '~').replace('?', '~').replace('"', '~').replace('<', '~').replace('>', '~').replace('|', '~')
cover = response['coverImage']['extraLarge']
image = path + '/' + part + '/!con' + os.path.splitext(cover)[1]
open(image, 'wb').write(requests.get(cover).content)
icon = PIL.Image.open(image)
width, height = icon.size
crop = width if width <= height else height
icon = icon.crop(((width - crop) // 2, (height - crop) // 2, (width + crop) // 2, (height + crop) // 2)).resize((256,256))
icon.save(os.path.splitext(image)[0] + '.ico', format = 'ICO', sizes=[(256,256)], quality=95)
os.remove(image)
index = open(path + '/' + part + '/!ndex.txt', 'w')
index.write(part)
index.close()
os.system(path[0] + ': & cd ' + path + ' & attrib +s "' + part + '" & cd ' + part + ' & echo [.ShellClassInfo] > desktop.ini & echo IconResource=!con.ico,0 >> desktop.ini & attrib +s +h desktop.ini & attrib +h !con.ico & attrib +h !ndex.txt')
os.rename(path + '/' + part, path + '/' + title)
print('Added-', title)
time.sleep(1)
return title
def add(path):
index = open(path + '/!ndex.txt', 'r')
data = index.read().strip()
matched = []
pres = []
counter = 0
add, subtract = [], []
res = open(path + '/!restrict.txt', 'a+')
res.close()
restrict = open(path + '/!restrict.txt', 'r')
restrictions = list(map(int, restrict.read().strip().split(',')[:-1]))
os.system(path[0] + ': & cd ' + path + ' & attrib +h !restrict.txt')
restrict.close()
for part in os.listdir(path):
if not isValid(part):
continue
index = open(path + '/' + part + '/!ndex.txt', 'r')
newdata = int(index.read().strip())
index.close()
pres.append(newdata)
index.close()
if int(data) not in pres:
pres.append(int(data))
add.append(int(data))
print('.')
counter += 1
matched = [int(data)]
for current in matched:
response = anilist('RELATIONS', {'current': current})
rln = map(lambda x: x['node']['id'], filter(lambda x: x['node']['type']!='MANGA', response))
for node in rln:
if node not in matched and node not in restrictions:
if node in pres:
print('.')
counter += 1
matched.append(node)
continue
webbrowser.open('https://anilist.co/anime/'+str(node), new=2)
choice = input('Add '+str(node)+'? [Yes(y)/No(n)]')
if choice in ['n','N']:
subtract.append(str(node))
else:
add.append(node)
print('.')
counter += 1
matched.append(node)
time.sleep(1)
if add:
for part in add:
create(str(part), path)
if subtract:
os.system(path[0] + ': & cd ' + path + ' & attrib -h !restrict.txt')
index = open(path + '/' + '!restrict.txt', 'a+')
restrictions = ','.join(subtract)+','
index.write(restrictions)
index.close()
os.system(path[0] + ': & cd ' + path + ' & attrib +h !restrict.txt')
print(len(add), '- new anime added!')
def new(path):
print()
link = input('Enter link of the anime page (anilist.co): ')
anime_id = link.split('/')[4]
title = create(anime_id, path)
add(path+'/'+title)
def update(path):
cnt = 1
for part in os.listdir(path):
if not isValid(part):
continue
print()
print(str(cnt)+'.', part)
add(path+'/'+part)
cnt += 1
print()
choice = input('Update folder icons and names too? [Yes(y)/No(n)]')
if choice in ['n', 'N']:
return
for anime in os.listdir(path):
if not isValid(anime):
continue
for part in os.listdir(path+'/'+anime):
if not isValid(part):
continue
index = open(path+'/'+anime+'/'+part+'/!ndex.txt', 'r')
data = index.read().strip()
index.close()
response = anilist('DETAILS', {'part': data})
if not response:
print('Anime not found on anilist! Please delete folder', part)
continue
title = response['title']['english'] if response['title']['english'] else response['title']['romaji']
title = title.replace('&', 'and').replace('/', '~').replace(':', '~').replace('*', '~').replace('?', '~').replace('"', '~').replace('<', '~').replace('>', '~').replace('|', '~')
cover = response['coverImage']['extraLarge']
image = path + '/' + anime + '/' + part + '/!con' + os.path.splitext(cover)[1]
os.system(path[0] + ': & cd ' + path + '/' + anime + '/' + part + ' & attrib -h !con.ico')
open(image, 'wb').write(requests.get(cover).content)
icon = PIL.Image.open(image)
width, height = icon.size
crop = width if width <= height else height
icon = icon.crop(((width - crop) // 2, (height - crop) // 2, (width + crop) // 2, (height + crop) // 2)).resize((256,256))
os.remove(path+'/'+anime+'/'+part+'/!con.ico')
icon.save(os.path .splitext(image)[0] + '.ico', format = 'ICO', sizes=[(256,256)], quality=95)
os.remove(image)
os.system(path[0] + ': & cd ' + path + '/' + anime + '/' + part + ' & attrib +h !con.ico')
os.rename(path + '/' + anime + '/' + part, path + '/' + anime + '/' + title)
print('Updated-', title)
time.sleep(1)
print('If the changes to folder icons are not visible, restart the system.')
def sync(path):
uid = int(input('Enter user-id: '))
response = anilist('LISTS', {'uid': uid, 'type': 'ANIME'})
if not response:
print('User not found!')
completed = set()
for list in response:
if list.get('status') in ['COMPLETED', 'WATCHING']:
for entry in list.get('entries'):
completed.add(entry.get('mediaId'))
unformatted = []
present = set()
for anime in os.listdir(path):
if not isValid(anime):
continue
if not os.path.isfile(path+'/'+anime+'/!ndex.txt'):
unformatted.append(anime)
try:
for part in os.listdir(path+'/'+anime):
if part in ['!ndex.txt', '!restrict.txt', 'desktop.ini', '!con.ico']:
continue
try:
index = open(path+'/'+anime+'/'+part+'/!ndex.txt', 'r')
data = index.read().strip()
present.add(int(data))
index.close()
except:
unformatted.append(anime+'/'+part)
except:
continue
comp_not, pres_not = [], []
for comp in completed:
if comp not in present:
comp_not.append(comp)
for pres in present:
if pres not in completed:
pres_not.append(pres)
while True:
print()
print('1. Anime in anilist but not in folder:', len(comp_not))
print('2. Anime in folder but not in anilist:', len(pres_not))
print('3. Unformatted folders:', len(unformatted))
print('4. Exit to Main Menu')
choice = input('Enter the respective number to view full list: ')
if choice in ['1', '2']:
to_be_printed = comp_not if choice=='1' else pres_not
for part in to_be_printed:
response = anilist('DETAILS', {'part': part})
title = response['title']['english'] if response['title']['english'] else response['title']['romaji']
print(title)
time.sleep(1)
elif choice == '3':
for part in unformatted:
print(part)
else:
return
try:
if __name__ == '__main__':
ANIME_PATH = input('Enter Anime Path: ').strip('\\').strip('/')
while True:
print()
print('1. New')
print('2. Update')
print('3. Sync')
print('4. Exit')
choice = input()
if choice == '1':
new(ANIME_PATH)
elif choice == '2':
update(ANIME_PATH)
elif choice == '3':
sync(ANIME_PATH)
else:
break
except Exception as e:
print(e)