|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
|
|
|
|
|
|
from requests_futures.sessions import FuturesSession
|
|
|
|
|
|
|
|
|
|
|
|
BASE_URL = 'https://teespring.com'
|
|
|
|
CAMPAIGN_DATA = re.compile(r'var campaign_data = (\{.*\})</script>')
|
|
|
|
|
|
|
|
|
|
|
|
class Teespring(object):
|
|
|
|
def __init__(self, store_name):
|
|
|
|
self.store_name = store_name
|
|
|
|
|
|
|
|
def _parse_variants(self, pid, campaign_data):
|
|
|
|
result = []
|
|
|
|
product = campaign_data['products'][pid]
|
|
|
|
for color in product['colors']:
|
|
|
|
cid = str(color['id'])
|
|
|
|
front = [i for i in product['images'][cid] if i['side'] == 'front']
|
|
|
|
back = [i for i in product['images'][cid] if i['side'] == 'back']
|
|
|
|
result.append(dict(
|
|
|
|
color_id=cid,
|
|
|
|
front_url=front[0]['src'],
|
|
|
|
back_url=back[0]['src'],
|
|
|
|
color_value=color['value']))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def fetch_products(self):
|
|
|
|
session = FuturesSession()
|
|
|
|
def get_products(page):
|
|
|
|
url = '{0}/api/stores/{1}/store_products'.format(BASE_URL, self.store_name)
|
|
|
|
params = dict(page=page)
|
|
|
|
return session.get(url, params=params, headers={'Accept': 'application/json'})
|
|
|
|
result = []
|
|
|
|
requests = []
|
|
|
|
page = 1
|
|
|
|
while True:
|
|
|
|
request = get_products(page)
|
|
|
|
r = request.result()
|
|
|
|
if not r.ok:
|
|
|
|
return []
|
|
|
|
data = r.json()
|
|
|
|
for product in data.get('products', []):
|
|
|
|
product['url'] = BASE_URL + product['url']
|
|
|
|
requests.append(session.get(product['url']))
|
|
|
|
result.append(product)
|
|
|
|
next_url = data.get('next')
|
|
|
|
if not next_url:
|
|
|
|
break
|
|
|
|
q = parse_qs(urlparse(next_url).query)
|
|
|
|
page = q.get('page', [])[0]
|
|
|
|
for product, request in zip(result, requests):
|
|
|
|
q = parse_qs(urlparse(product['url']).query)
|
|
|
|
pid = q.get('pid', [])[0]
|
|
|
|
r = request.result()
|
|
|
|
m = CAMPAIGN_DATA.search(r.text)
|
|
|
|
if m:
|
|
|
|
campaign_data = json.loads(m.group(1))
|
|
|
|
product['variants'] = self._parse_variants(pid, campaign_data)
|
|
|
|
return result
|