From 75d05f8f4afab30e14b9f4e78fe0ee164e1945d1 Mon Sep 17 00:00:00 2001 From: xeals Date: Wed, 8 Jul 2020 16:10:14 +1000 Subject: [PATCH] initial commit --- fetch.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch.sh | 7 +++++++ shell.nix | 10 ++++++++++ 3 files changed, 75 insertions(+) create mode 100644 fetch.py create mode 100755 fetch.sh create mode 100644 shell.nix diff --git a/fetch.py b/fetch.py new file mode 100644 index 0000000..5e93b22 --- /dev/null +++ b/fetch.py @@ -0,0 +1,58 @@ +#!/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}') diff --git a/fetch.sh b/fetch.sh new file mode 100755 index 0000000..2a91972 --- /dev/null +++ b/fetch.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +if nix-shell --version 2>&1 >/dev/null ; then + nix-shell --command "python ./fetch.py $@" +else + python ./fetch.py "$@" +fi diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..6c8ad5c --- /dev/null +++ b/shell.nix @@ -0,0 +1,10 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + buildInputs = pkgs.lib.singleton + (pkgs.python3.withPackages (ppkgs: with ppkgs; [ + beautifulsoup4 + requests + ipython + ])); +}