support replying on topics

This commit is contained in:
Romain Bignon 2011-08-25 22:00:55 +02:00
commit 9c54ca4b92
4 changed files with 49 additions and 10 deletions

View file

@ -24,7 +24,7 @@ from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.newsfeed import Newsfeed from weboob.tools.newsfeed import Newsfeed
from weboob.tools.value import Value, ValueInt, ValueBackendPassword from weboob.tools.value import Value, ValueInt, ValueBackendPassword
from weboob.tools.misc import limit from weboob.tools.misc import limit
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread, CantSendMessage from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread
from .browser import PhpBB from .browser import PhpBB
from .tools import rssid, url2id, id2url from .tools import rssid, url2id, id2url
@ -33,7 +33,7 @@ from .tools import rssid, url2id, id2url
__all__ = ['PhpBBBackend'] __all__ = ['PhpBBBackend']
class PhpBBBackend(BaseBackend, ICapMessages): class PhpBBBackend(BaseBackend, ICapMessages, ICapMessagesPost):
NAME = 'phpbb' NAME = 'phpbb'
MAINTAINER = 'Romain Bignon' MAINTAINER = 'Romain Bignon'
EMAIL = 'romain@weboob.org' EMAIL = 'romain@weboob.org'
@ -179,7 +179,7 @@ class PhpBBBackend(BaseBackend, ICapMessages):
assert message.thread assert message.thread
with self.browser: with self.browser:
return self.browser.post_answer(message.thread.id, return self.browser.post_answer(message.thread.id if message.thread else 0,
message.title, message.title,
message.content) message.content)

View file

@ -25,7 +25,7 @@ from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from weboob.capabilities.messages import CantSendMessage from weboob.capabilities.messages import CantSendMessage
from .pages.index import LoginPage from .pages.index import LoginPage
from .pages.forum import ForumPage, TopicPage from .pages.forum import ForumPage, TopicPage, PostingPage
from .tools import id2url, url2id from .tools import id2url, url2id
# Browser # Browser
@ -35,6 +35,7 @@ class PhpBB(BaseBrowser):
'https?://.*/viewforum.php\?f=(\d+)': ForumPage, 'https?://.*/viewforum.php\?f=(\d+)': ForumPage,
'https?://.*/search.php\?.*': ForumPage, 'https?://.*/search.php\?.*': ForumPage,
'https?://.*/viewtopic.php\?.*': TopicPage, 'https?://.*/viewtopic.php\?.*': TopicPage,
'https?://.*/posting.php\?.*': PostingPage,
'https?://.*/ucp.php\?mode=login.*': LoginPage, 'https?://.*/ucp.php\?mode=login.*': LoginPage,
} }
@ -92,7 +93,7 @@ class PhpBB(BaseBrowser):
parent = 0 parent = 0
while 1: while 1:
for post in self.page.iter_posts(): for post in self.page.iter_posts():
if post.id == stop_id: if post.id >= stop_id:
return return
post.parent = parent post.parent = parent
@ -116,7 +117,7 @@ class PhpBB(BaseBrowser):
if child: if child:
child.parent = post.id child.parent = post.id
yield child yield child
if post.id == stop_id: if post.id <= stop_id:
return return
child = post child = post
@ -145,4 +146,18 @@ class PhpBB(BaseBrowser):
return post return post
def post_answer(self, topic, title, content): def post_answer(self, topic, title, content):
pass if topic == 0:
raise CantSendMessage('Unable to create new topic for now')
else:
self.location('%s/%s' % (self.BASEPATH, id2url(topic)))
assert self.is_on_page(TopicPage)
self.page.go_reply()
assert self.is_on_page(PostingPage)
self.page.post(title, content)
assert self.is_on_page(PostingPage)
error = self.page.get_error_message()
if error:
raise CantSendMessage('Unable to send message: %s' % error)

View file

@ -26,7 +26,7 @@ from .index import PhpBBPage
from ..tools import parse_date from ..tools import parse_date
__all__ = ['Link', 'ForumPage', 'TopicPage'] __all__ = ['Link', 'ForumPage', 'TopicPage', 'PostingPage']
class Link(object): class Link(object):
@ -96,6 +96,9 @@ class TopicPage(PhpBBPage):
text = text[:20] + u'' text = text[:20] + u''
self.forum_title = '[%s] ' % text self.forum_title = '[%s] ' % text
def go_reply(self):
self.browser.follow_link(url_regex='posting\.php')
def next_page_url(self): def next_page_url(self):
try: try:
return self.parser.select(self.document.getroot(), 'a.right-box', 1).attrib['href'] return self.parser.select(self.document.getroot(), 'a.right-box', 1).attrib['href']
@ -186,3 +189,22 @@ class TopicPage(PhpBBPage):
id = int(div.attrib['id'][1:]) id = int(div.attrib['id'][1:])
return id return id
class PostingPage(PhpBBPage):
def post(self, title, content):
self.browser.select_form(predicate=lambda form: form.attrs.get('id', '') == 'postform')
self.browser.set_all_readonly(False)
if title:
self.browser['subject'] = title.encode('utf-8')
self.browser['message'] = content.encode('utf-8')
# This code on phpbb:
# if ($cancel || ($current_time - $lastclick < 2 && $submit))
# {
# /* ... */
# redirect($redirect);
# }
# To prevent that shit because weboob is too fast, we simulate
# a value of lastclick 10 seconds before.
self.browser['lastclick'] = str(int(self.browser['lastclick']) - 10)
self.browser.submit(name='post')

View file

@ -28,10 +28,12 @@ class PhpBBPage(BasePage):
links = self.document.getroot().cssselect('link[type="application/atom+xml"]') links = self.document.getroot().cssselect('link[type="application/atom+xml"]')
return links[-1].attrib['href'] return links[-1].attrib['href']
class LoginPage(PhpBBPage):
def get_error_message(self): def get_error_message(self):
errors = [] errors = []
for div in self.parser.select(self.document.getroot(), 'div.error'): for div in self.parser.select(self.document.getroot(), 'div.error,p.error'):
if div.text: if div.text:
errors.append(div.text.strip()) errors.append(div.text.strip())
return ', '.join(errors) return ', '.join(errors)
class LoginPage(PhpBBPage):
pass