Skip to content

Commit

Permalink
Merge pull request #19 from ukwhatn/develop
Browse files Browse the repository at this point in the history
Release v3.1.0dev2
  • Loading branch information
ukwhatn authored Aug 17, 2024
2 parents 2392a1a + 234ec97 commit 1443937
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 158 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "wikidot"
version = "3.1.0dev1"
version = "3.1.0dev2"
authors = [{ name = "ukwhatn", email = "[email protected]" }]
description = "Wikidot Utility Library"
readme = "README.md"
Expand Down
21 changes: 12 additions & 9 deletions src/wikidot/module/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@
if TYPE_CHECKING:
from wikidot.module.site import Site


class ForumCategoryMethods:
def __init__(self, forum: "Forum") -> None:
self.forum = forum

def get(self, id: int):
category = ForumCategory(
site = self.forum.site,
id = id,
forum = self.forum,
)
site=self.forum.site,
id=id,
forum=self.forum,
)
return category.update()


class ForumThreadMethods:
def __init__(self, forum: "Forum") -> None:
self.forum = forum

def get(self, id: int):
thread = ForumThread(
site = self.forum.site,
id = id,
forum = self.forum,
site=self.forum.site,
id=id,
forum=self.forum,
)
return thread.update()


@dataclass
class Forum:
site: "Site"
Expand Down
60 changes: 34 additions & 26 deletions src/wikidot/module/forum_category.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime
import re
from typing import TYPE_CHECKING, Optional

from bs4 import BeautifulSoup
Expand All @@ -12,21 +12,21 @@
from wikidot.util.parser import user as user_parser

if TYPE_CHECKING:
from wikidot.module.forum_group import ForumGroup
from wikidot.module.forum import Forum
from wikidot.module.forum_group import ForumGroup
from wikidot.module.site import Site


class ForumCategoryCollection(list["ForumCategory"]):
def __init__(self, forum: "Forum", categories: list["ForumCategory"]):
super().__init__(categories)
self.forum = forum

def __iter__(self) -> Iterator["ForumCategory"]:
return super().__iter__()

@staticmethod
def get_categories(site: "Site",forum: "Forum"):
def get_categories(site: "Site", forum: "Forum"):
categories = []

for group in forum.groups:
Expand All @@ -36,10 +36,11 @@ def get_categories(site: "Site",forum: "Forum"):

def find(self, id: int = None, title: str = None) -> Optional["ForumCategory"]:
for category in self:
if ((id is None or category.id == id) and
(title is None or category.title) == title):
if (id is None or category.id == id) and (
title is None or category.title
) == title:
return category

@staticmethod
def _acquire_update(forum: "Forum", categories: list["ForumCategory"]):
if len(categories) == 0:
Expand All @@ -49,7 +50,7 @@ def _acquire_update(forum: "Forum", categories: list["ForumCategory"]):
[
{
"c": category.id,
"moduleName":"forum/ForumViewCategoryModule",
"moduleName": "forum/ForumViewCategoryModule",
}
for category in categories
]
Expand All @@ -58,7 +59,9 @@ def _acquire_update(forum: "Forum", categories: list["ForumCategory"]):
html = BeautifulSoup(response.json()["body"], "lxml")
statistics = html.select_one("div.statistics").text
description = html.select_one("div.description-block").text.strip()
info = re.search(r"([ \S]*) /\s+([ \S]*)", html.select_one("div.forum-breadcrumbs").text)
info = re.search(
r"([ \S]*) /\s+([ \S]*)", html.select_one("div.forum-breadcrumbs").text
)
counts = re.findall(r"\d+", statistics)

if category.posts_counts != int(counts[1]):
Expand All @@ -67,16 +70,17 @@ def _acquire_update(forum: "Forum", categories: list["ForumCategory"]):
category.threads_counts, category.posts_counts = counts
category.group = category.forum.groups.find(info.group(1))
category.title = info.group(2)
if (pagerno:=html.select_one("span.pager-no")) is None:
if (pagerno := html.select_one("span.pager-no")) is None:
category.pagerno = 1
else:
category.pagerno = int(re.search(r"of (\d+)", pagerno.text).group(1))

return categories

def update(self):
return ForumCategoryCollection._acquire_update(self.forum, self)


@dataclass
class ForumCategory:
site: "Site"
Expand All @@ -91,7 +95,7 @@ class ForumCategory:
_last_thread_id: int = None
_last_post_id: int = None
_last: "ForumPost" = None

def get_url(self):
return f"{self.site.get_url}/forum/c-{self.id}"

Expand All @@ -102,9 +106,11 @@ def update(self):
def last(self):
if self._last_thread_id is not None and self._last_post_id is not None:
if self._last is None:
self._last = self.forum.thread.get(self._last_thread_id).get(self._last_post_id)
self._last = self.forum.thread.get(self._last_thread_id).get(
self._last_post_id
)
return self._last

@last.setter
def last(self, value: "ForumPost"):
self._last = value
Expand All @@ -116,9 +122,9 @@ def threads(self):
responses = self.site.amc_request(
[
{
"p": no+1,
"p": no + 1,
"c": self.id,
"moduleName":"forum/ForumViewCategoryModule",
"moduleName": "forum/ForumViewCategoryModule",
}
for no in range(self.pagerno)
]
Expand All @@ -127,10 +133,10 @@ def threads(self):
threads = []

for response in responses:
html = BeautifulSoup(response.json()["body"],"lxml")
html = BeautifulSoup(response.json()["body"], "lxml")
for info in html.select("table.table tr.head~tr"):
title = info.select_one("div.title a")
thread_id = re.search(r"t-(\d+)",title.get("href")).group(1)
thread_id = re.search(r"t-(\d+)", title.get("href")).group(1)
description = info.select_one("div.description")
user = info.select_one("span.printuser")
odate = info.select_one("span.odate")
Expand All @@ -139,7 +145,9 @@ def threads(self):
if last_id is None:
post_id = None
else:
post_id = int(re.search(r"post-(\d+)",last_id.get("href")).group(1))
post_id = int(
re.search(r"post-(\d+)", last_id.get("href")).group(1)
)

thread = ForumThread(
site=self.site,
Expand All @@ -150,14 +158,14 @@ def threads(self):
created_by=user_parser(client, user),
created_at=odate_parser(odate),
posts_counts=int(posts_count.text),
_last_post_id=post_id
_last_post_id=post_id,
)

threads.append(thread)

return ForumThreadCollection(self, threads)
def new_thread(self, title: str, source :str, description: str = ""):

def new_thread(self, title: str, source: str, description: str = ""):
client = self.site.client
client.login_check()

Expand All @@ -169,7 +177,7 @@ def new_thread(self, title: str, source :str, description: str = ""):
"description": description,
"source": source,
"action": "ForumAction",
"event": "newThread"
"event": "newThread",
}
]
)[0]
Expand All @@ -185,5 +193,5 @@ def new_thread(self, title: str, source :str, description: str = ""):
description=description,
created_by=client.user.get(client.username),
created_at=datetime.fromtimestamp(body["CURRENT_TIMESTAMP"]),
posts_counts=1
)
posts_counts=1,
)
48 changes: 28 additions & 20 deletions src/wikidot/module/forum_group.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import re
from collections.abc import Iterator
from dataclasses import dataclass
import re
from typing import TYPE_CHECKING, Optional

from bs4 import BeautifulSoup

from wikidot.module.forum_category import ForumCategory, ForumCategoryCollection

if TYPE_CHECKING:
from wikidot.module.site import Site
from wikidot.module.forum import Forum
from wikidot.module.site import Site


class ForumGroupCollection(list["ForumGroup"]):
def __init__(self, forum: "Forum", groups: list["ForumGroup"]):
super().__init__(groups)
self.forum = forum

def __iter__(self) -> Iterator["ForumGroup"]:
return super().__iter__()

@staticmethod
def get_groups(site: "Site",forum: "Forum"):
def get_groups(site: "Site", forum: "Forum"):
groups = []

response = site.amc_request([{
"moduleName":"forum/ForumStartModule",
"hidden":"true"
}])[0]
response = site.amc_request(
[{"moduleName": "forum/ForumStartModule", "hidden": "true"}]
)[0]
body = response.json()["body"]
html = BeautifulSoup(body, "lxml")

Expand All @@ -36,7 +35,7 @@ def get_groups(site: "Site",forum: "Forum"):
site=site,
forum=forum,
title=group_info.select_one("div.title").text,
description=group_info.select_one("div.description").text
description=group_info.select_one("div.description").text,
)

categories = []
Expand All @@ -49,44 +48,53 @@ def get_groups(site: "Site",forum: "Forum"):
if last_id is None:
thread_id, post_id = None, None
else:
thread_id, post_id = re.search(r"t-(\d+).+post-(\d+)",last_id.get("href")).groups()
thread_id, post_id = re.search(
r"t-(\d+).+post-(\d+)", last_id.get("href")
).groups()
thread_id, post_id = int(thread_id), int(post_id)

category = ForumCategory(
site=site,
id=int(re.search(r"c-(\d+)", name.select_one("a").get("href")).group(1)),
id=int(
re.search(r"c-(\d+)", name.select_one("a").get("href")).group(1)
),
description=name.select_one("div.description").text,
forum=forum,
title=name.select_one("a").text,
group=group,
threads_counts=thread_count,
posts_counts=post_count,
_last_thread_id=thread_id,
_last_post_id=post_id
_last_post_id=post_id,
)

categories.append(category)

group.categories = ForumCategoryCollection(site, categories)

groups.append(group)

forum._groups = ForumGroupCollection(site, groups)

def find(self, title: str = None, description: str = None) -> Optional["ForumGroup"]:
def find(
self, title: str = None, description: str = None
) -> Optional["ForumGroup"]:
for group in self:
if ((title is None or group.title == title) and
(description is None or group.description == description)):
if (title is None or group.title == title) and (
description is None or group.description == description
):
return group

def findall(self, title: str = None, description: str = None) -> list["ForumGroup"]:
groups = []
for group in self:
if ((title is None or group.title == title) and
(description is None or group.description == description)):
if (title is None or group.title == title) and (
description is None or group.description == description
):
groups.append(group)
return groups


@dataclass
class ForumGroup:
site: "Site"
Expand Down
Loading

0 comments on commit 1443937

Please sign in to comment.