Skip to content

Commit 3d519d6

Browse files
committed
Support for the no tournaments page
- Replaced all URL constants with get_tibia_url method
1 parent c20656f commit 3d519d6

File tree

12 files changed

+70
-42
lines changed

12 files changed

+70
-42
lines changed

serve.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ async def home(request):
2121
return web.Response(text=content, content_type='text/html')
2222

2323

24-
@routes.get('/character/{name}')
24+
@routes.get('/characters/{name}')
2525
async def get_character(request):
2626
name = request.match_info['name']
2727
char = await app["tibiapy"].fetch_character(name)
2828
return web.Response(text=char.to_json())
2929

3030

31-
@routes.get('/guild/{name}')
31+
@routes.get('/guilds/{name}')
3232
async def get_guild(request):
3333
name = request.match_info['name']
3434
char = await app["tibiapy"].fetch_guild(name)
3535
return web.Response(text=char.to_json())
3636

3737

38-
@routes.get('/guilds/{name}')
38+
@routes.get('/worlds/{name}/guilds')
3939
async def get_guilds(request):
4040
name = request.match_info['name']
4141
guild_list = await app["tibiapy"].fetch_world_guilds(name)
@@ -86,7 +86,7 @@ async def get_worlds(request):
8686
return web.Response(text=worlds.to_json())
8787

8888

89-
@routes.get('/world/{name}')
89+
@routes.get('/worlds/{name}')
9090
async def get_world(request):
9191
name = request.match_info['name']
9292
world = await app["tibiapy"].fetch_world(name)
@@ -113,6 +113,22 @@ async def get_news_html(request):
113113
return web.Response(text=news.content, content_type='text/html')
114114

115115

116+
@routes.get('/tournaments/{tournament_id}')
117+
async def get_tournaments(request):
118+
tournament_id = request.match_info['tournament_id']
119+
tournament = await app["tibiapy"].fetch_tournament(int(tournament_id))
120+
return web.Response(text=tournament.to_json())
121+
122+
123+
@routes.get('/tournaments/{tournament_id}/leaderboards/{world}/{page}')
124+
async def get_tournaments_leaderboard(request):
125+
tournament_id = request.match_info['tournament_id']
126+
world = request.match_info['world']
127+
page = request.match_info['page']
128+
tournament = await app["tibiapy"].fetch_tournament_leaderboard(int(tournament_id), world, int(page))
129+
return web.Response(text=tournament.to_json())
130+
131+
116132
def json_error(status_code: int, exception: Exception, tb=None) -> web.Response:
117133
return web.Response(
118134
status=status_code,

tests/resources/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ nonexistent world or a unselected world.
100100
- [tibiacom_leaderboard_selector.txt](tournaments/tibiacom_leaderboard_selector.txt) - The content of the tournament leaderboards before selecting any options.
101101
- [tibiacom_not_found.txt](tournaments/tibiacom_not_found.txt) - The content of the tournament section for a tournament that doesn't exist.
102102
- [tibiacom_sign_up.txt](tournaments/tibiacom_sign_up.txt) - The content for the tournament's page for a upcoming tournament.
103+
- [tibiacomnot_active.txt](tournaments/tibiacom_not_active.txt) - The content for the tournament's page when there's no running tournament.
103104

104105
## World resources
105106
- [tibiacom_online.txt](world/tibiacom_online.txt) - An online world on Tibia.com.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="BoxContent" style="background-image:url(https://static.tibia.com/images/global/content/scroll.gif);">
2+
<center class="TournamentBannerRelaive"><img src="https://static.tibia.com/images/global/content/tournament_banner.png" /></center><br /><div class="TableContainer"> <table class="Table5" cellpadding="0" cellspacing="0"> <div class="CaptionContainer"> <div class="CaptionInnerContainer"> <span class="CaptionEdgeLeftTop" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionEdgeRightTop" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionBorderTop" style="background-image:url(https://static.tibia.com/images/global/content/table-headline-border.gif);"></span> <span class="CaptionVerticalLeft" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-vertical.gif);" /></span> <div class="Text">Your Tournament Information</div> <span class="CaptionVerticalRight" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-vertical.gif);" /></span> <span class="CaptionBorderBottom" style="background-image:url(https://static.tibia.com/images/global/content/table-headline-border.gif);"></span> <span class="CaptionEdgeLeftBottom" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionEdgeRightBottom" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> </div> </div> <tr> <td> <div class="InnerTableContainer"> <table style="width:100%;"><tr><td><div class="TableShadowContainerRightTop"> <div class="TableShadowRightTop" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-rt.gif);"></div></div><div class="TableContentAndRightShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-rm.gif);"> <div class="TableContentContainer"> <table class="TableContent" width="100%" style="border:1px solid #faf0d7;"><tr class="Even"><td><p><div id="TournamentPersonalInformationLoginButton"><form action="https://www.tibia.com/account/index.php?subtopic=redirectlogin&redirect=https%3A%2F%2Fwww.tibia.com%2Fcommunity%2F%3Fsubtopic%3Dtournament" method="post" style="padding:0px;margin:0px;"><div class="BigButton" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton.gif)"><div onMouseOver="MouseOverBigButton(this);" onMouseOut="MouseOutBigButton(this);"><div class="BigButtonOver" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton_over.gif);"></div><input class="ButtonText" type="image" name="Login" alt="Login" src="https://static.tibia.com/images/global/buttons/_sbutton_login.gif"></div></div></form></div><b>Log in</b> to see your personal Tournament information!</p></td></tr> </table> </div></div><div class="TableShadowContainer"> <div class="TableBottomShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-bm.gif);"> <div class="TableBottomLeftShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-bl.gif);"></div> <div class="TableBottomRightShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-br.gif);"></div> </div></div></td></tr><tr><td><table class="InnerTableButtonRow" cellpadding="0" cellspacing="0"><tr><td><div style="float: right; margin-right: 5px; margin-left: 5px;"><form action="https://www.tibia.com/community/?subtopic=tournamentleaderboards" method="post" style="padding:0px;margin:0px;"><div class="BigButton" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton.gif)"><div onMouseOver="MouseOverBigButton(this);" onMouseOut="MouseOutBigButton(this);"><div class="BigButtonOver" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton_over.gif);"></div><input class="ButtonText" type="image" name="Tournament Leaderboard" alt="Tournament Leaderboard" src="https://static.tibia.com/images/global/buttons/_sbutton_leaderboard.gif"></div></div></form></div><div style="float: right; margin-right: 5px; margin-left: 5px;"><form action="https://www.tibia.com/community/?subtopic=tournament" method="post" style="padding:0px;margin:0px;"><input type="hidden" name="action" value="archive"><div class="BigButton" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton.gif)"><div onMouseOver="MouseOverBigButton(this);" onMouseOut="MouseOutBigButton(this);"><div class="BigButtonOver" style="background-image:url(https://static.tibia.com/images/global/buttons/sbutton_over.gif);"></div><input class="ButtonText" type="image" name="Tournament Archive" alt="Tournament Archive" src="https://static.tibia.com/images/global/buttons/_sbutton_archive.gif"></div></div></form></div></tr></table></td></tr> </table> </div> </td> </tr> </table></div><br /><div class="TableContainer"> <table class="Table5" cellpadding="0" cellspacing="0"> <div class="CaptionContainer"> <div class="CaptionInnerContainer"> <span class="CaptionEdgeLeftTop" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionEdgeRightTop" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionBorderTop" style="background-image:url(https://static.tibia.com/images/global/content/table-headline-border.gif);"></span> <span class="CaptionVerticalLeft" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-vertical.gif);" /></span> <div class="Text">Tournament Details</div> <span class="CaptionVerticalRight" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-vertical.gif);" /></span> <span class="CaptionBorderBottom" style="background-image:url(https://static.tibia.com/images/global/content/table-headline-border.gif);"></span> <span class="CaptionEdgeLeftBottom" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> <span class="CaptionEdgeRightBottom" style="background-image:url(https://static.tibia.com/images/global/content/box-frame-edge.gif);" /></span> </div> </div> <tr> <td> <div class="InnerTableContainer"> <table style="width:100%;"><tr><td><div class="TableShadowContainerRightTop"> <div class="TableShadowRightTop" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-rt.gif);"></div></div><div class="TableContentAndRightShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-rm.gif);"> <div class="TableContentContainer"> <table class="TableContent" width="100%" style="border:1px solid #faf0d7;"><tr><td><b>Currently there is no Tournament running.</b><p>Check out our <a href="https://www.tibia.com/news/">News</a> section regularly to find out when the next Tournament will start!</p></td></tr> </table> </div></div><div class="TableShadowContainer"> <div class="TableBottomShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-bm.gif);"> <div class="TableBottomLeftShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-bl.gif);"></div> <div class="TableBottomRightShadow" style="background-image:url(https://static.tibia.com/images/global/content/table-shadow-br.gif);"></div> </div></div></td></tr> </table> </div> </td> </tr> </table></div><br /> </div>
3+
</div>
4+
</div>
5+
<div class="Border_1" style="background-image:url(https://static.tibia.com/images/global/content/border-1.gif);"></div>
6+
<div class="CornerWrapper-b"><div class="Corner-bl" style="background-image:url(https://static.tibia.com/images/global/content/corner-bl.gif);"></div></div>
7+
<div class="CornerWrapper-b"><div class="Corner-br" style="background-image:url(https://static.tibia.com/images/global/content/corner-br.gif);"></div></div>
8+
</div>

tests/tests_tournament.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
FILE_TOURNAMENT_SIGN_UP = "tournaments/tibiacom_sign_up.txt"
88
FILE_TOURNAMENT_ARCHIVE = "tournaments/tibiacom_archive.txt"
9+
FILE_TOURNAMENT_NOT_ACTIVE = "tournaments/tibiacom_not_active.txt"
910
FILE_TOURNAMENT_NOT_FOUND = "tournaments/tibiacom_not_found.txt"
1011
FILE_TOURNAMENT_LEADERBOARD_ENDED = "tournaments/tibiacom_leaderboard_ended.txt"
1112
FILE_TOURNAMENT_LEADERBOARD_CURRENT = "tournaments/tibiacom_leaderboard_current.txt"
@@ -126,6 +127,12 @@ def test_tournament_from_content_archived(self):
126127
self.assertIsNone(last_prize.deed)
127128
self.assertIsNone(last_prize.other_rewards)
128129

130+
def test_tournament_from_content_not_active(self):
131+
"""Testing parsing the tournament page when ther'es no active tournament.."""
132+
content = self._load_resource(FILE_TOURNAMENT_NOT_ACTIVE)
133+
tournament = Tournament.from_content(content)
134+
self.assertIsNone(tournament)
135+
129136
def test_tournament_from_content_not_found(self):
130137
"""Testing parsing a tournament that doesn't exist."""
131138
content = self._load_resource(FILE_TOURNAMENT_NOT_FOUND)

tests/tests_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,6 @@ def test_parse_integer(self):
181181

182182
def test_get_tibia_url(self):
183183
self.assertEqual("https://www.tibia.com/community/?subtopic=character&name=Galarzaa+Fidera",
184-
get_tibia_url("community", "character", {"name": "Galarzaa Fidera"}))
184+
get_tibia_url("community", "character", name="Galarzaa Fidera"))
185185
self.assertEqual("https://www.tibia.com/community/?subtopic=character&name=Fn%F6",
186-
get_tibia_url("community", "character", {"name": "Fnö"}))
186+
get_tibia_url("community", "character", name="Fnö"))

tibiapy/abc.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@
88
from tibiapy.enums import HouseType, HouseStatus, HouseOrder
99
from tibiapy.utils import get_tibia_url
1010

11-
CHARACTER_URL = "https://www.tibia.com/community/?subtopic=characters&name=%s"
1211
CHARACTER_URL_TIBIADATA = "https://api.tibiadata.com/v2/characters/%s.json"
13-
HOUSE_URL = "https://www.tibia.com/community/?subtopic=houses&page=view&houseid=%d&world=%s"
1412
HOUSE_URL_TIBIADATA = "https://api.tibiadata.com/v2/house/%s/%d.json"
15-
HOUSE_LIST_URL = "https://www.tibia.com/community/?subtopic=houses&world=%s&town=%s&type=%s&status=%s&order=%s"
1613
HOUSE_LIST_URL_TIBIADATA = "https://api.tibiadata.com/v2/houses/%s/%s/%s.json"
17-
GUILD_URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=%s"
1814
GUILD_URL_TIBIADATA = "https://api.tibiadata.com/v2/guild/%s.json"
19-
GUILD_LIST_URL = "https://www.tibia.com/community/?subtopic=guilds&world="
2015
GUILD_LIST_URL_TIBIADATA = "https://api.tibiadata.com/v2/guilds/%s.json"
21-
NEWS_URL = "https://www.tibia.com/news/?subtopic=newsarchive&id=%d"
22-
NEWS_SEARCH_URL = "https://www.tibia.com/news/?subtopic=newsarchive"
23-
WORLD_URL = "https://www.tibia.com/community/?subtopic=worlds&world=%s"
2416
WORLD_URL_TIBIADATA = "https://api.tibiadata.com/v2/world/%s.json"
2517

2618

@@ -147,7 +139,7 @@ def get_url(cls, name):
147139
--------
148140
:class:`str`
149141
The URL to the character's page."""
150-
return CHARACTER_URL % urllib.parse.quote(name.encode('iso-8859-1'))
142+
return get_tibia_url("community", "characters", name=name)
151143

152144
@classmethod
153145
def get_url_tibiadata(cls, name):
@@ -206,7 +198,7 @@ def get_url(cls, name):
206198
--------
207199
:class:`str`
208200
The URL to the guild's page"""
209-
return GUILD_URL % urllib.parse.quote(name.encode('iso-8859-1'))
201+
return get_tibia_url("community", "guilds", page="view", GuildName=name)
210202

211203
@classmethod
212204
def get_url_tibiadata(cls, name):
@@ -237,7 +229,7 @@ def get_world_list_url(cls, world):
237229
:class:`str`
238230
The URL to the guild's page
239231
"""
240-
return GUILD_LIST_URL + urllib.parse.quote(world.title().encode('iso-8859-1'))
232+
return get_tibia_url("community", "guilds", world=world)
241233

242234
@classmethod
243235
def get_world_list_url_tibiadata(cls, world):
@@ -302,7 +294,7 @@ def get_url(cls, house_id, world):
302294
-------
303295
The URL to the house in Tibia.com
304296
"""
305-
return HOUSE_URL % (house_id, world)
297+
return get_tibia_url("community", "houses", page="view", houseid=house_id, world=world)
306298

307299
@classmethod
308300
def get_url_tibiadata(cls, house_id, world):
@@ -347,7 +339,8 @@ def get_list_url(cls, world, town, house_type: HouseType = HouseType.HOUSE, stat
347339
"""
348340
house_type = "%ss" % house_type.value
349341
status = "" if status is None else status.value
350-
return HOUSE_LIST_URL % (urllib.parse.quote(world), urllib.parse.quote(town), house_type, status, order.value)
342+
return get_tibia_url("community", "houses", world=world, town=town, type=house_type, status=status,
343+
order=order.value)
351344

352345
@classmethod
353346
def get_list_url_tibiadata(cls, world, town, house_type: HouseType = HouseType.HOUSE):
@@ -469,7 +462,7 @@ def get_url(cls, news_id):
469462
--------
470463
:class:`str`
471464
The URL to the news' page"""
472-
return NEWS_URL % news_id
465+
return get_tibia_url("news", "newsarchive", id=news_id)
473466

474467
@classmethod
475468
def get_list_url(cls):
@@ -485,7 +478,7 @@ def get_list_url(cls):
485478
:class:`str`
486479
The URL to the news archive page on Tibia.com.
487480
"""
488-
return NEWS_SEARCH_URL
481+
return get_tibia_url("news", "newsarchive")
489482

490483

491484
class BaseTournament(Serializable, metaclass=abc.ABCMeta):
@@ -530,8 +523,11 @@ def get_url(cls, tournament_cycle):
530523
"""
531524
params = None
532525
if tournament_cycle:
533-
params = {"tournamentcycle": tournament_cycle}
534-
return get_tibia_url("community", "tournament", params)
526+
params = {
527+
"tournamentcycle": tournament_cycle,
528+
"action": "archive",
529+
}
530+
return get_tibia_url("community", "tournament", **params)
535531

536532

537533
class BaseWorld(Serializable, metaclass=abc.ABCMeta):
@@ -609,7 +605,7 @@ def get_url(cls, name):
609605
:class:`str`
610606
The URL to the world's information page.
611607
"""
612-
return WORLD_URL % name.title()
608+
return get_tibia_url("community", "worlds", world=name.title())
613609

614610
@classmethod
615611
def get_url_tibiadata(cls, name):

tibiapy/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ async def fetch_news_archive(self, begin_date, end_date, categories=None, types=
441441
if NewsType.NEWS_TICKER in types:
442442
data["filter_ticker"] = "ticker"
443443

444-
content = await self._post(tibiapy.abc.NEWS_SEARCH_URL, data)
444+
content = await self._post(News.get_list_url(), data)
445445
news = ListedNews.list_from_content(content)
446446
return news
447447

tibiapy/highscores.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import math
66

77
from tibiapy import Category, InvalidContent, Vocation, VocationFilter, abc
8-
from tibiapy.utils import parse_json, parse_tibiacom_content, try_enum
8+
from tibiapy.utils import get_tibia_url, parse_json, parse_tibiacom_content, try_enum
99

1010
__all__ = (
1111
"ExpHighscoresEntry",
@@ -16,7 +16,6 @@
1616

1717
results_pattern = re.compile(r'Results: (\d+)')
1818

19-
HIGHSCORES_URL = "https://www.tibia.com/community/?subtopic=highscores&world=%s&list=%s&profession=%d&currentpage=%d"
2019
HIGHSCORES_URL_TIBIADATA = "https://api.tibiadata.com/v2/highscores/%s/%s/%s.json"
2120

2221

@@ -211,7 +210,8 @@ def get_url(cls, world, category=Category.EXPERIENCE, vocation=VocationFilter.AL
211210
-------
212211
The URL to the Tibia.com highscores.
213212
"""
214-
return HIGHSCORES_URL % (world, category.value, vocation.value, page)
213+
return get_tibia_url("community", "highscores", world=world, list=category.value, profession=vocation.value,
214+
currentpage=page)
215215

216216
@classmethod
217217
def get_url_tibiadata(cls, world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL):

tibiapy/kill_statistics.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from typing import Dict
22

33
from tibiapy import abc, InvalidContent
4-
from tibiapy.utils import parse_tibiacom_content
4+
from tibiapy.utils import get_tibia_url, parse_tibiacom_content
55

66
__all__ = (
77
"KillStatistics",
88
"RaceEntry",
99
)
1010

11-
KILL_STATISTICS_URL = "https://www.tibia.com/community/?subtopic=killstatistics&world=%s"
12-
1311

1412
class KillStatistics(abc.Serializable):
1513
"""Represents the kill statistics of a world.
@@ -56,7 +54,7 @@ def get_url(cls, world):
5654
-------
5755
The URL to the Tibia.com kill statistics for this world.
5856
"""
59-
return KILL_STATISTICS_URL % world
57+
return get_tibia_url("community", "killstatistics", world=world)
6058

6159
@classmethod
6260
def from_content(cls, content):

0 commit comments

Comments
 (0)