more flexible code
This commit is contained in:
parent
2184951c4a
commit
c0773c1f92
4 changed files with 69 additions and 22 deletions
|
|
@ -41,17 +41,21 @@ class PhpBBBackend(BaseBackend, ICapMessages):
|
||||||
LICENSE = 'AGPLv3+'
|
LICENSE = 'AGPLv3+'
|
||||||
DESCRIPTION = "phpBB forum"
|
DESCRIPTION = "phpBB forum"
|
||||||
CONFIG = BackendConfig(Value('url', label='URL of forum', regexp='https?://.*'),
|
CONFIG = BackendConfig(Value('url', label='URL of forum', regexp='https?://.*'),
|
||||||
Value('username', label='Username'),
|
Value('username', label='Username', default=''),
|
||||||
ValueBackendPassword('password', label='Password'),
|
ValueBackendPassword('password', label='Password', default=''),
|
||||||
ValueInt('thread_unread_messages', label='Limit number of unread messages to retrieve for a thread', default=500)
|
ValueInt('thread_unread_messages', label='Limit number of unread messages to retrieve for a thread', default=500)
|
||||||
)
|
)
|
||||||
STORAGE = {'seen': {}}
|
STORAGE = {'seen': {}}
|
||||||
BROWSER = PhpBB
|
BROWSER = PhpBB
|
||||||
|
|
||||||
def create_default_browser(self):
|
def create_default_browser(self):
|
||||||
|
username = self.config['username'].get()
|
||||||
|
if len(username) > 0:
|
||||||
|
password = self.config['password'].get()
|
||||||
|
else:
|
||||||
|
password = None
|
||||||
return self.create_browser(self.config['url'].get(),
|
return self.create_browser(self.config['url'].get(),
|
||||||
self.config['username'].get(),
|
username, password)
|
||||||
self.config['password'].get())
|
|
||||||
|
|
||||||
#### ICapMessages ##############################################
|
#### ICapMessages ##############################################
|
||||||
|
|
||||||
|
|
@ -174,13 +178,13 @@ class PhpBBBackend(BaseBackend, ICapMessages):
|
||||||
return self.get_thread(thread)
|
return self.get_thread(thread)
|
||||||
|
|
||||||
#### ICapMessagesReply #########################################
|
#### ICapMessagesReply #########################################
|
||||||
#def post_message(self, message):
|
def post_message(self, message):
|
||||||
# assert message.thread
|
assert message.thread
|
||||||
|
|
||||||
# with self.browser:
|
with self.browser:
|
||||||
# return self.browser.post_comment(message.thread.id,
|
return self.browser.post_comment(message.thread.id,
|
||||||
# message.parent.id,
|
message.parent.id,
|
||||||
# message.title,
|
message.title,
|
||||||
# message.content)
|
message.content)
|
||||||
|
|
||||||
OBJECTS = {Thread: fill_thread}
|
OBJECTS = {Thread: fill_thread}
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,10 @@ from .tools import id2url, url2id
|
||||||
# Browser
|
# Browser
|
||||||
class PhpBB(BaseBrowser):
|
class PhpBB(BaseBrowser):
|
||||||
PAGES = {'https?://.*/index.php': ForumPage,
|
PAGES = {'https?://.*/index.php': ForumPage,
|
||||||
|
'https?://.*/': ForumPage,
|
||||||
'https?://.*/viewforum.php\?f=(\d+)': ForumPage,
|
'https?://.*/viewforum.php\?f=(\d+)': ForumPage,
|
||||||
'https?://.*/viewtopic.php\?.*': TopicPage,
|
'https?://.*/search.php\?.*': ForumPage,
|
||||||
|
'https?://.*/viewtopic.php\?.*': TopicPage,
|
||||||
'https?://.*/ucp.php\?mode=login.*': LoginPage,
|
'https?://.*/ucp.php\?mode=login.*': LoginPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@
|
||||||
|
|
||||||
|
|
||||||
from urlparse import urlsplit, parse_qs
|
from urlparse import urlsplit, parse_qs
|
||||||
from datetime import datetime
|
|
||||||
|
from weboob.tools.browser import BrokenPageError
|
||||||
|
|
||||||
from .index import PhpBBPage
|
from .index import PhpBBPage
|
||||||
from ..tools import parse_date
|
from ..tools import parse_date
|
||||||
|
|
@ -51,7 +52,16 @@ class ForumPage(PhpBBPage):
|
||||||
title = li.cssselect('a.topictitle')[0]
|
title = li.cssselect('a.topictitle')[0]
|
||||||
link = Link(Link.TOPIC, title.attrib['href'])
|
link = Link(Link.TOPIC, title.attrib['href'])
|
||||||
link.title = title.text.strip()
|
link.title = title.text.strip()
|
||||||
link.date = parse_date(li.find('dl').find('dt').findall('a')[-1].tail.strip(u'» \r\n'))
|
for a in li.find('dl').find('dt').findall('a'):
|
||||||
|
for text in (a.text, a.tail):
|
||||||
|
if text is None:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
link.date = parse_date(text.strip(u'» \r\n'))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
# it only lists number of answers, so we add 1.
|
# it only lists number of answers, so we add 1.
|
||||||
link.nb_messages = int(li.cssselect('dd.posts')[0].text.strip()) + 1
|
link.nb_messages = int(li.cssselect('dd.posts')[0].text.strip()) + 1
|
||||||
yield link
|
yield link
|
||||||
|
|
@ -78,13 +88,13 @@ class TopicPage(PhpBBPage):
|
||||||
args = parse_qs(v.query)
|
args = parse_qs(v.query)
|
||||||
self.topic_id = int(args['t'][0])
|
self.topic_id = int(args['t'][0])
|
||||||
|
|
||||||
nav = self.parser.select(self.document.getroot(), 'li.icon-home', 1)
|
|
||||||
self.forum_title = u''
|
self.forum_title = u''
|
||||||
for a in nav.findall('a')[1:]:
|
nav = self.parser.select(self.document.getroot(), 'li.icon-home')
|
||||||
text = a.text.strip()
|
if len(nav) > 0:
|
||||||
|
text = nav[0].findall('a')[-1].text.strip()
|
||||||
if len(text) >= 20:
|
if len(text) >= 20:
|
||||||
text = text[:20] + u'…'
|
text = text[:20] + u'…'
|
||||||
self.forum_title = '[%s]' % text
|
self.forum_title = '[%s] ' % text
|
||||||
|
|
||||||
def next_page_url(self):
|
def next_page_url(self):
|
||||||
return self.document.getroot().cssselect('a.right-box')[0].attrib['href']
|
return self.document.getroot().cssselect('a.right-box')[0].attrib['href']
|
||||||
|
|
@ -116,9 +126,39 @@ class TopicPage(PhpBBPage):
|
||||||
|
|
||||||
id = div.attrib['id'][1:]
|
id = div.attrib['id'][1:]
|
||||||
post = Post(self.topic_id, id)
|
post = Post(self.topic_id, id)
|
||||||
post.title = '%s %s' % (self.forum_title, body.cssselect('h3 a')[0].text.strip())
|
|
||||||
post.author = profile.cssselect('dt a')[-1].text.strip()
|
title_tags = body.cssselect('h3 a')
|
||||||
post.date = parse_date(body.cssselect('p.author')[0].find('strong').tail.strip(u'» \n\r'))
|
if len(title_tags) == 0:
|
||||||
|
title_tags = self.document.getroot().cssselect('h2 a')
|
||||||
|
if len(title_tags) == 0:
|
||||||
|
title = u''
|
||||||
|
self.logger.warning('Unable to parse title')
|
||||||
|
else:
|
||||||
|
title = title_tags[0].text.strip()
|
||||||
|
|
||||||
|
post.title = self.forum_title + title
|
||||||
|
for a in profile.cssselect('dt a'):
|
||||||
|
if a.text:
|
||||||
|
post.author = a.text.strip()
|
||||||
|
|
||||||
|
p_tags = body.cssselect('p.author')
|
||||||
|
if len(p_tags) == 0:
|
||||||
|
p_tags = body.find('p')
|
||||||
|
if len(p_tags) == 0:
|
||||||
|
post.date = None
|
||||||
|
self.logger.warning('Unable to parse datetime')
|
||||||
|
else:
|
||||||
|
p = p_tags[0]
|
||||||
|
text = p.find('strong') and p.find('strong').tail
|
||||||
|
if not text:
|
||||||
|
text = p.text[4:]
|
||||||
|
|
||||||
|
text = text.strip(u'» \n\r')
|
||||||
|
try:
|
||||||
|
post.date = parse_date(text)
|
||||||
|
except ValueError:
|
||||||
|
self.logger.warning(u'Unable to parse datetime "%s"' % text)
|
||||||
|
|
||||||
post.content = self.parser.tostring(body.cssselect('div.content')[0])
|
post.content = self.parser.tostring(body.cssselect('div.content')[0])
|
||||||
|
|
||||||
signature = body.cssselect('div.signature')
|
signature = body.cssselect('div.signature')
|
||||||
|
|
|
||||||
|
|
@ -55,5 +55,6 @@ def parse_date(s):
|
||||||
.replace(u'Juin', 'Jun') \
|
.replace(u'Juin', 'Jun') \
|
||||||
.replace(u'Juil', 'Jul') \
|
.replace(u'Juil', 'Jul') \
|
||||||
.replace(u'Aoû', 'Aug') \
|
.replace(u'Aoû', 'Aug') \
|
||||||
|
.replace(u'Ao\xfbt', 'Aug') \
|
||||||
.replace(u'Déc', 'Dec')
|
.replace(u'Déc', 'Dec')
|
||||||
return local2utc(_parse_dt(s))
|
return local2utc(_parse_dt(s))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue