Add support for Zapiex extractor

This commit is contained in:
Olivier 'reivilibre' 2021-01-13 21:31:13 +00:00
parent 6a7f408d19
commit 89d2427a57
2 changed files with 60 additions and 1 deletions

View File

@ -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())

View File

@ -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