Skip to content

Commit 8b99820

Browse files
Update the Python API to use the new API
This update adds the functionality for custom domains and specific domains. The library may have some incompatibilities with the old one, so a disclaimer was added to the README file.
1 parent 524a46b commit 8b99820

File tree

5 files changed

+150
-72
lines changed

5 files changed

+150
-72
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ __pychache__/
22
TempMail/__pycache__/
33
build/
44
dist/
5-
*.egg-info/
5+
*.egg-info/
6+
7+
.idea
8+
*.iml
9+
venv

README.md

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,49 @@
55

66
This repository is for the [TempMail.lol](https://tempmail.lol/) Python API.
77

8+
## Updating form v1
9+
The library is different from version 1.x.x. Please see Usage to learn more about the changes to
10+
the Python library.
11+
812
## Installation
913

10-
Use this command in terminal.
14+
You can install the TempMail API using PIP:
1115
```
1216
pip install tempmail-lol
1317
```
1418

19+
## TempMail Plus (optional)
20+
21+
Optionally, you can purchase time on a BananaCrumbs ID to get TempMail Plus. This includes higher rate limits, as well
22+
as some other features. For more info, see this page: https://tempmail.lol/pricing.html
23+
1524
## Usage
1625
```python
17-
from TempMail import TempMail #imports everything from TempMail library
18-
import time #import time module
19-
20-
inbox = TempMail.generateInbox() #returns an Inbox object with Email and Token
21-
22-
print("Email Address: "+ inbox.address) #View output below
23-
print("Authorization Token: "+ inbox.token)
24-
25-
#output:
26-
"""
27-
Email Address: m8h69n52824315@theeyeoftruth.com
28-
Authorization Token: RCfc1og1z1JzuN1mkXL2eFdAc_8uxSRAwcGhUoXuH26e7nnJMdVVtSxxasZLD9D2OHTKIjVEvLhK7S0K5QIanA
29-
"""
30-
31-
while(True): #Infinite Loop
32-
emails = TempMail.getEmails(inbox) #Returns list of Email objects
33-
print(emails) # View output below
34-
time.sleep(30) #wait 30 sec
35-
36-
#output:
37-
"""
38-
[Email (sender=ExampleEmail@gmail.com, recipient=d6inmp52824914@magicaljellyfish.com,
39-
subject=Subject line, body=Text Area, html=<div dir="ltr">Text Area</div>,
40-
date=1652824961713 )]
41-
"""
26+
from TempMail import TempMail
27+
28+
# Create a new TempMail object
29+
tmp = TempMail()
30+
31+
# If you have a BananaCrumbs ID, you can login using the constructor
32+
tmp = TempMail("24 number ID", "32 or 36 character token")
33+
34+
# Generate an inbox
35+
inb = TempMail.generateInbox(tmp)
36+
37+
# Generate an inbox using Community (formerly Rush) domains
38+
inb = TempMail.generateInbox(tmp, rush=True)
39+
40+
# Generate an inbox using a specific normal/community domain
41+
inb = TempMail.generateInbox(tmp, rush=False, domain="cringemonster.com")
42+
43+
# Check for emails
44+
emails = TempMail.getEmails(tmp, inbox=inb)
45+
46+
# Check custom domains (requires TempMail Plus)
47+
custom_domain_emails = TempMail.checkCustomInbox(tmp, "example.com", "token given on website")
4248
```
49+
50+
## Custom Domain Keys
51+
Note that the token for custom inboxes is stored on your domain as a text record with a name of `_tmpml` and a sha512 hash.
52+
The token that you submit is the text pre-sha512. This helps disconnect a user's BananaCrumbs ID and the domain he/she owns.
53+

TempMail/Email.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Email class is used to store the email data which consists of
2+
Email class is used to store the email data which consists of
33
the sender, recipient, subject, body, html and date
44
"""
55

TempMail/TempMail.py

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,130 @@
33
from TempMail.Email import Email
44
from TempMail.Inbox import Inbox
55

6+
67
class TempMail:
78
global BASE_URL
89
BASE_URL = "https://api.tempmail.lol"
9-
10-
10+
11+
# class vars
12+
auth_id = None
13+
auth_token = None
14+
15+
# constructor
16+
def __init__(self, auth_id=None, auth_token=None):
17+
TempMail.auth_id = auth_id
18+
TempMail.auth_token = auth_token
19+
1120
"""
1221
Make a request to the tempmail.lol api with a given endpoint
1322
The content of the request is a json string and is returned as a string object
1423
"""
15-
16-
def makeHTTPRequest(endpoint):
24+
25+
def makeHTTPRequest(self, endpoint):
1726
headers = {
1827
"User-Agent": "TempMailPythonAPI/1.0",
1928
"Accept": "application/json"
2029
}
21-
try:
22-
connection = requests.get(BASE_URL + endpoint, headers=headers)
23-
if connection.status_code >= 400:
24-
raise Exception("HTTP Error: " + str(connection.status_code))
25-
except Exception as e:
26-
print(e)
27-
return None
30+
31+
if TempMail.auth_id is not None and TempMail.auth_token is not None:
32+
headers["X-BananaCrumbs-ID"] = TempMail.auth_id
33+
headers["X-BananaCrumbs-MFA"] = TempMail.auth_token
34+
35+
connection = requests.get(BASE_URL + endpoint, headers=headers)
36+
37+
# Check some error codes
38+
# This includes rate limits, auth errors, and server errors
39+
if connection.status_code == 429: # Rate limit
40+
raise Exception("TempMail Rate Limit: " + connection.text)
41+
elif connection.status_code == 402: # No time left on account
42+
raise Exception("BananaCrumbs ID has no time left. See https://tempmail.lol/pricing.html for more info")
43+
elif 400 <= connection.status_code < 500: # Client error
44+
raise Exception("HTTP Error: " + str(connection.status_code))
45+
elif 500 <= connection.status_code < 600: # Server error
46+
raise Exception("TempMail Server returned an error: " + str(
47+
connection.status_code) + " " + connection.text + " please report this.")
2848

2949
response = connection.text
30-
50+
3151
return response
3252

3353
"""
3454
GenerateInbox will generate an inbox with an address and a token
3555
and returns an Inbox object
3656
> rush = False will generate a normal inbox with no rush (https://tempmail.lol/news/2022/08/03/introducing-rush-mode-for-tempmail/)
57+
> domain = None will generate an inbox with a random domain
3758
"""
38-
def generateInbox(rush = False):
39-
try :
40-
s = TempMail.makeHTTPRequest("/generate" + ("/rush" if rush else ""))
41-
except:
42-
print("Website responded with: "+ s)
59+
60+
def generateInbox(self, rush=False, domain=None):
61+
url = "/generate"
62+
# with rush mode: /generate/rush
63+
# with domain: /generate/<domain>
64+
# these two cannot be combined. If both are true, only rush will be used
65+
66+
if rush:
67+
url = url + "/rush"
68+
elif domain is not None:
69+
url = url + "/" + domain
70+
71+
s = TempMail.makeHTTPRequest(self, url)
4372
data = json.loads(s)
4473
return Inbox(data["address"], data["token"])
45-
4674

4775
"""
4876
getEmail gets the emails from an inbox object
4977
and returns a list of Email objects
5078
"""
51-
def getEmails(inbox):
52-
s = TempMail.makeHTTPRequest("/auth/" + inbox.token)
79+
80+
def getEmails(self, inbox):
81+
# if inbox is an instance of Inbox object, get the token, otherwise inbox is a string
82+
if isinstance(inbox, Inbox):
83+
token = inbox.token
84+
else:
85+
token = inbox
86+
87+
s = TempMail.makeHTTPRequest(self, "/auth/" + token)
5388
data = json.loads(s)
5489

55-
#Raise an exception if the token is invalid
90+
# Raise an exception if the token is invalid
5691
if "token" in s and "token" in data:
5792
if data["token"] == "invalid":
5893
raise Exception("Invalid Token")
5994

60-
#if no emails are found, return an empty list
61-
#else return a list of email
62-
if data["email"] == None:
95+
# if no emails are found, return an empty list
96+
# else return a list of email
97+
if data["email"] is None:
6398
return ["None"]
6499
else:
65100
emails = []
66101
for email in data["email"]:
67-
emails.append(Email(email["from"], email["to"], email["subject"], email["body"], email["html"], email["date"]))
102+
# Some emails may not have html, so we will check for that
103+
if "html" in email:
104+
emails.append(
105+
Email(email["from"], email["to"], email["subject"], email["body"], email["html"], email["date"]))
106+
else:
107+
emails.append(
108+
Email(email["from"], email["to"], email["subject"], email["body"], None, email["date"]))
109+
return emails
110+
111+
"""
112+
checkCustomInbox checks if there are any emails in a custom inbox
113+
and returns a list of Email objects
114+
> domain Required
115+
> token Required
116+
"""
117+
def checkCustomInbox(self, domain, token):
118+
url = "/custom/" + token + "/" + domain
119+
120+
s = TempMail.makeHTTPRequest(self, url)
121+
data = json.loads(s)
122+
123+
# There is no way to check if the token is invalid
124+
# so we will just return an empty list if there are no emails
125+
if data["email"] is None:
126+
return []
127+
else:
128+
emails = []
129+
for email in data["email"]:
130+
emails.append(
131+
Email(email["from"], email["to"], email["subject"], email["body"], email["html"], email["date"]))
68132
return emails
69-

setup.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
here = os.path.abspath(os.path.dirname(__file__))
66

77
with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
8-
long_description = "\n" + fh.read()
8+
long_description = "\n" + fh.read()
99

1010
setup(name='tempmail-lol',
11-
version='1.1.0',
12-
description='A Python API for TempMail',
13-
author='Alex Torres',
14-
author_email='cloudbotsedc@gmail.com',
15-
url='https://github.com/tempmail-lol/api-python',
16-
packages=find_packages(),
17-
install_requires=['requests'],
18-
keywords=['python', 'video', 'api', 'tempmail'],
19-
classifiers=[
20-
"Development Status :: 1 - Planning",
21-
"Intended Audience :: Developers",
22-
"Programming Language :: Python :: 3",
23-
"Operating System :: Unix",
24-
"Operating System :: MacOS :: MacOS X",
25-
"Operating System :: Microsoft :: Windows",
26-
]
27-
)
11+
version='2.0.0',
12+
description='A Python API for TempMail',
13+
author='Alex Torres, Alexander Epolite',
14+
author_email='cloudbotsedc@gmail.com',
15+
url='https://github.com/tempmail-lol/api-python',
16+
packages=find_packages(),
17+
install_requires=['requests'],
18+
keywords=['tempmail', 'api', 'lol', 'tempmail-lol', 'tempmail.lol', 'email', 'free'],
19+
classifiers=[
20+
"Development Status :: 1 - Planning",
21+
"Intended Audience :: Developers",
22+
"Programming Language :: Python :: 3",
23+
"Operating System :: Unix",
24+
"Operating System :: MacOS :: MacOS X",
25+
"Operating System :: Microsoft :: Windows",
26+
]
27+
)

0 commit comments

Comments
 (0)