diff --git a/fleabay/extractors/zapiex_api_extractor.py b/fleabay/extractors/zapiex_api_extractor.py new file mode 100644 index 0000000..68d87bc --- /dev/null +++ b/fleabay/extractors/zapiex_api_extractor.py @@ -0,0 +1,58 @@ +from typing import List + +from requests import Session + +from fleabay.definitions import ItemSummary, ItemDetails + + +class ZapiexApiExtractor: + def __init__(self, key): + self.api_key = key + self.session = Session() + + def search(self, term: str, items_per_page: int = 200, remote_lowest_first: bool = False) -> List[ItemSummary]: + headers = { + "x-api-key": self.api_key + } + params = { + "text": term, + "currency": "GBP", + "shipTo": "UK", + } + if remote_lowest_first: + params["sort"] = "LOWEST_PRICE" + + response = self.session.post("https://api.zapiex.com/v3/search", json=params, headers=headers) + + resp = response.json() + + if response.status_code == 419: + raise RuntimeError(f"Out of API credits, sadly. {response.status_code} during /search. :(") + elif response.status_code != 200: + raise RuntimeError(f"{response.status_code} during /search. :(") + + if resp["data"]["currency"] != "GBP": + raise RuntimeError(f"Requested GBP but got {resp['data']['currency']}") + + items = [] + + for item in resp["data"]["items"]: + items.append(ItemSummary( + item["productId"], + f"https://www.aliexpress.com/item/{item['productId']}.html", + item["title"], + "AE item. Min price only", + int(100 * float(item["productMinPrice"]["value"])), + int(100 * float(item["productMinPrice"]["value"])), + None, + "", + int(100 * float(item["shippingMinPrice"]["value"] or 0)), + item["imageUrl"], + dict() + )) + + return items + + @staticmethod + def details(_summary: ItemSummary) -> ItemDetails: + return ItemDetails("?", 0, "?", dict(), dict(), dict()) diff --git a/fleabay/webapp.py b/fleabay/webapp.py index c624346..3434373 100644 --- a/fleabay/webapp.py +++ b/fleabay/webapp.py @@ -9,6 +9,7 @@ from wtforms_async import Form, StringField, BooleanField, SelectField from fleabay.extractors.ebay_api_extractor import EbayApiExtractor from fleabay.definitions import ItemDetails from fleabay.extractors.ebay_raw_extractor import EbayExtractor +from fleabay.extractors.zapiex_api_extractor import ZapiexApiExtractor app = Quart(__name__) @@ -29,7 +30,7 @@ def make_extractors() -> Tuple[Dict[str, Any], Dict[str, str]]: zapiex_key = os.environ.get("ZAPIEX_KEY") if zapiex_key: - available_extractors["AE_zapiex"] = None + available_extractors["AE_zapiex"] = ZapiexApiExtractor(zapiex_key) extractor_names["AE_zapiex"] = "AliExpress (by Zapiex API — limited)" return available_extractors, extractor_names