PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/PYopLib.py

https://bitbucket.org/y0no/pyopmail
Python | 78 lines | 68 code | 8 blank | 2 comment | 1 complexity | acdb77b485ecfdec22f54af82f7cd994 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. from os.path import join
  4. from bs4 import BeautifulSoup as bs4
  5. from bs4 import Comment
  6. class PYopmail(object):
  7. def __init__(self, username=None, url='http://www.yopmail.com', lang='en', version='2.4'):
  8. self.url = url
  9. self.lang = lang
  10. self.version = version
  11. self.username = username
  12. self.session = requests.session()
  13. def list_mails(self):
  14. payload = {'login': self.username, 'v': self.version}
  15. r = self.session.get(join(self.url, self.lang, 'inbox.php'), params=payload)
  16. return self._parse_list_mails(r.content)
  17. # To modify to catch title and date
  18. def _parse_list_mails(self, source):
  19. html = bs4(source)
  20. messages = html.find_all('a', class_='lm')
  21. return [self._parse_elem(i) for i in messages]
  22. def _parse_elem(self, elem):
  23. return elem['href'].replace('r=1&', '').replace('mail.php?id=', '')
  24. def get_mail(self, mail_id):
  25. payload = {'id': mail_id}
  26. r = self.session.get(join(self.url, self.lang, 'mail.php'), params=payload)
  27. return self._parse_get_mail(r.content)
  28. def _parse_get_mail(self, source):
  29. html = bs4(source)
  30. message = html.find('div', id='mailmillieu')
  31. message.script.decompose()
  32. message.span.decompose()
  33. #spans = message.find_all('span')
  34. #if spans is not None:
  35. # [span.decompose() for span in spans[-1]]
  36. return message
  37. def read_mail(self, mail_id):
  38. return self.get_mail(mail_id).get_text()
  39. def click_mail(self, mail_id, limit=None):
  40. links = get_links(mail_id)
  41. if links is not None:
  42. return [self.session.get(link) for link in links[:limit]]
  43. def click_last_mail(self, limit=None):
  44. links = self.get_last_mail_links()
  45. if links is not None:
  46. return [self.session.get(link) for link in links[:limit]]
  47. def get_links(self, mail_id):
  48. message = self.get_mail(mail_id)
  49. links = message.find_all('a')
  50. if links is not None:
  51. return [link.get('href') for link in links]
  52. def get_last_mail_links(self):
  53. last_mail_id = self.list_mails()[0]
  54. return self.get_links(last_mail_id)