tempmail.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import string
  2. import random
  3. from hashlib import md5
  4. import requests
  5. class TempMail(object):
  6. """
  7. API Wrapper for service which provides temporary email address.
  8. :param login: (optional) login for email address.
  9. :param domain: (optional) domain (from current available)
  10. for email address.
  11. :param api_domain: (optional) domain for temp-mail api.
  12. Default value is ``api.temp-mail.ru``.
  13. """
  14. def __init__(self, login=None, domain=None, api_domain='api.temp-mail.ru'):
  15. self.login = login
  16. self.domain = domain
  17. self.api_domain = api_domain
  18. def __repr__(self):
  19. return u'<TempMail [{0}]>'.format(self.get_email_address())
  20. @property
  21. def available_domains(self):
  22. """
  23. Return list of available domains for use in email address.
  24. """
  25. if not hasattr(self, '_available_domains'):
  26. url = 'http://{0}/request/domains/format/json/'.format(
  27. self.api_domain)
  28. req = requests.get(url)
  29. domains = req.json()
  30. setattr(self, '_available_domains', domains)
  31. return self._available_domains
  32. def generate_login(self, min_length=6, max_length=10, digits=True):
  33. """
  34. Generate string for email address login with defined length and
  35. alphabet.
  36. :param min_length: (optional) min login length.
  37. Default value is ``6``.
  38. :param max_length: (optional) max login length.
  39. Default value is ``10``.
  40. :param digits: (optional) use digits in login generation.
  41. Default value is ``True``.
  42. """
  43. chars = string.ascii_lowercase
  44. if digits:
  45. chars += string.digits
  46. length = random.randint(min_length, max_length)
  47. return ''.join(random.choice(chars) for x in range(length))
  48. def get_email_address(self):
  49. """
  50. Return full email address from login and domain from params in class
  51. initialization or generate new.
  52. """
  53. if self.login is None:
  54. self.login = self.generate_login()
  55. available_domains = self.available_domains
  56. if self.domain is None:
  57. self.domain = random.choice(available_domains)
  58. elif self.domain not in available_domains:
  59. raise ValueError('Domain not found in available domains!')
  60. return u'{0}{1}'.format(self.login, self.domain)
  61. def get_hash(self, email):
  62. """
  63. Return md5 hash for given email address.
  64. :param email: email address for generate md5 hash.
  65. """
  66. return md5(email).hexdigest()
  67. def get_mailbox(self, email=None, email_hash=None):
  68. """
  69. Return list of emails in given email address
  70. or dict with `error` key if mail box is empty.
  71. :param email: (optional) email address.
  72. :param email_hash: (optional) md5 hash from email address.
  73. """
  74. if email is None:
  75. email = self.get_email_address()
  76. if email_hash is None:
  77. email_hash = self.get_hash(email)
  78. url = 'http://{0}/request/mail/id/{1}/format/json/'.format(
  79. self.api_domain, email_hash)
  80. req = requests.get(url)
  81. return req.json()