Coverage for otaku_info/external/reddit.py: 49%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info.
6otaku-info is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11otaku-info is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with otaku-info. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20import requests
21from typing import List, Optional
22from datetime import datetime
23from bs4 import BeautifulSoup
24from jerrycan.base import app
25from otaku_info.external.entities.RedditLnRelease import RedditLnRelease
28def load_ln_releases(year: Optional[int] = None) -> List[RedditLnRelease]:
29 """
30 Loads the light novel releases
31 """
32 releases: List[RedditLnRelease] = []
33 current_year = datetime.utcnow().year
35 if year is None: 35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true
36 for year in range(2018, current_year + 2):
37 releases += load_ln_releases(year)
38 return releases
40 tables = load_tables(year)
42 for i, table in enumerate(tables):
43 month_number = i + 1
45 for entry in table.find_all("tr"):
46 release = RedditLnRelease.from_parts(year, entry.find_all("td"))
48 if month_number != release.release_date.month: 48 ↛ 49line 48 didn't jump to line 49, because the condition on line 48 was never true
49 app.logger.debug(
50 f"Incorrect month: "
51 f"{month_number} != {release.release_date.month} "
52 f"({release.release_date}/{release.series_name})"
53 )
55 releases.append(release)
57 return releases
60def load_tables(year: int) -> List[BeautifulSoup]:
61 """
62 Loads the tables containing the release data
63 """
64 current_year = datetime.utcnow().year
66 # TODO Parse years from 2015-2017
67 if year < 2018 or year > current_year + 1: 67 ↛ 68line 67 didn't jump to line 68, because the condition on line 67 was never true
68 return []
70 url = f"https://old.reddit.com/r/LightNovels/wiki/{year}releases"
72 if year >= current_year: 72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never true
73 if requests.head(url).status_code >= 300:
74 url = "https://old.reddit.com/r/LightNovels/wiki/upcomingreleases"
76 headers = {"User-Agent": "Mozilla/5.0"}
77 resp = requests.get(url, headers=headers).text
78 soup = BeautifulSoup(resp, "html.parser")
80 tables = soup.find_all("tbody")
82 # Table 0: Releases for current month on side bar
83 # Table 1: Table below current month releases on side bar
84 # Table -1: To be announced
85 tables = tables[2:-1]
87 if url.endswith("upcomingreleases"): 87 ↛ 89line 87 didn't jump to line 89, because the condition on line 87 was never true
89 month_names = soup.find_all("h3")
90 month_names.pop(0)
91 month_names = [
92 x.text.lower().split(" ")[0].strip() for x in month_names
93 ]
95 is_current_year = True
96 filtered = []
97 for i, month_name in enumerate(month_names):
98 table = tables[i]
100 if (year == current_year and is_current_year) or \
101 (year > current_year and not is_current_year):
102 filtered.append(table)
104 if month_name == "december":
105 is_current_year = False
106 tables = filtered
108 while year == current_year and len(tables) < 12:
109 tables = [BeautifulSoup("")] + tables
111 return tables