line-stickers/fetch.py

59 lines
1.3 KiB
Python
Raw Permalink Normal View History

2020-07-08 16:10:14 +10:00
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import json
import os
import requests
import sys
args = sys.argv[1:]
pack = args[0]
print(f'fetching pack {pack}')
try:
pack = int(pack)
packurl = f'https://store.line.me/stickershop/product/{pack}/en'
except ValueError:
packurl = pack
try:
res = requests.get(packurl)
except:
print('> failed:')
print(res.contenf)
sys.exit(1)
soup = BeautifulSoup(res.text, 'html.parser')
title = soup.title.text.replace(' LINE stickers | LINE STORE','')
author = soup.find('a', class_='mdCMN38Item01Author')
print(f'> {title}')
print(f'> {author.text}')
os.mkdir(title)
stickers = soup.find_all('li', class_='mdCMN09Li')
def progress(cur, total, fname):
if cur == total:
end = '\n'
else:
end = ''
print(f'\r[{cur}/{total}] {fname}', end=end)
print(f'fetching {len(stickers)} stickers')
total = len(stickers)
for i, sticker in enumerate(stickers):
j = json.loads(sticker['data-preview'])
id = j['id']
url = j['staticUrl'].replace(';compress=true','')
progress(i+1, total, id)
img = requests.get(url)
if img.ok:
fname = id + '.png'
out = os.path.join(title, fname)
with open(out, 'wb') as f:
f.write(img.content)
else:
print('>> failed: {img.content}')