Skip to content

Commit 1913748

Browse files
committed
Added method to fetch forum post
1 parent 692ccc3 commit 1913748

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ 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+
.. v3.1.0:
10+
11+
3.1.0 (2020-07-29)
12+
==================
13+
14+
- Added ``fetch_forum_post`` method to fetch a forum post directly.
15+
- Fixed bug with forum posts made by tournament characters.
16+
917
.. v3.0.3:
1018
1119
3.0.3 (2020-07-28)

docs/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ ForumBoard
127127
:members:
128128
:inherited-members:
129129

130+
ForumPost
131+
---------
132+
.. autoclass:: ForumPost
133+
:members:
134+
:inherited-members:
135+
130136
ForumThread
131137
-----------
132138
.. autoclass:: ForumThread

serve.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ async def get_forum_thread(request: web.Request):
9999
return web.json_response(thread, dumps=CustomJson.dumps)
100100

101101

102+
@routes.get('/forums/post/{post_id}')
103+
async def get_forum_post(request: web.Request):
104+
post_id = request.match_info['post_id']
105+
post = await app["tibiapy"].fetch_forum_post(int(post_id))
106+
return web.json_response(post, dumps=CustomJson.dumps)
107+
108+
102109
@routes.get('/forums/announcement/{announcement_id}/')
103110
async def get_forum_announcement(request: web.Request):
104111
announcement_id = request.match_info['announcement_id']

tibiapy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from tibiapy.world import *
1717
from tibiapy.client import *
1818

19-
__version__ = '3.0.3'
19+
__version__ = '3.1.0'
2020

2121
from logging import NullHandler
2222

tibiapy/client.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tibiapy.enums import Category, HouseOrder, HouseStatus, HouseType, NewsCategory, NewsType, VocationFilter
1414
from tibiapy.errors import Forbidden, NetworkError
1515
from tibiapy.event import EventSchedule
16-
from tibiapy.forum import CMPostArchive, ForumAnnouncement, ForumBoard, ForumThread, ListedBoard
16+
from tibiapy.forum import CMPostArchive, ForumAnnouncement, ForumBoard, ForumPost, ForumThread, ListedBoard
1717
from tibiapy.guild import Guild, GuildWars, ListedGuild
1818
from tibiapy.highscores import Highscores
1919
from tibiapy.house import House, ListedHouse
@@ -419,6 +419,41 @@ async def fetch_forum_thread(self, thread_id, page=1):
419419
parsing_time = time.perf_counter() - start_time
420420
return TibiaResponse(response, thread, parsing_time)
421421

422+
async def fetch_forum_post(self, post_id):
423+
"""Fetches a forum post with a given id.
424+
425+
The thread that contains the post will be returned, containing the desired post in
426+
:py:attr:`ForumThread.anchored_post`.
427+
428+
The displayed page will be the page where the post is located.
429+
430+
.. versionadded:: 3.1.0
431+
432+
Parameters
433+
----------
434+
post_id : :class:`int`
435+
The id of the post.
436+
437+
Returns
438+
-------
439+
:class:`TibiaResponse` of :class:`ForumThread`
440+
A response containing the forum, if found.
441+
442+
Raises
443+
------
444+
Forbidden
445+
If a 403 Forbidden error was returned.
446+
This usually means that Tibia.com is rate-limiting the client because of too many requests.
447+
NetworkError
448+
If there's any connection errors during the request."""
449+
response = await self._request("get", ForumPost.get_url(post_id))
450+
start_time = time.perf_counter()
451+
thread = ForumThread.from_content(response.content)
452+
if thread:
453+
thread.anchored_post = next((p for p in thread.posts if p.post_id == post_id), None)
454+
parsing_time = time.perf_counter() - start_time
455+
return TibiaResponse(response, thread, parsing_time)
456+
422457
async def fetch_forum_announcement(self, announcement_id):
423458
"""Fetches a forum announcement.
424459

tibiapy/forum.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ def _parse_author_table(cls, character_info_container):
512512
convert_line_breaks(char_info)
513513
char_info_text = char_info.text
514514
info_match = author_info_regex.search(char_info_text)
515-
author.world = info_match.group(1)
516-
author.vocation = try_enum(Vocation, info_match.group(2))
517-
author.level = int(info_match.group(3))
515+
if info_match:
516+
author.world = info_match.group(1)
517+
author.vocation = try_enum(Vocation, info_match.group(2))
518+
author.level = int(info_match.group(3))
518519
if guild_info:
519520
guild_match = guild_regexp.search(guild_info.text)
520521
guild_name = guild_match.group(2)
@@ -782,7 +783,7 @@ def __repr__(self):
782783

783784

784785
class ForumPost(abc.BasePost, abc.Serializable):
785-
"""Represent's a forum post.
786+
"""Represents a forum post.
786787
787788
.. versionadded:: 3.0.0
788789
@@ -872,6 +873,10 @@ class ForumThread(abc.BaseThread, abc.Serializable):
872873
Whether the thread has a golden frame or not.
873874
874875
In the Proposals board,a golden frame means the thread has a reply by a staff member.
876+
anchored_post: :class:`ForumPost`
877+
The post where the page is anchored to, if any.
878+
879+
When a post is fetched directly, the thread that contains it is displayed, anchored to the specific post.
875880
"""
876881
__slots__ = (
877882
"title",
@@ -883,6 +888,7 @@ class ForumThread(abc.BaseThread, abc.Serializable):
883888
"page",
884889
"total_pages",
885890
"golden_frame",
891+
"anchored_post",
886892
"posts",
887893
)
888894

@@ -897,6 +903,7 @@ def __init__(self, **kwargs):
897903
self.total_pages: int = kwargs.get("total_pages", 1)
898904
self.posts: List[ForumPost] = kwargs.get("posts", [])
899905
self.golden_frame: bool = kwargs.get("golden_frame", False)
906+
self.anchored_post: Optional[ForumPost] = None
900907

901908
def __repr__(self):
902909
return f"<{self.__class__.__name__} title={self.title!r} board={self.board!r} section={self.section!r}>"

0 commit comments

Comments
 (0)