forked from Vinyzu/DiscordGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempmail.py
127 lines (102 loc) · 3.46 KB
/
tempmail.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
117
118
119
120
121
122
123
124
125
126
127
# source: https://github.com/tempmail-lol/api-python/tree/main/TempMail
import requests
import json
class TempMail:
global BASE_URL
BASE_URL = "https://api.tempmail.lol"
"""
Make a request to the tempmail.lol api with a given endpoint
The content of the request is a json string and is returned as a string object
"""
def makeHTTPRequest(endpoint):
headers = {
"User-Agent": "TempMailPythonAPI/1.0",
"Accept": "application/json"
}
try:
connection = requests.get(BASE_URL + endpoint, headers=headers)
if connection.status_code >= 400:
raise Exception("HTTP Error: " + str(connection.status_code))
except Exception as e:
print(e)
return None
response = connection.text
return response
"""
GenerateInbox will generate an inbox with an address and a token
and returns an Inbox object
> rush = False will generate a normal inbox with no rush (https://tempmail.lol/news/2022/08/03/introducing-rush-mode-for-tempmail/)
"""
def generateInbox(rush=False):
try:
s = TempMail.makeHTTPRequest(
"/generate" + ("/rush" if rush else ""))
except:
print("Website responded with: " + s)
data = json.loads(s)
return Inbox(data["address"], data["token"])
"""
getEmail gets the emails from an inbox object
and returns a list of Email objects
"""
def getEmails(inbox):
s = TempMail.makeHTTPRequest("/auth/" + inbox.token)
data = json.loads(s)
# Raise an exception if the token is invalid
if "token" in s:
if data["token"] == "invalid":
raise Exception("Invalid Token")
# if no emails are found, return an empty list
# else return a list of email
if data["email"] == None:
return ["None"]
else:
emails = []
for email in data["email"]:
emails.append(Email(
email["from"], email["to"], email["subject"], email["body"], email["html"], email["date"]))
return emails
class Email:
def __init__(self, sender, recipient, subject, body, html, date):
# make the propertys immutable using @property
self._sender = sender
self._recipient = recipient
self._subject = subject
self._body = body
self._html = html
self._date = date
@property
def sender(self):
return self._sender
@property
def recipient(self):
return self._recipient
@property
def subject(self):
return self._subject
@property
def body(self):
return self._body
@property
def html(self):
return self._html
@property
def date(self):
return self._date
def __repr__(self):
return ("Email (sender={}, recipient={}, subject={}, body={}, html={}, date={} )"
.format(self.sender, self.recipient, self.subject, self.body, self.html, self.date))
class Inbox:
def __init__(self, address, token):
# make the propertys immutable using @property
self._address = address
self._token = token
@property
def address(self):
return self._address
@property
def token(self):
return self._token
def __repr__(self):
return ("Inbox (address={}, token={} )"
.format(self.address, self.token))