Skip to content

Commit

Permalink
Multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timotk committed Jul 29, 2023
1 parent dd61a2a commit 4c2addc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
6 changes: 4 additions & 2 deletions tests/test_frontpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from tests.mocks import mock_get
from tweakers import frontpage
from tweakers.comment import Comment


@mock.patch("tweakers.frontpage.get", mock_get)
Expand All @@ -16,7 +17,8 @@ def test_article_comments():
article = frontpage.Article(
url="https://tweakers.net/nieuws/148534/amd-maakt-meer-winst-dankzij-goede-verkopen-van-ryzen-cpus.html",
title="AMD maakt meer winst dankzij goede verkopen van Ryzen-cpu's",
comment_count=10, # Can be any number
published_at=datetime.datetime(2019, 1, 30, 10, 45), # noqa: F821
comment_count=10, # Can be any number here
published_at=datetime.datetime(2019, 1, 30, 10, 45),
)
assert len(article.comments) > 0
assert all([isinstance(comment, Comment) for comment in article.comments])
8 changes: 8 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
from pydantic import ValidationError

from tweakers.user import User


Expand All @@ -9,3 +12,8 @@ def test_user_id():
def test_user_name():
user = User(name="Femme")
assert user


def test_user_no_id_and_no_name():
with pytest.raises(ValidationError):
User()
29 changes: 25 additions & 4 deletions tweakers/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
from tweakers.user import User


def get_comment_count(html: HTML) -> int:
def get_article_comment_count(html: HTML) -> int:
comment_count: int
try:
comment_count = int(html.find(".comment-counter", first=True).text)
except AttributeError:
comment_count = 0
return comment_count


def get_topic_comment_count(html: HTML) -> int:
comment_count: int
try:
comment_count = int(html.find(".commentCount", first=True).text)
Expand All @@ -22,14 +31,26 @@ def get_comment_count(html: HTML) -> int:

def active_topics(html: HTML) -> Generator[dict, None, None]:
for tr in html.find(".listing tr")[1:]:
poster_elem = tr.find(".poster", first=True)
span = poster_elem.find("span", first=True)
if span is not None and "multiauthor" in span.attrs["class"]:
author = [
User(name=name.strip()) for name in span.attrs["title"].split(",")
]
else:
username = poster_elem.find("a", first=True).text
user_url = tr.find(".poster a", first=True).attrs["href"]
user_id = user_url.split("/")[-1]
author = User(id=user_id, name=username, url=user_url)

topic: Dict = {
"title": tr.find(".topic a", first=True).text,
"url": tr.find(".topic a", first=True).attrs["href"],
"poster": tr.find(".poster", first=True).text,
"author": author,
"last_reply": dateparser.parse(
tr.find(".time a", first=True).text, languages=["nl"]
),
"comment_count": get_comment_count(tr),
"comment_count": get_topic_comment_count(tr),
}
yield topic

Expand Down Expand Up @@ -108,7 +129,7 @@ def frontpage_articles(html: HTML) -> Generator[dict, None, None]:
topic: Dict = {
"title": elem.find(".headline--anchor", first=True).text,
"url": elem.find("a.headline--anchor", first=True).attrs["href"],
"comment_count": get_comment_count(elem),
"comment_count": get_article_comment_count(elem),
"published_at": timestamp,
}
yield topic
Expand Down
14 changes: 7 additions & 7 deletions tweakers/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
"""

import time
from datetime import datetime
from typing import Any, Generator, List, Optional, Union

from pydantic import BaseModel
from requests_html import HTMLResponse

from tweakers.user import User

from . import parsers
from .comment import Comment
from .utils import get, id_from_url


class Topic(BaseModel):
url: str
title: Optional[str] = None
author: Optional[Union[User, list[User]]] = None
last_reply: Optional[datetime] = None
comment_count: Optional[int] = None
_html: Optional[Any] = None

@property
Expand All @@ -28,13 +35,6 @@ def html(self):
self._html = response.html
return self._html

@property
def title(self) -> str:
if self._title is None:
self._title = self.html.find("#title", first=True).text
return self._title
raise NotImplementedError

def comments(self, page: Union[int, str]) -> List[Comment]:
"""
Get comments for a specific Topic page
Expand Down

0 comments on commit 4c2addc

Please sign in to comment.