Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding comment count #47

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions moddb/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,12 @@ class Thumbnail:
"""

def __init__(self, **attrs):
self.url = join(attrs.get("url"))
self.name = attrs.get("name", None)
self.image = attrs.get("image", None)
self.summary = attrs.get("summary", None)
self.date = attrs.get("date", None)
self.type = attrs.get("type")
self.url: str = join(attrs.get("url"))
self.name: str | None = attrs.get("name", None)
self.image: str | None = attrs.get("image", None)
self.summary: str | None = attrs.get("summary", None)
self.date: datetime.datetime | None = attrs.get("date", None)
self.type: ThumbnailType = attrs.get("type")

def __repr__(self):
return f"<Thumbnail name={self.name} type={self.type.name}>"
Expand Down Expand Up @@ -516,7 +516,7 @@ def _parse_results(html):
)
except (TypeError, KeyError):
# parse as a title-content pair of articles
LOGGER.info("Parsing articles as key-pair list", exc_info=LOGGER.level >= logging.DEBUG)
LOGGER.info("Parsing articles as key-value pair list", exc_info=LOGGER.level >= logging.DEBUG)
for title, content in zip(search_raws[::2], search_raws[1::2]):
date = title.find("time")
url = title.find("h4").a
Expand Down Expand Up @@ -583,6 +583,27 @@ def _parse_comments(html):
return comments, current_page, total_page, total_results


class CommentAuthor(Thumbnail):
"""Represents the thumbnail of a user having left a comment on a page. Functions the same as a
thumbnail but with an extra attribute.

Attributes
-----------
comment_count : int
Number of comments the user has posted
"""

def __init__(self, **attrs):
super().__init__(**attrs)

self.comment_count: int = attrs.get("comment_count", 0)

def __repr__(self):
return (
f"<Thumbnail name={self.name} type={self.type.name} comment_count={self.comment_count}>"
)


class Comment:
"""A moddb comment object.

Expand Down Expand Up @@ -634,11 +655,19 @@ class Comment:
def __init__(self, html: BeautifulSoup):
author = html.find("a", class_="avatar")
self.id = int(html["id"])
self.author = Thumbnail(
comment_count = int(
html.find("span", class_="heading")
.text.strip()
.split("-")[-1]
.replace("comments", "")
.replace(",", "")
)
self.author = CommentAuthor(
name=author["title"],
url=author["href"],
image=author.img["src"],
type=ThumbnailType.member,
comment_count=comment_count,
)
self.date = get_date(html.find("time")["datetime"])
actions = html.find("span", class_="actions")
Expand Down
15 changes: 9 additions & 6 deletions moddb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ def __init__(self, rate: float, per: float, sleep: Optional[None] = None):
self.initial_call = datetime.datetime.min
self.call_count = 0

def reset(self, now: datetime.datetime):
def reset(self, now: datetime.datetime = None):
if now is None:
now = datetime.datetime.now()

self.initial_call = now
self.call_count = 0

Expand All @@ -114,7 +117,7 @@ def call(self):

expiry = self.initial_call + datetime.timedelta(seconds=self.per)
if now > expiry:
LOGGER.info("Reseting ratelimit")
LOGGER.info("Resetting ratelimit")
self.reset(now)

if self.call_count + 1 > self.rate:
Expand Down Expand Up @@ -265,9 +268,9 @@ def request(req: requests.Request):
The returned response object

"""
SESSION = sys.modules["moddb"].SESSION
prepped = prepare_request(req, SESSION)
r = SESSION.send(prepped)
session: requests.Session = sys.modules["moddb"].SESSION
prepped = prepare_request(req, session)
r = session.send(prepped)

raise_for_status(r)
return r
Expand Down Expand Up @@ -318,7 +321,7 @@ def get_page(url: str, *, params: dict = {}, json: bool = False):


def get_views(string: str) -> Tuple[int, int]:
"""A helper function that takes a string reresentation of total something and
"""A helper function that takes a string representation of total something and
daily amount of that same thing and returns both as a tuple of ints.

Parameters
Expand Down
4 changes: 2 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if [ ${1:-not_full} == "full" ]
then
pytest -k "main" --log-level="INFO" --html=report.html --self-contained-html --record-mode=new_episodes -vv "${@:2}"
python -m pytest -k "main" --log-level="INFO" --html=report.html --self-contained-html --record-mode=new_episodes -vv "${@:2}"
else
pytest -k "not main and not test_client" --log-level="INFO" --html=report.html --self-contained-html --record-mode=new_episodes -vv "$@"
python -m pytest -k "not main and not test_client" --log-level="INFO" --html=report.html --self-contained-html --record-mode=new_episodes -vv "$@"
fi
Loading
Loading