-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.py
162 lines (135 loc) · 4.62 KB
/
gen.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import requests as rq
import os
import random
import time
from dotenv import load_dotenv
from imap_tools import MailBox, AND
from bs4 import BeautifulSoup
from colorama import Fore
from datetime import datetime
from string import ascii_letters, digits
from threading import Thread, Semaphore, Lock
from urllib.parse import unquote
load_dotenv()
CATCHALL = os.getenv('CATCHALL')
IMAPSERVER = os.getenv('IMAPSERVER')
EMAIL = os.getenv('EMAIL')
PASSWORD = os.getenv('PASSWORD')
PROXYS = []
emaillock = Semaphore(15)
filelock = Lock()
dontcheck = 99999999999999999
discountcounter = {
50:0,
75:0,
100:0
}
def setTitle(discount=None):
global discountcounter
if discount:
discountcounter[discount]+=1
else:
discountcounter = {
50:0,
75:0,
100:0
}
os.system(f"title 100%: {discountcounter[100]} 75%: {discountcounter[75]} 50%: {discountcounter[50]}")
def loadProxies():
global PROXYS
p = []
with open("proxies.txt", "r") as file:
for line in file.readlines():
if len(line) != 0:
line = line.replace('\n', '')
p.append({
"http":f"http://{line}",
"https":f"http://{line}"
})
PROXYS = p
def log(text,color,taskid):
print(color + f"[{datetime.now().strftime('%H:%M:%S')}] [Task {taskid}] " + str(text) + Fore.RESET)
def createBahncardVoucher(proxy):
if "@" in CATCHALL:
data = {
'e': f'{CATCHALL.split("@")[0]}+{"".join(random.choices(ascii_letters+digits, k=random.randint(10,20)))}@{CATCHALL.split("@")[1]}',
'k': '1',
'g': '0',
}
else:
data = {
'e': f'{"".join(random.choices(ascii_letters+digits, k=random.randint(10,25)))}@{CATCHALL}',
'k': '1',
'g': '0',
}
response = rq.post('https://www.probebahncard.de/api/abschicken?TNB=Zustimmung', json=data, proxies=proxy, timeout=15)
response.raise_for_status()
return data["e"]
def fetchVerificationCodeFromEmail(email):
with emaillock:
attempts = 0
while True:
with MailBox(IMAPSERVER).login(EMAIL, PASSWORD) as mailbox:
for message in mailbox.fetch(AND(to=email), reverse=True):
emailcontent = BeautifulSoup(message.html, 'html.parser')
link = emailcontent.find('a', {'title': 'E-Mail-Adresse verifizieren & eCoupon erhalten'})['href']
code = link.split("=")[1]
mailbox.delete(message.uid)
return unquote(code)
if attempts == 40 or dontcheck < time.time():
raise Exception("Cant find the Email")
time.sleep(1)
attempts+=1
def submitVerificationCode(code, proxy):
data = {
'ig': code,
}
response = rq.post('https://www.probebahncard.de/api/confirmation', json=data, proxies=proxy, timeout=15)
response.raise_for_status()
return response.json()
def create(taskid):
if len(PROXYS) != 0:
proxy = random.choice(PROXYS)
else:
proxy = {}
try:
email = createBahncardVoucher(proxy)
except Exception as e:
log(e, Fore.RED, taskid)
return
log(f"Fetching verification code from {email} ....", Fore.BLUE, taskid)
try:
code = fetchVerificationCodeFromEmail(email)
except Exception as e:
log(e, Fore.RED, taskid)
return
log(f"Submiting verification code ({code}) to the Deutsche Bahn server", Fore.MAGENTA, taskid)
try:
voucher = submitVerificationCode(code, proxy)
except Exception as e:
log(e, Fore.RED, taskid)
return
log(f"Got the Voucher {voucher['e']} with a {voucher['g']}% discount", Fore.GREEN, taskid)
with filelock:
setTitle(int(voucher['g']))
with open(f"codes/{voucher['g']}.txt", "a") as file:
file.write(f"{voucher['e']}\n")
if __name__ == "__main__":
while True:
setTitle()
vouchers = int(input("How many codes to you want to generate? "))
if input("Do you want to use proxys? (y|n) ") == "y":
loadProxies()
else:
PROXYS = []
dontcheck = time.time()+180
threadPool = []
for task in range(1, vouchers+1):
t = Thread(target=create, args=(task,))
threadPool.append(t)
t.start()
while any(t.is_alive() for t in threadPool):
time.sleep(1)
print("--- Finished ---")
if input("Do you want to generate more codes? (y|n) ") == "n":
break