-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangapanda_dav_provider.py
295 lines (251 loc) · 9.87 KB
/
mangapanda_dav_provider.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
Proxy mangapanda.me as virtual DAV
"""
import urllib2
import re
from BeautifulSoup import BeautifulSoup
from collections import OrderedDict
from wsgidav.util import joinUri
from wsgidav.dav_provider import DAVProvider, DAVNonCollection, DAVCollection
from wsgidav.dav_error import DAVError, HTTP_FORBIDDEN, HTTP_INTERNAL_ERROR,\
PRECONDITION_CODE_ProtectedProperty
from wsgidav import util
__docformat__ = "reStructuredText"
_logger = util.getModuleLogger(__name__)
from util import _dircache
_last_path = None
ROOT_URL = "http://www.mangapanda.com"
req_headers = {
"User-Agent": "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3",
}
PTN_NAV = re.compile('"/popular/[^"]*/(\d+)"')
PTN_MAXPG = re.compile("of (\d+)")
PTN_IMGURL = re.compile('<img[^>]*src="([^"]*)"')
AddImgExt = True
#===============================================================================
# Browising
#===============================================================================
class RootCollection(DAVCollection):
"""Resolve top-level requests '/'."""
def __init__(self, environ):
DAVCollection.__init__(self, "/", environ)
def getMemberNames(self):
return ["by_genre", ]
def getMember(self, name):
# Handle visible categories and also /by_key/...
if name == "by_genre":
return GenreCollection(joinUri(self.path, name), self.environ)
_logger.error("unexpected member name, "+name)
return None
class GenreCollection(DAVCollection):
""" hardcoded without parsing site"""
def __init__(self, path, environ):
DAVCollection.__init__(self, path, environ)
self.genres = [
'Action', 'Adventure', 'Comedy', 'Demons', 'Drama',
'Ecchi', 'Fantasy', 'Gender Bender', 'Harem', 'Historical',
'Horror', 'Josei', 'Magic', 'Martial Arts', 'Mature',
'Mecha', 'Military', 'Mystery', 'One Shot', 'Psychological',
'Romance', 'School Life', 'Sci-Fi', 'Seinen', 'Shoujo',
'Shoujoai', 'Shounen', 'Shounenai', 'Slice of Life', 'Smut',
'Sports', 'Super Power', 'Supernatural', 'Tragedy', 'Vampire',
'Yaoi', 'Yuri',
]
def getDisplayInfo(self):
return {"type": "Directory"}
def getMemberNames(self):
return self.genres
def getMember(self, name):
if name in self.genres:
genre = name.lower().replace(' ','-')
return DirectoryPageCollection(joinUri(self.path, name), self.environ, genre)
_logger.error("unexpected member name, "+name)
return None
#===============================================================================
# Directory w/ pages
#===============================================================================
class DirectoryPageCollection(DAVCollection):
""" site: /popular/{genre}"""
def __init__(self, path, environ, genre):
DAVCollection.__init__(self, path, environ)
self.genre = genre
self.abspath = self.provider.sharePath + path
try:
self.maxidx = _dircache[self.abspath]
except KeyError:
self.maxidx = 0
def getDisplayInfo(self):
return {"type": "Pages"}
def getMemberNames(self):
if not self.maxidx:
url = ROOT_URL + "/popular/%s" % self.genre
_logger.debug(url)
html = urllib2.urlopen(url).read()
navurls = PTN_NAV.findall(html)
_logger.debug("page buttons %d" % len(navurls))
self.maxidx = int(navurls[-1])
_dircache[self.abspath] = self.maxidx
return map(str, range(0, self.maxidx+1, 30))
def getMember(self, name):
if name.isdigit():
startid = int(name)
url = ROOT_URL + "/popular/%s/%d" % (self.genre, startid)
return DirectoryCollection(joinUri(self.path, name), self.environ, url)
_logger.error("unexpected member name, "+name)
return None
class DirectoryCollection(DAVCollection):
""" site: /popular/{genre}/{startnum}"""
def __init__(self, path, environ, url):
DAVCollection.__init__(self, path, environ)
self.url = url
self.abspath = self.provider.sharePath + path
try:
self.mangas = _dircache[self.abspath]
except KeyError:
self.mangas = None
def getDisplayInfo(self):
return {"type": "List"}
def getMemberNames(self):
if self.mangas is None:
self.extractInfo()
return self.mangas.keys()
def getMember(self, name):
if self.mangas is None:
self.extractInfo()
try:
url = ROOT_URL + "/" + self.mangas[name]
return MangaCollection(joinUri(self.path, name), self.environ, url)
except:
return None
def extractInfo(self):
_logger.debug(self.url)
html = urllib2.urlopen(self.url).read()
soup = BeautifulSoup(html)
self.mangas = OrderedDict()
for node in soup.findAll('h3'):
title = str(node.a.string)
mid = node.a.get('href')[1:]
self.mangas[title] = mid
_dircache[self.abspath] = self.mangas
#===============================================================================
# Manga / Chapter
#===============================================================================
class MangaCollection(DAVCollection):
""" site: /{manga}"""
def __init__(self, path, environ, url):
DAVCollection.__init__(self, path, environ)
self.url = url
self.abspath = self.provider.sharePath + path
try:
self.chapters = _dircache[self.abspath]
except KeyError:
self.chapters = None
def getDisplayInfo(self):
return {"type": "Manga"}
def getMemberNames(self):
if self.chapters is None:
self.extractInfo()
return self.chapters
def getMember(self, name):
if name.isdigit():
url = self.url + "/" + name
return ChapterCollection(joinUri(self.path, name), self.environ, url)
_logger.error("unexpected chapter name, "+name)
return None
def extractInfo(self):
_logger.debug(self.url)
html = urllib2.urlopen(self.url).read()
soup = BeautifulSoup(html)
self.chapters = []
for node in soup.find('table',{'id':'listing'}).findAll('a'):
url = str(node.get('href'))
self.chapters.append( url.split('/')[-1] )
_dircache[self.abspath] = self.chapters
class ChapterCollection(DAVCollection):
""" site: /{manga}/{chapter_num}"""
def __init__(self, path, environ, url):
DAVCollection.__init__(self, path, environ)
self.url = url
self.abspath = self.provider.sharePath + path
try:
self.maxpg = _dircache[self.abspath]
except KeyError:
self.maxpg = 0
def getDisplayInfo(self):
return {"type": "Chapter"}
def getMemberNames(self):
if not self.maxpg:
_logger.debug(self.url)
html = urllib2.urlopen(self.url).read()
soup = BeautifulSoup(html)
node = soup.find('div',{'id':'selectpage'})
if node is None:
_logger.error("no page info found")
return []
maxpg_text = str(node)
match = PTN_MAXPG.search(maxpg_text)
if not match:
_logger.error("no max page found")
return []
self.maxpg = int(match.group(1))
_dircache[self.abspath] = self.maxpg
if AddImgExt:
return ["%d.jpg" % pg for pg in range(1, self.maxpg+1)]
return map(str, range(1, self.maxpg+1))
def getMember(self, name):
url = self.url + "/" + name.rsplit(".", 1)[0] # =splitext()
return ImageFile(joinUri(self.path, name), self.environ, url)
#===============================================================================
# ImageFile
#===============================================================================
class ImageFile(DAVNonCollection):
""" site: /{manga}/{chapter_num}/{page_num}"""
def __init__(self, path, environ, url):
DAVNonCollection.__init__(self, path, environ)
self.url = url
def getContentLength(self):
return None
def getContentType(self):
return util.guessMimeType(self.path)
def getCreationDate(self):
return None
def getDisplayName(self):
return self.name
def getDisplayInfo(self):
return {"type": "Image file"}
def getEtag(self):
return None
def getLastModified(self):
return None
def supportRanges(self):
return False
def getContent(self):
_logger.debug(self.url)
html = urllib2.urlopen(self.url).read()
match = PTN_IMGURL.search(html)
if match:
url = match.group(1)
_logger.debug(url)
req = urllib2.Request(url, headers=req_headers)
return urllib2.urlopen(req)
_logger.warning("no image found at "+self.url)
return None
#===============================================================================
# DAVProvider
#===============================================================================
class MangapandaProvider(DAVProvider):
def __init__(self):
super(MangapandaProvider, self).__init__()
def getResourceInst(self, path, environ):
_logger.info("getResourceInst('%s')" % path)
self._count_getResourceInst += 1
global _last_path
npath = self.sharePath + path
if _last_path == npath:
global _dircache
#del _dircache[npath]
_dircache.__delete__(npath)
_last_path = npath
root = RootCollection(environ)
return root.resolve("", path)