-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (106 loc) · 4.15 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
import datetime
import json
import os
import platform
import sys
from os import rename
from pathlib import Path
import filetype
import requests
from pygifsicle import optimize
import convert
from convert import convertFile
try:
from winreg import *
except:
pass
def checkifexist(folder, name):
return Path(folder + '/' + name).exists()
def downloadurl(url, count, name, folder):
name += '.gif' if not '.gif' in name else ''
if checkifexist(folder, name):
name = str(count) + '_duplicate_' + name
if url.split('.')[-1] != 'gif':
url += '.gif'
r = requests.get(url)
print("\t status (", r.status_code, ')')
if r.status_code in range(200, 300):
file = open(folder + '/' + name, "wb")
file.write(r.content)
file.close()
print(f'\t downloaded {name} in {folder}')
return 1
return 0
def downloadsrc(src, count, name, folder):
if checkifexist(folder, name):
name = str(count) + '_duplicate_' + name
r = requests.get(src, allow_redirects=True)
print("\t status (", r.status_code, ')')
if r.status_code in range(200, 300):
file = open(folder + '/' + name, "wb")
file.write(r.content)
file.close()
print(f'\t downloaded {name} in {folder}')
return 1
return 0
def checkfolder(path):
from os import walk
_, _, filenames = next(walk(path))
for file in filenames:
fullpath = path + '/' + file
guess = filetype.guess(fullpath)
if guess is None:
print(f'\033[93m\033[1mcouldn\'t process {path}/{file} you ended up with a binary file')
elif file[len(file) - len(guess.extension) - 1:] != '.' + guess.extension:
rename(fullpath, fullpath + '.' + guess.extension)
if not guess.mime.split('/')[0] == 'video':
print(f'renamed {path}/{file} ➡ {path}/{file}.{guess.extension}')
else:
convertFile(f'{fullpath}.{guess.extension}', convert.TargetFormat.GIF)
if os.path.getsize(f'{fullpath}.{guess.extension}') < os.path.getsize(f'{fullpath}.gif'):
print(f'optimizing {fullpath}.gif')
optimize(f'{fullpath}.gif')
os.remove(f'{fullpath}.{guess.extension}')
def finddownloaddirectory():
folder = None
if len(sys.argv) == 2:
folder = sys.argv[1]
if platform.system() == 'Windows':
with OpenKey(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') as key:
folder = QueryValueEx(key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
else:
folder = str(Path.home()) + '/Downloads'
if folder:
return folder
else:
raise NameError('Path do HOME Download folder not found')
def main(giffavs, folder):
errorlist = []
value = json.loads(giffavs)["_state"]["favorites"]
print(f'found {len(value)} faved gifs')
for count, el in enumerate(value):
url = str(el["url"])
src = (el["src"])
print(f'\n➡{count + 1}: {url}')
name = url.split('/')[-1].split('?')[-2] if '?' in url.split('/')[-1] else url.split('/')[-1]
if not downloadurl(url, count, name, folder):
print(
f'\t\033[93m\033[1mCouldn\'t download {name}\n\ttrying src method but you might end up with a non gif file :\033[0m')
if not downloadsrc(src, count, name, folder):
print(
f'\t\033[91m\033[1mAn error occurred while downloading {name} try manually at source:\033[0m {el["src"]}')
errorlist.append(el["src"])
if len(errorlist) > 0:
checkfolder(folder)
return errorlist
if __name__ == '__main__':
now = datetime.datetime.now()
# put your JSON GIFFavorite store here
data = ''
#
folder = finddownloaddirectory()
folder += '/DiscordFavoriteGif(' + now.strftime("%d") + '.' + now.strftime("%b") + '.' + now.strftime(
"%Y") + '_' + now.strftime("%H") + now.strftime("%M") + now.strftime("%S") + ')'
Path(folder).mkdir(parents=True, exist_ok=True)
result = main(data, folder)
print('script ended with ' + str(len(result)) + ' error(s)')