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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

"""LICENSE 

Copyright 2017 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of malscraper. 

 

malscraper is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

malscraper is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

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

along with malscraper. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

from typing import List 

from malscraper.Cache import Cache 

from malscraper.types.AiringState import AiringState 

from malscraper.types.MediaType import MediaType 

 

 

class MalAnime(object): 

""" 

Class that models a myanimelist anime 

""" 

 

def __init__(self, mal_id: int): 

""" 

Generates a MalAnime object 

:param mal_id: The anime's ID on myanimelist.net 

""" 

self.id = mal_id 

self.soup = Cache().load_mal_page(self.id, MediaType.ANIME) 

self.name = self.__parse_name() 

self.related_anime = self.__parse_related("anime") 

self.related_manga = self.__parse_related("manga") 

self.airing_status = self.__parse_airing_status() 

self.episode_count = self.__parse_episode_count() 

 

def __parse_name(self) -> str: 

""" 

Parses the title of the anime 

:return: The title of the name 

""" 

return self.soup.select("h1")[0].text 

 

def __parse_related(self, media_type: str) -> List[int]: 

""" 

Parses the URLs of related anime 

:param media_type: The media type to check for (anime or manga) 

:return: A dictionary consisting of the names and the URLS 

of the related series 

""" 

related = [] 

try: 

table = self.soup.select(".anime_detail_related_anime")[0] 

for entry in table.select("a"): 

path = entry["href"] 

mal_id = path.rsplit("/", 2)[1] 

if path.startswith("/" + media_type + "/"): 

related.append(int(mal_id)) 

 

except IndexError: 

pass 

return related 

 

def __parse_airing_status(self) -> AiringState: 

""" 

Parses the airing status of a series 

:return: The airing status 

""" 

state = str(self.soup)\ 

.split("Status:</span>")[1]\ 

.split("</div>")[0]\ 

.strip() 

 

for airing_type in AiringState: 

if airing_type.value == state: 

return airing_type 

 

def __parse_episode_count(self) -> int or None: 

""" 

Parses the episode count of a series 

:return: The amount of episodes or 

None if that information is not available 

""" 

eps = str(self.soup).split("Episodes:</span>")[1].split("</div>")[0]\ 

.strip() 

try: 

return int(eps) 

except ValueError: 

return None