PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/ovs/browser.py

https://gitlab.com/phyks/weboob
Python | 120 lines | 75 code | 28 blank | 17 comment | 5 complexity | 1336f82da9ab426d28045878418a3f6e MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Copyright(C) 2013 Vincent A
  3. #
  4. # This file is part of weboob.
  5. #
  6. # weboob is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # weboob is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
  18. from weboob.deprecated.browser import Browser, BrowserIncorrectPassword
  19. from weboob.deprecated.browser.parsers.iparser import IParser
  20. import BeautifulSoup
  21. from .pages import PagePrivateThreadsList, PagePrivateThread, PageLogin, PageIndex, DummyPage, PageUserProfile, PageCityList
  22. __all__ = ['OvsBrowser']
  23. class SoupParser(IParser):
  24. def parse(self, data, encoding=None):
  25. return BeautifulSoup.BeautifulSoup(data.read().decode(encoding or 'utf-8'), convertEntities=BeautifulSoup.BeautifulStoneSoup.ALL_ENTITIES)
  26. class OvsBrowser(Browser):
  27. PROTOCOL = 'http'
  28. DOMAIN = 'paris.onvasortir.com'
  29. ENCODING = 'cp1252'
  30. def __init__(self, city, username, password, *a, **kw):
  31. self.DOMAIN = '%s.onvasortir.com' % city
  32. self.PAGES = {
  33. '%s://%s/' % (self.PROTOCOL, self.DOMAIN): PageIndex,
  34. r'%s://%s/message_read.php\?Id=.+' % (self.PROTOCOL, self.DOMAIN): PagePrivateThread,
  35. '%s://%s/vue_messages_recus.php' % (self.PROTOCOL, self.DOMAIN): PagePrivateThreadsList,
  36. '%s://%s/vue_messages_envoyes.php' % (self.PROTOCOL, self.DOMAIN): PagePrivateThreadsList,
  37. '%s://%s/page_action_connect.php' % (self.PROTOCOL, self.DOMAIN): PageLogin,
  38. r'%s://%s/\?Langue=EN' % (self.PROTOCOL, self.DOMAIN): DummyPage,
  39. '%s://%s/page_action_boost.php' % (self.PROTOCOL, self.DOMAIN): DummyPage,
  40. '%s://%s/vue_profil_all.php.php' % (self.PROTOCOL, self.DOMAIN): DummyPage,
  41. r'%s://%s/message_msg_envoi_ok.php\?.*' % (self.PROTOCOL, self.DOMAIN): DummyPage,
  42. '%s://%s/message_action_envoi.php' % (self.PROTOCOL, self.DOMAIN): DummyPage,
  43. r'%s://%s/profil_read.php\?.+' % (self.PROTOCOL, self.DOMAIN): PageUserProfile,
  44. 'http://www.onvasortir.com/?': PageCityList,
  45. 'http://www.urbeez.com/?': PageCityList,
  46. }
  47. kw['parser'] = SoupParser()
  48. Browser.__init__(self, username, password, *a, **kw)
  49. self.city = city
  50. def iter_threads_list(self):
  51. self.location('/vue_messages_recus.php')
  52. assert self.is_on_page(PagePrivateThreadsList)
  53. for thread in self.page.iter_threads_list():
  54. yield thread
  55. self.location('/vue_messages_envoyes.php')
  56. assert self.is_on_page(PagePrivateThreadsList)
  57. for thread in self.page.iter_threads_list():
  58. yield thread
  59. def get_thread(self, _id):
  60. self.location('/message_read.php?Id=%s&AffMsg=all' % _id)
  61. assert self.is_on_page(PagePrivateThread)
  62. return self.page.get_thread(_id)
  63. def login(self):
  64. assert not self.is_logged()
  65. self.page.login(self.username, self.password)
  66. if not self.is_logged():
  67. raise BrowserIncorrectPassword()
  68. self.location('/?Langue=EN')
  69. self.location('/page_action_boost.php')
  70. self.location('/')
  71. def is_logged(self):
  72. return (self.is_on_page(DummyPage) or self.page.is_logged())
  73. def post_to_thread(self, thread_id, subject, body):
  74. self.location('/message_read.php?Id=%s' % thread_id.encode(self.ENCODING)) # FIXME
  75. assert self.is_on_page(PagePrivateThread)
  76. self.page.post_to_thread(thread_id, subject, body)
  77. def create_thread(self, recipient, subject, body):
  78. self.location('/profil_read.php?%s' % recipient.encode(self.ENCODING)) # FIXME
  79. assert self.is_on_page(PageUserProfile)
  80. self.page.create_thread(recipient, subject, body)
  81. def get_contact(self, id):
  82. self.location('/profil_read.php?%s' % id.encode(self.ENCODING)) # FIXME
  83. assert self.is_on_page(PageUserProfile)
  84. return self.page.get_contact()
  85. def get_french_cities(self):
  86. self.location('http://www.onvasortir.com')
  87. assert self.is_on_page(PageCityList)
  88. return self.page.get_cities('onvasortir.com')
  89. def get_world_cities(self):
  90. self.location('http://www.urbeez.com')
  91. assert self.is_on_page(PageCityList)
  92. return self.page.get_cities('urbeez.com')