Skip to content

Commit 1877bde

Browse files
committed
Added prey_wildcards parameter and filters to auction history.
1 parent ef36ad1 commit 1877bde

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Changelog
66
Due to this library relying on external content, older versions are not guaranteed to work.
77
Try to always use the latest version.
88

9+
.. v4.1.0
10+
11+
4.1.0 (2021-03-30)
12+
==================
13+
- Added ``prey_wildcards`` attribute to ``AuctionDetails``.
14+
- Added ``filters`` parameter to ``CharacterBazaar.get_auctions_history_url`` and ``Client.fetch_auction_history``.
15+
916
.. v4.0.0:
1017
1118
4.0.0 (2021-03-10)

serve.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ async def home(request: web.Request):
5353
@routes.get('/auctions')
5454
async def get_current_auctions(request: web.Request):
5555
page = int(request.query.get("page", 1))
56+
filters = await filters_from_query(request)
57+
response = await app["tibiapy"].fetch_current_auctions(page, filters)
58+
return json_response(response)
59+
60+
61+
async def filters_from_query(request):
5662
filters = tibiapy.AuctionFilters()
5763
filters.world = request.query.get("world")
5864
filters.battleye = try_enum(tibiapy.BattlEyeTypeFilter, request.query.get("battleye"))
@@ -65,14 +71,15 @@ async def get_current_auctions(request: web.Request):
6571
filters.max_skill_level = tibiapy.utils.parse_integer(request.query.get("max_skill_level"), None)
6672
filters.order_by = try_enum(tibiapy.AuctionOrderBy, request.query.get("order_by"))
6773
filters.order = try_enum(tibiapy.AuctionOrder, request.query.get("order"))
68-
filters.item = request.query.get("item")
69-
response = await app["tibiapy"].fetch_current_auctions(page, filters)
70-
return json_response(response)
74+
filters.search_string = request.query.get("item")
75+
return filters
7176

7277

7378
@routes.get('/auctions/history')
7479
async def get_auction_history(request: web.Request):
75-
response = await app["tibiapy"].fetch_auction_history()
80+
page = int(request.query.get("page", 1))
81+
filters = await filters_from_query(request)
82+
response = await app["tibiapy"].fetch_auction_history(page, filters)
7683
return json_response(response)
7784

7885

@@ -350,7 +357,7 @@ async def middleware_handler(request: web.Request):
350357
raise
351358
except Exception as e:
352359
tb = traceback.format_exc()
353-
print(tb)
360+
log.exception("%s %s", request.method, request.path)
354361
return json_error(500, e, tb)
355362

356363
return middleware_handler

tibiapy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '4.0.0'
1+
__version__ = '4.1.0'
22
__author__ = 'Allan Galarza'
33

44
import logging

tibiapy/bazaar.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,9 @@
1010
from tibiapy import abc, InvalidContent, Sex, Vocation
1111
from tibiapy.abc import BaseCharacter
1212
from tibiapy.enums import AuctionOrder, AuctionOrderBy, AuctionSearchType, AuctionStatus, BattlEyeTypeFilter, \
13-
BazaarType, BidType, \
14-
PvpTypeFilter, \
15-
SkillFilter, \
16-
VocationAuctionFilter
13+
BazaarType, BidType, PvpTypeFilter, SkillFilter, VocationAuctionFilter
1714
from tibiapy.utils import convert_line_breaks, deprecated, get_tibia_url, parse_integer, parse_pagination, \
18-
parse_tibia_datetime, \
19-
parse_tibia_money, \
20-
parse_tibiacom_content, \
21-
try_enum
15+
parse_tibia_datetime, parse_tibiacom_content, try_enum
2216

2317
__all__ = (
2418
"AchievementEntry",
@@ -365,15 +359,23 @@ def get_current_auctions_url(cls, page=1, filters=None):
365359
return get_tibia_url("charactertrade", "currentcharactertrades", currentpage=page, **filters.query_params)
366360

367361
@classmethod
368-
def get_auctions_history_url(cls, page=1):
362+
def get_auctions_history_url(cls, page=1, filters=None):
369363
"""Gets the URL to the auction history in Tibia.com
370364
365+
Parameters
366+
----------
367+
page: :class:`int`
368+
The page to show the URL for.
369+
filters: :class:`AuctionFilters`
370+
The filtering criteria to use.
371+
371372
Returns
372373
-------
373374
:class:`str`
374375
The URL to the auction history section in Tibia.com
375376
"""
376-
return get_tibia_url("charactertrade", "pastcharactertrades", currentpage=page)
377+
filters = filters or AuctionFilters()
378+
return get_tibia_url("charactertrade", "pastcharactertrades", currentpage=page, **filters.query_params)
377379

378380
@classmethod
379381
def from_content(cls, content):
@@ -876,6 +878,8 @@ class AuctionDetails(ListedAuction):
876878
The amount of charm points the character has available to spend.
877879
spent_charm_points: :class:`int`
878880
The total charm points the character has spent.
881+
prey_wildcards: :class:`int`
882+
The number of Prey Wildcards the character has.
879883
daly_reward_streak: :class:`int`
880884
The current daily reward streak.
881885
permanent_hunting_task_slots: :class:`int`
@@ -941,6 +945,7 @@ def __init__(self, **kwargs):
941945
self.spent_charm_points: int = kwargs.get("spent_charm_points", 0)
942946
self.daily_reward_streak: int = kwargs.get("daily_reward_streak", 0)
943947
self.hunting_task_points: int = kwargs.get("hunting_task_points", 0)
948+
self.prey_wildcards: int = kwargs.get("prey_wildcards", 0)
944949
self.permanent_hunting_task_slots: int = kwargs.get("permanent_hunting_task_slots", 0)
945950
self.permanent_prey_slots: int = kwargs.get("permanent_prey_slots", 0)
946951
self.hirelings: int = kwargs.get("hirelings", 0)
@@ -983,6 +988,7 @@ def __init__(self, **kwargs):
983988
"hunting_task_points",
984989
"permanent_hunting_task_slots",
985990
"permanent_prey_slots",
991+
"prey_wildcards",
986992
"hirelings",
987993
"hireling_jobs",
988994
"hireling_outfits",
@@ -1316,6 +1322,7 @@ def _parse_general_table(self, table):
13161322
self.hunting_task_points = parse_integer(hunting_data.get("hunting_task_points", ""))
13171323
self.permanent_hunting_task_slots = parse_integer(hunting_data.get("permanent_hunting_task_slots", ""))
13181324
self.permanent_prey_slots = parse_integer(hunting_data.get("permanent_prey_slots", ""))
1325+
self.prey_wildcards = parse_integer(hunting_data.get("prey_wildcards", ""))
13191326

13201327
hirelings_data = self._parse_data_table(content_containers[7])
13211328
self.hirelings = parse_integer(hirelings_data.get("hirelings", ""))

tibiapy/client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ async def _initialize_session(self, proxy_url=None):
145145
self._session_ready.set()
146146

147147
@classmethod
148-
def _handle_status(cls, status_code):
148+
def _handle_status(cls, status_code, fetching_time=0):
149149
"""Handles error status codes, raising exceptions if necessary."""
150150
if status_code < 400:
151151
return
152152
if status_code == 403:
153-
raise Forbidden("403 Forbidden: Might be getting rate-limited")
153+
raise Forbidden("403 Forbidden: Might be getting rate-limited", fetching_time=fetching_time)
154154
else:
155-
raise NetworkError("Request error, status code: %d" % status_code)
155+
raise NetworkError("Request error, status code: %d" % status_code, fetching_time=fetching_time)
156156

157157
async def _request(self, method, url, data=None, headers=None):
158158
"""Base request, handling possible error statuses.
@@ -182,24 +182,24 @@ async def _request(self, method, url, data=None, headers=None):
182182
If there's any connection errors during the request.
183183
"""
184184
await self._session_ready.wait()
185+
init_time = time.perf_counter()
185186
try:
186-
init_time = time.perf_counter()
187187
async with self.session.request(method, url, data=data, headers=headers) as resp:
188188
diff_time = time.perf_counter()-init_time
189189
if "maintenance.tibia.com" in str(resp.url):
190190
log.info(f"%s | %s | %s %s | maintenance.tibia.com", url, resp.method, resp.status, resp.reason)
191191
raise SiteMaintenanceError("Tibia.com is down for maintenance.")
192192
log.info(f"%s | %s | %s %s | %dms", url, resp.method, resp.status, resp.reason, int(diff_time*1000))
193-
self._handle_status(resp.status)
193+
self._handle_status(resp.status, diff_time)
194194
response = RawResponse(resp, diff_time)
195195
response.content = await resp.text()
196196
return response
197197
except aiohttp.ClientError as e:
198-
raise NetworkError("aiohttp.ClientError: %s" % e, e)
198+
raise NetworkError("aiohttp.ClientError: %s" % e, e, time.perf_counter()-init_time)
199199
except aiohttp_socks.SocksConnectionError as e:
200-
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e)
200+
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e, time.perf_counter()-init_time)
201201
except UnicodeDecodeError as e:
202-
raise NetworkError('UnicodeDecodeError: %s' % e, e)
202+
raise NetworkError('UnicodeDecodeError: %s' % e, e, time.perf_counter()-init_time)
203203

204204
async def fetch_current_auctions(self, page=1, filters=None):
205205
"""Fetches the current auctions in the bazaar
@@ -236,7 +236,7 @@ async def fetch_current_auctions(self, page=1, filters=None):
236236
parsing_time = time.perf_counter() - start_time
237237
return TibiaResponse(response, current_auctions, parsing_time)
238238

239-
async def fetch_auction_history(self, page=1):
239+
async def fetch_auction_history(self, page=1, filters=None):
240240
"""Fetches the auction history of the bazaar.
241241
242242
.. versionadded:: 3.3.0
@@ -245,6 +245,8 @@ async def fetch_auction_history(self, page=1):
245245
----------
246246
page: :class:`int`
247247
The page to display.
248+
filters: :class:`AuctionFilters`
249+
The filtering criteria to use.
248250
249251
Returns
250252
-------
@@ -263,7 +265,7 @@ async def fetch_auction_history(self, page=1):
263265
"""
264266
if page <= 0:
265267
raise ValueError('page must be 1 or greater.')
266-
response = await self._request("GET", CharacterBazaar.get_auctions_history_url(page))
268+
response = await self._request("GET", CharacterBazaar.get_auctions_history_url(page, filters))
267269
start_time = time.perf_counter()
268270
auction_history = CharacterBazaar.from_content(response.content)
269271
parsing_time = time.perf_counter() - start_time

tibiapy/errors.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class NetworkError(TibiapyException):
3030
Attributes
3131
----------
3232
original: :class:`Exception`
33-
The original exception that caused this exception."""
34-
def __init__(self, message, original=None):
33+
The original exception that caused this exception.
34+
fetching_time: :class:`float`
35+
The time between the request and the response.
36+
"""
37+
def __init__(self, message, original=None, fetching_time=0):
3538
super().__init__(message)
3639
self.original = original
40+
self.fetching_time = fetching_time
3741

3842

3943
class Forbidden(NetworkError):

0 commit comments

Comments
 (0)