-
Notifications
You must be signed in to change notification settings - Fork 1
/
tenminutesemail.py
83 lines (70 loc) · 2.61 KB
/
tenminutesemail.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
from datetime import datetime, timedelta
from requests import get
_HEADERS = {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'TE': 'Trailers'
}
class TenMinutesEmail:
_endpoint = "https://10minutemail.com/10MinuteMail/resources/session/{}".format
_msg_endpoint = "https://10minutemail.com/10MinuteMail/resources/messages/messagesAfter/{}".format
total_messages = 0
def __init__(self):
self.created_at = datetime.now()
self.updated_at = datetime.now()
self.messages = []
r = get("https://10minutemail.com/10MinuteMail/index.html",
headers=_HEADERS)
self.cookies = r.cookies
r = get(self._endpoint("address"),
cookies=self.cookies,
headers=_HEADERS)
self.address = r.text
def __str__(self):
return self.address
def __repr__(self):
return "({}, seconds_left={})".format(self.address,
self.seconds_left())
def is_alive(self, check_server=False):
if check_server:
r = get(self._endpoint("expired"),
cookies=self.cookies,
headers=_HEADERS)
return r == "true"
else:
return (datetime.now() - self.updated_at) < timedelta(minutes=10)
def reset_time(self):
assert self.is_alive()
self.updated_at = datetime.now()
get(self._endpoint("reset"), cookies=self.cookies, headers=_HEADERS)
def seconds_left(self, check_server=False):
if check_server:
r = get(self._endpoint("secondsLeft"),
cookies=self.cookies,
headers=_HEADERS)
return int(r.text) % 600
else:
if self.is_alive():
return int(600 -
(datetime.now() - self.updated_at).total_seconds())
else:
return 0
def get_messages(self):
while True:
message = get(self._msg_endpoint(self.total_messages),
cookies=self.cookies,
headers=_HEADERS)
message = message.json()
if message:
self.messages.append(message[0])
self.total_messages += 1
else:
break
return self.messages