forked from vlourme/yahoo-auction-alert-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoo.py
56 lines (45 loc) · 1.86 KB
/
yahoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from dataclasses import dataclass
import math
from logging import error
from hikari import Color
import math
from storechecker import AlertChecker, AbstractItem
@dataclass
class YahooAuctionItem(AbstractItem):
id: str
stock: int = 1
price: int = 0
buyout_price: int = 0
title: str = ''
image_url: str = ''
@property
def url(self) -> str:
return f"https://buyee.jp/item/yahoo/auction/{self.id}"
class YahooAuctionsChecker(AlertChecker):
def get_embed_color(self) -> Color:
return Color(0xFFA500) # Orange
async def fetch_items(self, session) -> list:
async def fetch(url):
async with session.get(url, headers=self.headers) as response:
return await response.json()
items_per_page = 99
content = await fetch(f"https://www.fromjapan.co.jp/japan/sites/yahooauction/search?keyword={self.search_query}&sort=score&hits={items_per_page}&page=1")
if 'items' not in content:
error(f"Failed to fetch items for {self.search_query}")
error(content)
if not content.get("items"):
return []
page_count = math.ceil(content["count"] / items_per_page)
if page_count > 1:
for page in range(2, page_count + 1):
page_content = await fetch(f"https://www.fromjapan.co.jp/japan/sites/yahooauction/search?keyword={self.search_query}&sort=score&hits={items_per_page}&page={page}")
content["items"].extend(page_content["items"])
return content["items"]
async def normalize_item(self, data: dict) -> AbstractItem:
return YahooAuctionItem(
id=data.get('id'),
image_url=data.get('imageUrl'),
title=data.get('title', 'Unknown Title'),
price=data.get('price'),
buyout_price=data.get('buyItNowPrice'),
)