Hide keyboard shortcuts

Hot-keys 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 2019 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of otaku-info-bot. 

5 

6otaku-info-bot 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. 

10 

11otaku-info-bot 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. 

15 

16You should have received a copy of the GNU General Public License 

17along with otaku-info-bot. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import requests 

21from typing import Dict, List, Optional 

22from bs4 import BeautifulSoup 

23 

24 

25def load_ln_releases(year: Optional[int] = None, month: Optional[str] = None) \ 

26 -> Dict[int, Dict[str, List[Dict[str, str]]]]: 

27 """ 

28 Loads the currently available light novel releases from reddit's 

29 /r/lightnovels subreddit. 

30 :param year: Limits releases to a specific year 

31 :param month: Limits releases to a specific month 

32 :return: The releases, categorized by year and month 

33 """ 

34 releases = {} # type: Dict[int, Dict[str, List[Dict[str, str]]]] 

35 

36 page = "https://old.reddit.com/r/LightNovels/wiki/upcomingreleases" 

37 headers = {"User-Agent": "Mozilla/5.0"} 

38 resp = requests.get(page, headers=headers).text 

39 soup = BeautifulSoup(resp, "html.parser") 

40 

41 years = soup.find_all("h2") 

42 years = years[5:] 

43 years = years[:-1] 

44 years = list(map(lambda x: int(x.text), years)) 

45 

46 months = [ 

47 "january", "february", "march", "april", 

48 "may", "june", "july", "august", 

49 "september", "october", "november", "december" 

50 ] 

51 

52 for _year in years: 

53 releases[_year] = {} 

54 for _month in months: 

55 releases[_year][_month] = [] 

56 

57 tables = soup.find_all("tbody") 

58 tables = tables[2:-1] 

59 

60 current_year = years.pop(0) 

61 first_table = True 

62 for table in tables: 

63 first_entry = True 

64 for entry in table.find_all("tr"): 

65 

66 parts = entry.find_all("td") 

67 title = parts[1].text 

68 

69 try: 

70 int(title) 

71 continue 

72 except ValueError: 

73 pass 

74 

75 _month, day = parts[0].text.split(" ", 1) 

76 _month = _month.lower() 

77 volume = parts[2].text 

78 

79 if first_entry and month == "January" and not first_table: 

80 current_year = years.pop(0) 

81 first_entry = False 

82 

83 releases[current_year][_month].append({ 

84 "title": title, 

85 "volume": volume, 

86 "day": day, 

87 "release_type": parts[4].text 

88 }) 

89 

90 first_table = False 

91 

92 if year is not None: 

93 releases = {year: releases.get(year, {})} 

94 if month is not None: 

95 _releases = {} # type: Dict[int, Dict[str, List[Dict[str, str]]]] 

96 for year, data in releases.items(): 

97 _releases[year] = {month: data.get(month, [])} 

98 releases = _releases 

99 

100 return releases