|
3 | 3 | from TempMail.Email import Email
|
4 | 4 | from TempMail.Inbox import Inbox
|
5 | 5 |
|
| 6 | + |
6 | 7 | class TempMail:
|
7 | 8 | global BASE_URL
|
8 | 9 | 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 | + |
11 | 20 | """
|
12 | 21 | Make a request to the tempmail.lol api with a given endpoint
|
13 | 22 | The content of the request is a json string and is returned as a string object
|
14 | 23 | """
|
15 |
| - |
16 |
| - def makeHTTPRequest(endpoint): |
| 24 | + |
| 25 | + def makeHTTPRequest(self, endpoint): |
17 | 26 | headers = {
|
18 | 27 | "User-Agent": "TempMailPythonAPI/1.0",
|
19 | 28 | "Accept": "application/json"
|
20 | 29 | }
|
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.") |
28 | 48 |
|
29 | 49 | response = connection.text
|
30 |
| - |
| 50 | + |
31 | 51 | return response
|
32 | 52 |
|
33 | 53 | """
|
34 | 54 | GenerateInbox will generate an inbox with an address and a token
|
35 | 55 | and returns an Inbox object
|
36 | 56 | > 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 |
37 | 58 | """
|
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) |
43 | 72 | data = json.loads(s)
|
44 | 73 | return Inbox(data["address"], data["token"])
|
45 |
| - |
46 | 74 |
|
47 | 75 | """
|
48 | 76 | getEmail gets the emails from an inbox object
|
49 | 77 | and returns a list of Email objects
|
50 | 78 | """
|
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) |
53 | 88 | data = json.loads(s)
|
54 | 89 |
|
55 |
| - #Raise an exception if the token is invalid |
| 90 | + # Raise an exception if the token is invalid |
56 | 91 | if "token" in s and "token" in data:
|
57 | 92 | if data["token"] == "invalid":
|
58 | 93 | raise Exception("Invalid Token")
|
59 | 94 |
|
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: |
63 | 98 | return ["None"]
|
64 | 99 | else:
|
65 | 100 | emails = []
|
66 | 101 | 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"])) |
68 | 132 | return emails
|
69 |
| - |
|
0 commit comments