-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpynada.py
116 lines (91 loc) · 3.3 KB
/
pynada.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import requests
import re
BASE_API_URL = "https://getnada.com/api/v1"
GET_DOMAINS_URL = BASE_API_URL + "/domains"
GET_INBOX_URL_TEMPLATE = BASE_API_URL + "/inboxes/{}"
GET_EMAIL_URL_TEMPLATE = BASE_API_URL + "/messages/{}"
DELETE_EMAIL_URL_TEMPLATE = BASE_API_URL + "/messages/{}"
class Pynada():
"""Main class for Pynada."""
def __init__(self):
self.domain_names = None
self.inboxes = None
def get_domains(self):
if self.domain_names == None:
r = requests.get(GET_DOMAINS_URL)
if r.status_code != 200:
raise ValueError("Can't get domains")
self.domain_names = [item["name"] for item in r.json()]
return self.domain_names
def is_valid_email(self, email):
return email[email.find("@")+1:] in self.get_domains()
def inbox(self, email):
if self.inboxes != None:
for ibx in self.inboxes:
if ibx.email == email:
return ibx
else:
self.inboxes = []
new_ibx = PynadaInbox(email)
self.inboxes.append(new_ibx)
return new_ibx
class PynadaInbox(object):
"""Inbox for an email address for PynadaInbox."""
def __init__(self, email):
self.inbox_email = email
self.emails = None
def get_emails(self):
if self.emails == None:
self.emails = []
r = requests.get(
GET_INBOX_URL_TEMPLATE.format(self.inbox_email))
if r.status_code != 200:
raise ValueError("Can't get inbox {}".format(self.inbox_email))
for email in r.json()["msgs"]:
self.emails.append(
PynadaEmail(
email['uid'],
email['f'],
email['fe'],
email['s'],
email['r']))
return self.emails
class PynadaEmail(object):
"""Email for PynadaEmail."""
def __init__(self, uid, from_name,
from_email, subject, timestamp):
self.uid = uid
self.from_name = from_name
self.from_email = from_email
self.subject = subject
self.timestamp = timestamp
self.contents = None
def get_contents(self):
if self.contents == None:
r = requests.get(
GET_EMAIL_URL_TEMPLATE.format(self.uid))
if r.status_code != 200:
raise ValueError("Can't get email contents : {}".format(self.uid))
self.contents = r.json()['html']
return self.contents
def delete(self):
r = requests.delete(
DELETE_EMAIL_URL_TEMPLATE.format(self.uid))
print r.text
print r.status_code
if r.status_code != 201:
raise ValueError("Couldn't delete email : {}".format(self.uid))
def main():
emails = Pynada().inbox("test@getnada.com").get_emails()
# contents = emails[0].get_contents()
# find instagram confirmation_email
# contents = re.search("https://instagram.com/accounts/confirm_email/[^\"]+", contents).group()
# contents = contents.replace("&", "&")
# print contents
# delete all emails in inbox
# for email in emails:
# email.delete()
if __name__ == '__main__':
main()