-
Notifications
You must be signed in to change notification settings - Fork 10
/
anicon.py
191 lines (153 loc) · 6.48 KB
/
anicon.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
import os
import re
import traceback
from warnings import filterwarnings
from PIL import Image, ImageOps
from jikanpy import Jikan
# noinspection PyPackageRequirements
from mal import AnimeSearch
from requests import get
filterwarnings("ignore")
jikan = Jikan()
def get_name(folder_name: str) -> str:
last_words = ['bd', 's0', '480p', '720p', '1080p']
words_to_remove = ['bluray', 'x265', 'x264', 'hevc', 'hi10p', 'avc', '10bit', 'dual', 'audio', 'eng', 'english',
'subbed', 'sub', 'dubbed', 'dub']
folder_name = folder_name.lower().replace('_', ' ').replace('.', ' ')
for word in words_to_remove:
folder_name = folder_name.replace(word, '')
folder_name = re.sub(r"(?<=\[)(.*?)(?=])", '', folder_name)
folder_name = re.sub(r"(?<=\()(.*?)(?=\))", '', folder_name)
folder_name = folder_name.replace('()', '').replace('[]', '')
for word in last_words:
regex_str = "(?<=" + word + ")(?s)(.*$)"
folder_name = re.sub(regex_str, '', folder_name).replace(word, '')
return folder_name.strip()
def get_artwork(anime_name: str, max_results: int = 5, mode: str = "mal-api") -> tuple:
print('\n' + anime_name.title())
counter, choice = 1, 0
if mode == "mal-api":
results = AnimeSearch(anime_name).results
for result in results:
if auto_mode:
print(' - ' + result.title)
choice = 1
break
else:
print(str(counter) + ' - ' + result.title)
if counter == max_results:
break
counter += 1
elif mode == "jikanpy":
results = jikan.search('anime', anime_name, parameters={'type': 'tv'})
for result in results['results']:
if auto_mode:
print(' - ' + result['title'])
choice = 1
break
else:
print(str(counter) + ' - ' + result['title'])
if counter == max_results:
break
counter += 1
else:
raise Exception("Invalid mode specified")
print("X - Skip this folder")
if not auto_mode:
choice = input('> ')
if choice == '':
choice = 1
elif choice.upper() == "X":
return None, None
choice = int(choice) - 1
image_url = results['results'][choice]['image_url'] if mode == "jikanpy" else results[choice].image_url
image_type = results['results'][choice]['type'] if mode == "jikanpy" else results[choice].type
return image_url, image_type
def create_icon(img_link: str):
art = get(img_link)
open(jpg_file, 'wb').write(art.content)
img = Image.open(jpg_file)
img = ImageOps.expand(img, (69, 0, 69, 0), fill=0)
img = ImageOps.fit(img, (300, 300)).convert("RGBA")
datas = img.getdata()
new_data = []
for item in datas:
if item[0] == 0 and item[1] == 0 and item[2] == 0:
new_data.append((0, 0, 0, 0))
else:
new_data.append(item)
img.putdata(new_data)
os.remove(jpg_file)
img.save(ico_file)
img.close()
return ico_file
if __name__ == "__main__":
print("""\
Run this in your anime folder
For help, info and memes, check out
https://github.com/notdedsec/anicon
""")
auto_mode = True if input('Use AutoMode? Y/N : ').upper() == 'Y' else False
max_res = input("Max Results (default 5): ")
try:
max_res = int(max_res)
except ValueError:
max_res = 5
lib_mode = input("""\
Image Source Library:
1. mal-api (default)
2. Jikanpy
> """)
if lib_mode == "2":
lib_mode = "jikanpy"
else:
lib_mode = "mal-api"
folder_list = next(os.walk('.'))[1]
if folder_list is None or len(folder_list) == 0:
# In case the file is placed inside an innermost directory which contains only files and no other folders,
# this list will be empty. Thus adding the current directory path as an element of the list.
folder_list = [str(os.getcwd())]
for folder in folder_list:
name = get_name(folder)
# Extracting the name of the folder without the path and then performing search for the same.
# This will be the name of the anime episode, thus instead of performing a search for the directory path,
# now performing a search for the directory name.
name = name.rpartition('\\')[2].strip()
icon_name = re.sub("[^A-Za-z0-9_,. ()-]", "_", name)
jpg_file = folder + '\\' + icon_name + '.jpg'
ico_file = folder + '\\' + icon_name + '.ico'
if os.path.isfile(folder + "\\" + "desktop.ini"):
print('An icon is already present. Delete the older icon and `desktop.ini` file before applying a new icon')
continue
link, artwork_type = get_artwork(name, max_results=max_res, mode=lib_mode)
if not link or not artwork_type:
print("Skipping this folder...")
continue
try:
icon = create_icon(link)
except Exception as e:
print('Ran into an error while creating the icon object. Blame the dev :(')
for line in traceback.format_exception(None, e, e.__traceback__):
print(line, end="")
continue
try:
f = open(folder + "\\desktop.ini", "w+")
f.write("[.ShellClassInfo]\nConfirmFileOp=0\n")
f.write("IconResource={},0".format(ico_file.replace(folder, "").strip("\\")))
f.write("\nIconFile={}\nIconIndex=0".format(ico_file.replace(folder, "").strip("\\")))
if artwork_type is not None and len(artwork_type) > 0:
# If the result has a type, then using this as the info-tip for the desktop icon.
f.write("\nInfoTip={}".format(artwork_type))
# Closing the output stream.
# All the text will be written into `desktop.ini` file only when the output is being closed.
f.close()
# Not marking the `desktop.ini` file as a system file.
# This will make sure that the file can be seen if display hidden items is enabled.
os.system('attrib +r \"{}\\{}\"'.format(os.getcwd(), folder))
os.system('attrib +h \"{}\\desktop.ini\"'.format(folder))
os.system('attrib +h \"{}\"'.format(icon))
except Exception as e:
print('Ran into an error while creating files. Blame the dev :(')
for line in traceback.format_exception(None, e, e.__traceback__):
print(line, end="")
continue