Commit | Line | Data |
---|---|---|
bbcdf86a FT |
1 | import BeautifulSoup, urlparse |
2 | import lib, htcache | |
3 | soup = BeautifulSoup.BeautifulSoup | |
4 | ||
bbcdf86a FT |
5 | class page(lib.page): |
6 | def __init__(self, chapter, stack, n, url): | |
7 | self.stack = stack | |
8 | self.chapter = chapter | |
9 | self.manga = chapter.manga | |
10 | self.n = n | |
11 | self.id = str(n) | |
12 | self.name = u"Page %s" % n | |
13 | self.url = url | |
14 | self.ciurl = None | |
15 | ||
16 | def iurl(self): | |
17 | if self.ciurl is None: | |
18 | page = soup(htcache.fetch(self.url)) | |
19 | self.ciurl = page.find("div", id="imgholder").find("img", id="img")["src"].encode("us-ascii") | |
20 | return self.ciurl | |
21 | ||
22 | def open(self): | |
b9e558ac | 23 | return lib.stdimgstream(self.iurl()) |
bbcdf86a FT |
24 | |
25 | def __str__(self): | |
26 | return self.name | |
27 | ||
28 | def __repr__(self): | |
29 | return "<mrnet.page %r.%r.%r>" % (self.manga.name, self.chapter.name, self.name) | |
30 | ||
31 | class chapter(lib.pagelist): | |
32 | def __init__(self, manga, stack, id, name, url): | |
33 | self.stack = stack | |
34 | self.manga = manga | |
35 | self.id = id | |
36 | self.name = name | |
37 | self.url = url | |
38 | self.cpag = None | |
39 | ||
40 | def __getitem__(self, i): | |
41 | return self.pages()[i] | |
42 | ||
43 | def __len__(self): | |
44 | return len(self.pages()) | |
45 | ||
46 | def pages(self): | |
47 | if self.cpag is None: | |
48 | pg = soup(htcache.fetch(self.url)) | |
49 | pag = [] | |
50 | for opt in pg.find("div", id="selectpage").find("select", id="pageMenu").findAll("option"): | |
51 | url = urlparse.urljoin(self.url, opt["value"].encode("us-ascii")) | |
52 | n = int(opt.string) | |
53 | pag.append(page(self, self.stack + [(self, len(pag))], n, url)) | |
54 | self.cpag = pag | |
55 | return self.cpag | |
56 | ||
57 | def __str__(self): | |
58 | return self.name | |
59 | ||
60 | def __repr__(self): | |
61 | return "<mrnet.chapter %r.%r>" % (self.manga.name, self.name) | |
62 | ||
63 | class manga(lib.manga): | |
64 | def __init__(self, lib, id, name, url): | |
65 | self.lib = lib | |
66 | self.id = id | |
67 | self.name = name | |
68 | self.url = url | |
69 | self.cch = None | |
70 | self.stack = [] | |
71 | ||
72 | def __getitem__(self, i): | |
73 | return self.ch()[i] | |
74 | ||
75 | def __len__(self): | |
76 | return len(self.ch()) | |
77 | ||
78 | def ch(self): | |
79 | if self.cch is None: | |
80 | page = soup(htcache.fetch(self.url)) | |
81 | cls = page.find("div", id="chapterlist").find("table", id="listing") | |
82 | i = 0 | |
83 | cch = [] | |
84 | for tr in cls.findAll("tr"): | |
85 | td = tr.find("td") | |
86 | if td is None: continue | |
87 | cla = td.find("a") | |
88 | url = urlparse.urljoin(self.url, cla["href"].encode("us-ascii")) | |
89 | name = cla.string | |
90 | cid = name.encode("utf8") | |
91 | if isinstance(cla.nextSibling, unicode): | |
92 | ncont = unicode(cla.nextSibling) | |
68143b85 | 93 | if len(ncont) > 3 and ncont[:3] == u" : ": |
bbcdf86a FT |
94 | name += u": " + ncont[3:] |
95 | cch.append(chapter(self, [(self, len(cch))], cid, name, url)) | |
96 | self.cch = cch | |
97 | return self.cch | |
98 | ||
99 | def __str__(self): | |
100 | return self.name | |
101 | ||
102 | def __repr__(self): | |
103 | return "<mrnet.manga %r>" % self.name | |
104 | ||
105 | class library(lib.library): | |
106 | def __init__(self): | |
107 | self.base = "http://www.mangareader.net/" | |
108 | ||
109 | def byid(self, id): | |
110 | url = self.base + id | |
111 | page = soup(htcache.fetch(url)) | |
112 | if page.find("h2", attrs={"class": "aname"}) is None: | |
113 | raise KeyError(id) | |
114 | name = page.find("h2", attrs={"class": "aname"}).string | |
115 | return manga(self, id, name, url) | |
68143b85 FT |
116 | |
117 | def __iter__(self): | |
118 | page = soup(htcache.fetch(self.base + "alphabetical")) | |
119 | for sec in page.findAll("div", attrs={"class": "series_alpha"}): | |
120 | for li in sec.find("ul", attrs={"class": "series_alpha"}).findAll("li"): | |
121 | url = li.a["href"].encode("us-ascii") | |
122 | name = li.a.string | |
123 | if url[:1] != "/": continue | |
124 | id = url[1:] | |
125 | if '/' in id: | |
126 | # Does this distinction mean something? | |
127 | id = id[id.rindex('/') + 1:] | |
128 | if id[-5:] != ".html": | |
129 | continue | |
130 | id = id[:-5] | |
131 | yield manga(self, id, name, urlparse.urljoin(self.base, url)) | |
132 | ||
133 | def byname(self, prefix): | |
134 | if not isinstance(prefix, unicode): | |
135 | prefix = prefix.decode("utf8") | |
136 | prefix = prefix.lower() | |
137 | for manga in self: | |
138 | if manga.name.lower()[:len(prefix)] == prefix: | |
139 | yield manga | |
ffd12e71 FT |
140 | |
141 | def search(self, expr): | |
142 | if not isinstance(expr, unicode): | |
143 | expr = expr.decode("utf8") | |
144 | expr = expr.lower() | |
145 | for manga in self: | |
146 | if expr in manga.name.lower(): | |
147 | yield manga |