-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift_watcher.py
More file actions
312 lines (273 loc) · 10.7 KB
/
shift_watcher.py
File metadata and controls
312 lines (273 loc) · 10.7 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import time
import random
import argparse
# Global verbose flag
verbose_mode = False
# Check dependencies before importing other modules
def check_dependencies():
"""Check if all required modules are installed."""
missing_modules: list[str] = []
try:
__import__("cryptography")
except ImportError:
missing_modules.append("cryptography")
try:
__import__("playwright")
except ImportError:
missing_modules.append("playwright")
try:
__import__("requests")
except ImportError:
missing_modules.append("requests")
try:
__import__("apprise")
except ImportError:
missing_modules.append("apprise")
try:
__import__("colorama")
except ImportError:
missing_modules.append("colorama")
try:
__import__("tqdm")
except ImportError:
missing_modules.append("tqdm")
if missing_modules:
print("❌ Missing required modules: {}".format(", ".join(missing_modules)))
print("💡 Please run: pip install -r requirements.txt")
print(
"💡 Make sure you're in the virtual environment: "
"source .venv/bin/activate"
)
return False
return True
# Only import other modules if dependencies are available
if not check_dependencies():
exit(1)
from colorama import Fore, Style, init
from tqdm import tqdm
from config import config
from session_manager import get_session, refresh_cookies, verify_login
from code_fetcher import fetch_new_codes
from code_redeemer import redeem_code
from rate_limiter import RateLimiter
from utils import load_json, save_json, notify, logger, setup_logging
init(autoreset=True)
rate_limiter = RateLimiter()
def main(verbose: bool = False):
global verbose_mode
verbose_mode = verbose
# Reconfigure logging for verbose mode
if verbose_mode:
setup_logging("DEBUG")
if verbose_mode:
print(
f"{Fore.BLUE}[{time.strftime('%H:%M:%S')}] Starting SHiFT Code "
f"Watcher (verbose mode){Style.RESET_ALL}"
)
session = get_session()
if verbose_mode:
print(
f"{Fore.BLUE}[{time.strftime('%H:%M:%S')}] Session loaded, "
f"verifying login...{Style.RESET_ALL}"
)
if not verify_login(session):
if verbose_mode:
print(
f"{Fore.YELLOW}[{time.strftime('%H:%M:%S')}] Session expired, "
f"refreshing cookies...{Style.RESET_ALL}"
)
notify(
config.APPRISE_URL, "ShiftWatcher", "Session expired — refreshing cookies."
)
refresh_cookies(verbose=verbose_mode)
session = get_session()
if not verify_login(session):
if verbose_mode:
print(
f"{Fore.RED}[{time.strftime('%H:%M:%S')}] Login failed "
f"after refresh{Style.RESET_ALL}"
)
notify(config.APPRISE_URL, "ShiftWatcher", "Login failed after refresh.")
return
all_codes = load_json(config.LOG_FILE, [])
used_codes = load_json(config.USED_FILE, [])
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.BLUE}[{timestamp}] Loaded {len(all_codes)} "
f"known codes, {len(used_codes)} used{Style.RESET_ALL}"
)
new_codes = fetch_new_codes()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.BLUE}[{timestamp}] Fetched {len(new_codes)} "
f"codes from sources{Style.RESET_ALL}"
)
fresh = [c for c in new_codes if c not in all_codes and c not in used_codes]
if not fresh:
print(
f"{Fore.GREEN}[{time.strftime('%H:%M:%S')}] All current codes "
f"checked. 🎉{Style.RESET_ALL}"
)
return
if verbose_mode:
print(
f"{Fore.BLUE}[{time.strftime('%H:%M:%S')}] Found {len(fresh)} "
f"new codes to check{Style.RESET_ALL}"
)
all_codes.extend(fresh)
save_json(config.LOG_FILE, all_codes)
notify(config.APPRISE_URL, "New SHiFT Codes Found", "New Code or Codes Found")
print(f"{Fore.CYAN}=== Checking {len(fresh)} new codes ==={Style.RESET_ALL}")
success_count = 0
fail_count = 0
check_counter = 0
with tqdm(
total=len(fresh), desc="Redeeming Codes", ncols=100, disable=verbose_mode
) as pbar:
for code in fresh:
result = redeem_code(session, code)
check_counter += 1
if result == "redeemed":
status = f"{Fore.GREEN}GOOD{Style.RESET_ALL}"
success_count += 1
notify(config.APPRISE_URL, "Code Redeemed", f"✅ {code}")
rate_limiter.reset()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.GREEN}[{timestamp}] Code redeemed "
f"successfully: {code}{Style.RESET_ALL}"
)
elif result == "used":
status = f"{Fore.RED}ALREADY REDEEMED{Style.RESET_ALL}"
fail_count += 1
used_codes.append(code)
save_json(config.USED_FILE, used_codes)
rate_limiter.increase()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.RED}[{timestamp}] Code already "
f"redeemed: {code}{Style.RESET_ALL}"
)
elif result == "expired":
status = f"{Fore.YELLOW}EXPIRED{Style.RESET_ALL}"
fail_count += 1
used_codes.append(code)
save_json(config.USED_FILE, used_codes)
rate_limiter.increase()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.YELLOW}[{timestamp}] Code expired: "
f"{code}{Style.RESET_ALL}"
)
elif result == "invalid":
status = f"{Fore.RED}INVALID{Style.RESET_ALL}"
fail_count += 1
used_codes.append(code)
save_json(config.USED_FILE, used_codes)
rate_limiter.increase()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.RED}[{timestamp}] Invalid code: "
f"{code}{Style.RESET_ALL}"
)
else:
status = f"{Fore.YELLOW}FAILED{Style.RESET_ALL}"
fail_count += 1
rate_limiter.increase()
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.YELLOW}[{timestamp}] Failed to "
f"redeem code: {code}{Style.RESET_ALL}"
)
if not verbose_mode:
tqdm.write(f"{Fore.WHITE}{code} → Status: {status}")
# Human like random delay 3-7 seconds
delay = random.uniform(3, 7)
if verbose_mode:
timestamp = time.strftime("%H:%M:%S")
print(
f"{Fore.BLUE}[{timestamp}] Waiting {delay:.1f}s "
f"before next code...{Style.RESET_ALL}"
)
time.sleep(delay)
if not verbose_mode:
pbar.update(1)
# Periodic update every 5 codes
if check_counter % 5 == 0 or check_counter == len(fresh):
if verbose_mode:
print(
f"{Fore.BLUE}[{time.strftime('%H:%M:%S')}] Progress: "
f"{check_counter}/{len(fresh)} codes processed "
f"({success_count} good, {fail_count} failed)"
f"{Style.RESET_ALL}"
)
else:
pbar.set_description_str(
f"Processed {check_counter}/{len(fresh)} | "
f"Success: {Fore.GREEN}{success_count}{Style.RESET_ALL} | "
f"Failed: {Fore.RED}{fail_count}{Style.RESET_ALL}"
)
if verbose_mode:
print(
f"{Fore.CYAN}[{time.strftime('%H:%M:%S')}] All codes processed — "
f"{success_count} redeemed, {fail_count} not available."
f"{Style.RESET_ALL}"
)
else:
print(
f"\n{Fore.CYAN}All codes processed — {success_count} redeemed, "
f"{fail_count} not available.{Style.RESET_ALL}"
)
# Status message: waiting for next check
hours = config.SCAN_INTERVAL // 3600
minutes = (config.SCAN_INTERVAL % 3600) // 60
time_str = f"{hours}h {minutes}m" if hours > 0 else f"{minutes}m"
print(
f"{Fore.YELLOW}[{time.strftime('%H:%M:%S')}] Waiting for new codes... "
f"Next check in {time_str}{Style.RESET_ALL}"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="SHiFT Code Watcher")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Enable verbose output with detailed logging",
)
parser.add_argument(
"--reddit",
action="store_true",
help="Monitor Reddit RSS feed for SHiFT codes instead of configured sources",
)
args = parser.parse_args()
if args.reddit:
# Import reddit parser only when needed
from reddit_parser import monitor_reddit_for_codes
# Get authenticated session for Reddit monitoring
session = get_session()
if not verify_login(session):
print(
f"{Fore.YELLOW}Session expired, refreshing cookies...{Style.RESET_ALL}"
)
refresh_cookies(verbose=args.verbose)
session = get_session()
if not verify_login(session):
print(f"{Fore.RED}Login failed after refresh{Style.RESET_ALL}")
exit(1)
# Start Reddit monitoring (runs indefinitely)
monitor_reddit_for_codes(session, rate_limiter, verbose=args.verbose)
else:
# Regular monitoring mode
while True:
try:
main(verbose=args.verbose)
except Exception as e:
logger.exception(f"Main loop error: {e}")
time.sleep(config.SCAN_INTERVAL)