forked from xdtianyu/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsticker.py
executable file
·86 lines (54 loc) · 1.74 KB
/
sticker.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
#!/usr/bin/env python3
import os
import urllib.request
from PIL import Image
import requests
from urllib3.exceptions import HTTPError
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/50.0.2661.75 Safari/537.36'
header = {'User-Agent': user_agent}
cache_dir = 'cache/'
def fetch(url, headers, cookies):
print('fetch: ' + url)
r = requests.get(url, headers=headers, cookies=cookies)
from bs4 import BeautifulSoup
import lxml
bs = BeautifulSoup(r.text, lxml.__name__)
name = bs.find('title').text.split(' -')[0]
print('name: ' + name)
import re
spans = bs.find_all('span', style=re.compile('129px;'))
for span in spans:
link = re.match('.*(https.*png)', span.get('style')).group(1)
download(name, link)
if spans:
pass
else:
print('Error html content!')
print(r.text)
def download(dir_name, url):
print('download: ' + url)
if not os.path.exists(cache_dir + dir_name):
os.makedirs(cache_dir + dir_name)
res = None
try:
res = urllib.request.urlopen(url)
except HTTPError:
print('download error.')
file_name = url.split('/')[-1]
path = cache_dir + dir_name + '/' + file_name
with open(path, 'b+w') as f:
f.write(res.read())
scale(path)
def scale(file):
print('scale: ' + file)
img = Image.open(file)
width = 512
p = (width / float(img.size[0]))
height = int((float(img.size[1]) * float(p)))
img.resize((width, height), Image.CUBIC).save(file.split('.')[0]+"_big.png")
if __name__ == '__main__':
import sys
if len(sys.argv) > 1 and sys.argv[1]:
fetch(sys.argv[1], header, {})
else:
print('Error parameter.')