forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_thumbnail.py
91 lines (84 loc) · 2.41 KB
/
get_thumbnail.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
import requests
import base64
import sys
import config
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
import urllib.parse
import time
from ieddit import db
from models import Post
def add_remote_image(url, tid):
p = db.session.query(Post).filter_by(id=tid).first()
p.remote_image_url = url
db.session.add(p)
db.session.commit()
print('post has remote image %s %s', url, tid)
def create_thumbnail(r, tid):
#r.raw.decode_content = rue
b = BytesIO(r.content)
im = Image.open(b)
size = 128, 128
im.thumbnail(size)
#im.save('thumbnails/' + str(tid) + '.JPEG', 'JPEG')
im.save('static/thumbnails/thumb-' + str(tid) + '.PNG', 'PNG')
#r = requests.get('http://127.0.0.1/clear_cache')
#print(r.text)
def main():
c = False
tid = int(sys.argv[1])
url = urllib.parse.unquote(sys.argv[2])
#url = 'https://i.redd.it/8ex7on41uzq31.png'
#tid = 1
r = requests.get(url, proxies=config.PROXIES, allow_redirects=True)
if r.headers['Content-Type'].split('/')[0] == 'image':
#ext = r.headers['Content-Type'].split('/')[1]
create_thumbnail(r, tid)
add_remote_image(url, tid)
c = True
else:
soup = BeautifulSoup(r.text)
image = soup.find('meta', property='og:image')
try:
iurl = image.get('content', None)
r = requests.get(iurl, proxies=config.PROXIES, allow_redirects=True)
soup = BeautifulSoup(r.text)
if r.headers['Content-Type'].split('/')[0] == 'image':
#ext = r.headers['Content-Type'].split('/')[1]
create_thumbnail(r, tid)
add_remote_image(iurl, tid)
except:
try:
raise Exception
#icon_link = soup.find("link", rel="shortcut icon")
#r = requests.get(icon_link['href'], proxies=config.PROXIES, allow_redirects=True)
#soup = BeautifulSoup(r.text)
#create_thumbnail(r, tid)
except:
i = soup.findall('img')
guess = 0
src = ''
limit = 0
for im in i:
try:
limit += 1
if limit > 15:
break
try:
height = int(im.attrs.get('height', None))
width = int(im.attrs.get('width', None))
except:
height=1
width=1
isrc = im.attrs.get('src', None)
if (height * width) > guess:
src = isrc
guess = height * width
except:
pass
if src != '':
r = requests.get(i['href'], proxies=config.PROXIES)
create_thumbnail(r, tid)
if __name__ == '__main__':
main()