chatgpt-creator 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatgpt_creator-0.1.0/.gitignore +18 -0
- chatgpt_creator-0.1.0/.python-version +1 -0
- chatgpt_creator-0.1.0/LICENSE +21 -0
- chatgpt_creator-0.1.0/PKG-INFO +119 -0
- chatgpt_creator-0.1.0/README.md +93 -0
- chatgpt_creator-0.1.0/pyproject.toml +42 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/__init__.py +8 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/cli.py +102 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/config.py +29 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/imap_otp.py +94 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/signup.py +336 -0
- chatgpt_creator-0.1.0/src/chatgpt_signup/storage.py +45 -0
- chatgpt_creator-0.1.0/uv.lock +350 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.11
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 5enox
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chatgpt-creator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Automated ChatGPT account signup using Outlook email stock with IMAP OTP retrieval
|
|
5
|
+
Project-URL: Homepage, https://github.com/5enox/chatgpt-creator
|
|
6
|
+
Project-URL: Repository, https://github.com/5enox/chatgpt-creator
|
|
7
|
+
Project-URL: Issues, https://github.com/5enox/chatgpt-creator/issues
|
|
8
|
+
Author-email: 5enox <animikantan@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: automation,chatgpt,openai,signup
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: curl-cffi>=0.7
|
|
22
|
+
Requires-Dist: faker>=28.0
|
|
23
|
+
Requires-Dist: openpyxl>=3.1
|
|
24
|
+
Requires-Dist: requests>=2.31
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# chatgpt-creator
|
|
28
|
+
|
|
29
|
+
Automated ChatGPT account signup using Outlook email stock with IMAP OTP retrieval.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install chatgpt-creator
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Or install from source:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
git clone https://github.com/5enox/chatgpt-creator.git
|
|
41
|
+
cd chatgpt-creator
|
|
42
|
+
uv sync
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## accounts.xlsx Format
|
|
46
|
+
|
|
47
|
+
Rows starting from row 4, column A. Each cell formatted as:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
email----password----client_id----refresh_token
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## CLI Usage
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Create 1 account (default)
|
|
57
|
+
chatgpt-creator
|
|
58
|
+
|
|
59
|
+
# Create multiple accounts
|
|
60
|
+
chatgpt-creator -n 5
|
|
61
|
+
|
|
62
|
+
# Custom stock file
|
|
63
|
+
chatgpt-creator --stock my_emails.xlsx
|
|
64
|
+
|
|
65
|
+
# With proxy
|
|
66
|
+
chatgpt-creator --proxy socks5://user:pass@host:port
|
|
67
|
+
|
|
68
|
+
# All options
|
|
69
|
+
chatgpt-creator -n 3 --stock emails.xlsx --output results.json --proxy socks5://host:port
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Library Usage
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from chatgpt_signup import signup, load_email_stock
|
|
76
|
+
|
|
77
|
+
# Load accounts from xlsx
|
|
78
|
+
stock = load_email_stock("accounts.xlsx")
|
|
79
|
+
|
|
80
|
+
# Sign up a single account
|
|
81
|
+
result = signup(
|
|
82
|
+
email=stock[0]["email"],
|
|
83
|
+
password="MyPassword123!",
|
|
84
|
+
name="John Doe",
|
|
85
|
+
birthday="1995-06-15",
|
|
86
|
+
client_id=stock[0]["client_id"],
|
|
87
|
+
refresh_token=stock[0]["refresh_token"],
|
|
88
|
+
proxy="socks5://user:pass@host:port", # optional
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
print(result["status"]) # "success" or "failed"
|
|
92
|
+
print(result["access_token"]) # ChatGPT access token
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Environment Variables
|
|
96
|
+
|
|
97
|
+
| Variable | Description |
|
|
98
|
+
|---|---|
|
|
99
|
+
| `SIGNUP_PROXY` | Default proxy URL |
|
|
100
|
+
| `ACCOUNTS_XLSX` | Default stock file path |
|
|
101
|
+
| `CREATED_ACCOUNTS_FILE` | Default output file path |
|
|
102
|
+
|
|
103
|
+
## Output
|
|
104
|
+
|
|
105
|
+
Created accounts are saved to `created_accounts.json` with email, password, name, birthday, and access token.
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
Contributions are welcome! Feel free to open an issue or submit a pull request.
|
|
110
|
+
|
|
111
|
+
1. Fork the repo
|
|
112
|
+
2. Create your branch (`git checkout -b feature/my-feature`)
|
|
113
|
+
3. Commit your changes (`git commit -m 'Add my feature'`)
|
|
114
|
+
4. Push to the branch (`git push origin feature/my-feature`)
|
|
115
|
+
5. Open a Pull Request
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# chatgpt-creator
|
|
2
|
+
|
|
3
|
+
Automated ChatGPT account signup using Outlook email stock with IMAP OTP retrieval.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install chatgpt-creator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or install from source:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
git clone https://github.com/5enox/chatgpt-creator.git
|
|
15
|
+
cd chatgpt-creator
|
|
16
|
+
uv sync
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## accounts.xlsx Format
|
|
20
|
+
|
|
21
|
+
Rows starting from row 4, column A. Each cell formatted as:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
email----password----client_id----refresh_token
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## CLI Usage
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# Create 1 account (default)
|
|
31
|
+
chatgpt-creator
|
|
32
|
+
|
|
33
|
+
# Create multiple accounts
|
|
34
|
+
chatgpt-creator -n 5
|
|
35
|
+
|
|
36
|
+
# Custom stock file
|
|
37
|
+
chatgpt-creator --stock my_emails.xlsx
|
|
38
|
+
|
|
39
|
+
# With proxy
|
|
40
|
+
chatgpt-creator --proxy socks5://user:pass@host:port
|
|
41
|
+
|
|
42
|
+
# All options
|
|
43
|
+
chatgpt-creator -n 3 --stock emails.xlsx --output results.json --proxy socks5://host:port
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Library Usage
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from chatgpt_signup import signup, load_email_stock
|
|
50
|
+
|
|
51
|
+
# Load accounts from xlsx
|
|
52
|
+
stock = load_email_stock("accounts.xlsx")
|
|
53
|
+
|
|
54
|
+
# Sign up a single account
|
|
55
|
+
result = signup(
|
|
56
|
+
email=stock[0]["email"],
|
|
57
|
+
password="MyPassword123!",
|
|
58
|
+
name="John Doe",
|
|
59
|
+
birthday="1995-06-15",
|
|
60
|
+
client_id=stock[0]["client_id"],
|
|
61
|
+
refresh_token=stock[0]["refresh_token"],
|
|
62
|
+
proxy="socks5://user:pass@host:port", # optional
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
print(result["status"]) # "success" or "failed"
|
|
66
|
+
print(result["access_token"]) # ChatGPT access token
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Environment Variables
|
|
70
|
+
|
|
71
|
+
| Variable | Description |
|
|
72
|
+
|---|---|
|
|
73
|
+
| `SIGNUP_PROXY` | Default proxy URL |
|
|
74
|
+
| `ACCOUNTS_XLSX` | Default stock file path |
|
|
75
|
+
| `CREATED_ACCOUNTS_FILE` | Default output file path |
|
|
76
|
+
|
|
77
|
+
## Output
|
|
78
|
+
|
|
79
|
+
Created accounts are saved to `created_accounts.json` with email, password, name, birthday, and access token.
|
|
80
|
+
|
|
81
|
+
## Contributing
|
|
82
|
+
|
|
83
|
+
Contributions are welcome! Feel free to open an issue or submit a pull request.
|
|
84
|
+
|
|
85
|
+
1. Fork the repo
|
|
86
|
+
2. Create your branch (`git checkout -b feature/my-feature`)
|
|
87
|
+
3. Commit your changes (`git commit -m 'Add my feature'`)
|
|
88
|
+
4. Push to the branch (`git push origin feature/my-feature`)
|
|
89
|
+
5. Open a Pull Request
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "chatgpt-creator"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Automated ChatGPT account signup using Outlook email stock with IMAP OTP retrieval"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "5enox", email = "animikantan@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
keywords = ["chatgpt", "signup", "automation", "openai"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Topic :: Software Development :: Libraries",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"curl-cffi>=0.7",
|
|
24
|
+
"faker>=28.0",
|
|
25
|
+
"openpyxl>=3.1",
|
|
26
|
+
"requests>=2.31",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/5enox/chatgpt-creator"
|
|
31
|
+
Repository = "https://github.com/5enox/chatgpt-creator"
|
|
32
|
+
Issues = "https://github.com/5enox/chatgpt-creator/issues"
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
chatgpt-creator = "chatgpt_signup.cli:main"
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["hatchling"]
|
|
39
|
+
build-backend = "hatchling.build"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/chatgpt_signup"]
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import random
|
|
3
|
+
import string
|
|
4
|
+
import sys
|
|
5
|
+
from datetime import date, timedelta
|
|
6
|
+
|
|
7
|
+
from faker import Faker
|
|
8
|
+
|
|
9
|
+
from .config import ACCOUNTS_XLSX, CREATED_ACCOUNTS_FILE
|
|
10
|
+
from .signup import signup
|
|
11
|
+
from .storage import load_email_stock, save_created_account
|
|
12
|
+
|
|
13
|
+
fake = Faker()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _rand_str(length: int = 8) -> str:
|
|
17
|
+
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _random_birthday() -> str:
|
|
21
|
+
today = date.today()
|
|
22
|
+
return fake.date_between(
|
|
23
|
+
start_date=today - timedelta(days=49 * 365),
|
|
24
|
+
end_date=today - timedelta(days=20 * 365),
|
|
25
|
+
).isoformat()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main():
|
|
29
|
+
parser = argparse.ArgumentParser(description="ChatGPT account signup")
|
|
30
|
+
parser.add_argument(
|
|
31
|
+
"-n", "--count", type=int, default=1,
|
|
32
|
+
help="Number of accounts to create (default: 1)",
|
|
33
|
+
)
|
|
34
|
+
parser.add_argument(
|
|
35
|
+
"--stock", default=ACCOUNTS_XLSX,
|
|
36
|
+
help=f"Path to Outlook accounts xlsx (default: {ACCOUNTS_XLSX})",
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--output", default=CREATED_ACCOUNTS_FILE,
|
|
40
|
+
help=f"Path to save created accounts (default: {CREATED_ACCOUNTS_FILE})",
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
"--proxy", default=None,
|
|
44
|
+
help="SOCKS5/HTTP proxy URL (overrides SIGNUP_PROXY env var)",
|
|
45
|
+
)
|
|
46
|
+
args = parser.parse_args()
|
|
47
|
+
|
|
48
|
+
print(f"[*] Loading email stock from {args.stock}...")
|
|
49
|
+
stock = load_email_stock(args.stock)
|
|
50
|
+
if not stock:
|
|
51
|
+
print("[!] No accounts found in stock file.")
|
|
52
|
+
sys.exit(1)
|
|
53
|
+
print(f" Loaded {len(stock)} account(s)")
|
|
54
|
+
|
|
55
|
+
if args.count > len(stock):
|
|
56
|
+
print(f"[!] Requested {args.count} but only {len(stock)} accounts available.")
|
|
57
|
+
sys.exit(1)
|
|
58
|
+
|
|
59
|
+
selected = random.sample(stock, args.count)
|
|
60
|
+
succeeded = 0
|
|
61
|
+
failed = 0
|
|
62
|
+
|
|
63
|
+
for i, acct in enumerate(selected, 1):
|
|
64
|
+
email = acct["email"]
|
|
65
|
+
first = fake.first_name()
|
|
66
|
+
last = fake.last_name()
|
|
67
|
+
name = f"{first} {last}"
|
|
68
|
+
password = "SuperSecure" + _rand_str() + "!1"
|
|
69
|
+
birthday = _random_birthday()
|
|
70
|
+
|
|
71
|
+
print(f"\n{'#' * 60}")
|
|
72
|
+
print(f" Account {i}/{args.count}: {email}")
|
|
73
|
+
print(f" Name: {name} Birthday: {birthday}")
|
|
74
|
+
print(f"{'#' * 60}")
|
|
75
|
+
|
|
76
|
+
result = signup(
|
|
77
|
+
email=email,
|
|
78
|
+
password=password,
|
|
79
|
+
name=name,
|
|
80
|
+
birthday=birthday,
|
|
81
|
+
client_id=acct["client_id"],
|
|
82
|
+
refresh_token=acct["refresh_token"],
|
|
83
|
+
proxy=args.proxy,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
save_created_account(result, args.output)
|
|
87
|
+
|
|
88
|
+
if result["status"] == "success":
|
|
89
|
+
succeeded += 1
|
|
90
|
+
print(f"[+] Saved to {args.output}")
|
|
91
|
+
else:
|
|
92
|
+
failed += 1
|
|
93
|
+
print(f"[!] Signup failed: {result['error']}")
|
|
94
|
+
|
|
95
|
+
print(f"\n{'=' * 60}")
|
|
96
|
+
print(f" Done: {succeeded} succeeded, {failed} failed")
|
|
97
|
+
print(f" Results saved to {args.output}")
|
|
98
|
+
print(f"{'=' * 60}")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
if __name__ == "__main__":
|
|
102
|
+
main()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
BASE_URL = "https://chatgpt.com"
|
|
4
|
+
AUTH_URL = "https://auth.openai.com"
|
|
5
|
+
AUTH_API = f"{AUTH_URL}/api/accounts"
|
|
6
|
+
|
|
7
|
+
IMAP_HOST = "outlook.office365.com"
|
|
8
|
+
IMAP_PORT = 993
|
|
9
|
+
OAUTH_TOKEN_URL = "https://login.microsoftonline.com/consumers/oauth2/v2.0/token"
|
|
10
|
+
|
|
11
|
+
PROXY = os.environ.get("SIGNUP_PROXY", "")
|
|
12
|
+
|
|
13
|
+
ACCOUNTS_XLSX = os.environ.get("ACCOUNTS_XLSX", "accounts.xlsx")
|
|
14
|
+
CREATED_ACCOUNTS_FILE = os.environ.get("CREATED_ACCOUNTS_FILE", "created_accounts.json")
|
|
15
|
+
|
|
16
|
+
BROWSER_HEADERS = {
|
|
17
|
+
"user-agent": (
|
|
18
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
|
19
|
+
"(KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
|
|
20
|
+
),
|
|
21
|
+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
22
|
+
"accept-language": "en-US,en;q=0.9",
|
|
23
|
+
"sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
|
24
|
+
"sec-ch-ua-mobile": "?0",
|
|
25
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
26
|
+
"sec-fetch-dest": "document",
|
|
27
|
+
"sec-fetch-mode": "navigate",
|
|
28
|
+
"sec-fetch-site": "same-origin",
|
|
29
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import time
|
|
3
|
+
import email as email_lib
|
|
4
|
+
import imaplib
|
|
5
|
+
|
|
6
|
+
import requests
|
|
7
|
+
|
|
8
|
+
from .config import IMAP_HOST, IMAP_PORT, OAUTH_TOKEN_URL
|
|
9
|
+
|
|
10
|
+
OTP_RE = re.compile(r"(?<!\d)(\d{6})(?!\d)")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _get_imap_access_token(client_id: str, refresh_token: str) -> str:
|
|
14
|
+
resp = requests.post(
|
|
15
|
+
OAUTH_TOKEN_URL,
|
|
16
|
+
data={
|
|
17
|
+
"grant_type": "refresh_token",
|
|
18
|
+
"client_id": client_id,
|
|
19
|
+
"refresh_token": refresh_token,
|
|
20
|
+
},
|
|
21
|
+
timeout=15,
|
|
22
|
+
)
|
|
23
|
+
resp.raise_for_status()
|
|
24
|
+
data = resp.json()
|
|
25
|
+
if "access_token" not in data:
|
|
26
|
+
raise ValueError(f"Token error: {data.get('error_description', data)}")
|
|
27
|
+
return data["access_token"]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _extract_text(msg: email_lib.message.Message) -> str:
|
|
31
|
+
parts = []
|
|
32
|
+
if msg.is_multipart():
|
|
33
|
+
for part in msg.walk():
|
|
34
|
+
if part.get_content_type() in ("text/plain", "text/html"):
|
|
35
|
+
try:
|
|
36
|
+
parts.append(part.get_payload(decode=True).decode(errors="ignore"))
|
|
37
|
+
except Exception:
|
|
38
|
+
pass
|
|
39
|
+
else:
|
|
40
|
+
try:
|
|
41
|
+
parts.append(msg.get_payload(decode=True).decode(errors="ignore"))
|
|
42
|
+
except Exception:
|
|
43
|
+
pass
|
|
44
|
+
return " ".join(parts)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def fetch_otp_imap(
|
|
48
|
+
email_addr: str,
|
|
49
|
+
client_id: str,
|
|
50
|
+
refresh_token: str,
|
|
51
|
+
max_retries: int = 20,
|
|
52
|
+
delay: float = 5.0,
|
|
53
|
+
) -> str | None:
|
|
54
|
+
"""Poll Outlook IMAP for the latest email and extract a 6-digit OTP."""
|
|
55
|
+
for attempt in range(1, max_retries + 1):
|
|
56
|
+
print(f" Polling IMAP for OTP... attempt {attempt}/{max_retries}")
|
|
57
|
+
try:
|
|
58
|
+
access_token = _get_imap_access_token(client_id, refresh_token)
|
|
59
|
+
auth_string = f"user={email_addr}\x01auth=Bearer {access_token}\x01\x01"
|
|
60
|
+
|
|
61
|
+
mail = imaplib.IMAP4_SSL(IMAP_HOST, IMAP_PORT)
|
|
62
|
+
mail.authenticate("XOAUTH2", lambda x: auth_string.encode())
|
|
63
|
+
mail.select("INBOX")
|
|
64
|
+
|
|
65
|
+
_, data = mail.search(None, "ALL")
|
|
66
|
+
mail_ids = data[0].split()
|
|
67
|
+
if not mail_ids:
|
|
68
|
+
mail.logout()
|
|
69
|
+
time.sleep(delay)
|
|
70
|
+
continue
|
|
71
|
+
|
|
72
|
+
latest_id = mail_ids[-1]
|
|
73
|
+
_, msg_data = mail.fetch(latest_id, "(RFC822)")
|
|
74
|
+
mail.logout()
|
|
75
|
+
|
|
76
|
+
raw = msg_data[0][1]
|
|
77
|
+
msg = email_lib.message_from_bytes(raw)
|
|
78
|
+
|
|
79
|
+
subject = msg.get("Subject", "")
|
|
80
|
+
body = _extract_text(msg)
|
|
81
|
+
plain = re.sub(r"<[^>]+>", " ", subject + " " + body)
|
|
82
|
+
|
|
83
|
+
match = OTP_RE.search(plain)
|
|
84
|
+
if match:
|
|
85
|
+
code = match.group(1)
|
|
86
|
+
print(f" Found OTP: {code} (Subject: {subject[:60]})")
|
|
87
|
+
return code
|
|
88
|
+
|
|
89
|
+
except Exception as e:
|
|
90
|
+
print(f" [!] IMAP attempt {attempt} error: {e}")
|
|
91
|
+
|
|
92
|
+
time.sleep(delay)
|
|
93
|
+
|
|
94
|
+
return None
|