Skip to content

Commit

Permalink
add sticker
Browse files Browse the repository at this point in the history
  • Loading branch information
xdtianyu committed Sep 22, 2016
1 parent 0eab811 commit e0ed298
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sticker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cache/
.idea/
11 changes: 11 additions & 0 deletions sticker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Sticker

```shell
pip3 install -r requirements.txt
```

## Usage

```shell
./sticker.py https://store.line.me/stickershop/product/1271679/en
```
5 changes: 5 additions & 0 deletions sticker/requirements.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 86 additions & 0 deletions sticker/sticker.py
Original file line number Diff line number Diff line change
@@ -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.')



0 comments on commit e0ed298

Please sign in to comment.