You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
import json
|
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
|
|
from requests_futures.sessions import FuturesSession
|
|
|
|
|
|
BASE_URL = 'https://teespring.com'
|
|
|
|
|
|
class Teespring(object):
|
|
def __init__(self, store_name):
|
|
self.store_name = store_name
|
|
|
|
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 = []
|
|
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']
|
|
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]
|
|
return result
|