add sticker

This commit is contained in:
tianyu 2016-09-22 21:17:03 +08:00
parent 0eab8116ca
commit e0ed298860
4 changed files with 104 additions and 0 deletions

2
sticker/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
cache/
.idea/

11
sticker/README.md Normal file
View File

@ -0,0 +1,11 @@
#Sticker
```shell
pip3 install -r requirements.txt
```
## Usage
```shell
./sticker.py https://store.line.me/stickershop/product/1271679/en
```

5
sticker/requirements.txt Normal file
View File

@ -0,0 +1,5 @@
lxml == 3.5.0
requests == 2.9.1
urllib3 == 1.13.1
Pillow == 3.3.1
beautifulsoup4 == 4.5.1

86
sticker/sticker.py Executable file
View File

@ -0,0 +1,86 @@
#!/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.')