-
Notifications
You must be signed in to change notification settings - Fork 1
/
giffy.py
85 lines (63 loc) · 2.62 KB
/
giffy.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
import re, os, requests, data, converter, downloader
from scraper import extract_url, check_url
from colors import Printer
websites = data.data
convert = converter.convert
download = downloader.download
# Run Printer
log = Printer()
def main(url_input, optimize_gif=False, enable_logging=True):
# Enable or disable logging
log.LOG = enable_logging
try:
log.print_info('Analyzing: ', url_input)
# Apply scheme to avoid error
if 'http' not in url_input:
url_input = 'https://' + url_input
GIF_RES = None
for one in websites:
child = websites[one]
r, idx = child['re']
# Check if url matches an existed provider
if re.match(r, url_input):
log.print_info('Provider: ', one)
log.print_info('Optimzation: ', 'ON' * optimize_gif + 'OFF' * (not optimize_gif))
# Getting ID to use as a name for the GIF
ID = re.split(r, url_input)[idx].replace('/', '-')
# If scrape is True
# We perform a given script via eval() to get media url
if child['scrape']:
log.print_info('Scraping: ', 'In Progress...')
GIF_RES = extract_url(url_input, child['scrape'])
if not GIF_RES:
return
else:
log.print_info('Scraping: ', 'Skipped')
# if not scrape, then we perform direct download
# using "op" key in data.py
GIF_RES = child['op'].format(ID)
else:
continue
# Full name with extension
name = '{}.{}'.format(ID, child['ext'])
# Check if url is connectable
if not check_url(GIF_RES):
return
# Download mp4/GIF
download(GIF_RES, name)
if child['ext'] != 'gif':
log.print_info('Converting: ', 'In Progress...')
convert(name, optimize_gif)
else:
log.print_info('Converting: ', 'Skipped')
# Clean downloaded videos after converting
if name.split('.')[-1] != 'gif':
os.remove(name)
log.print_info('Result: ', 'Success!')
return
log.print_error('Result: ','Provider is not supported :(')
return
except KeyboardInterrupt:
log.print_error('Result: ', 'Interrupted!')
if __name__ == '__main__':
main(input('URL: '), True, True)