lexicalspace commited on
Commit
408bf44
·
verified ·
1 Parent(s): 0556950

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -16
app.py CHANGED
@@ -30,7 +30,7 @@ file_lock = threading.Lock()
30
  traffic_log_lock = threading.Lock()
31
 
32
  # =====================================================
33
- # TRAFFIC BOT LOGIC (Script B Adapted)
34
  # =====================================================
35
 
36
  def log_traffic(msg):
@@ -54,6 +54,10 @@ def save_proxy_as_used(proxy):
54
  f.write(f"{proxy}\n")
55
 
56
  def get_fresh_proxies(needed_count):
 
 
 
 
57
  # 1. Load History
58
  used_proxies = set()
59
  if os.path.exists(HISTORY_FILE):
@@ -65,31 +69,46 @@ def get_fresh_proxies(needed_count):
65
  client = Client(TRAFFIC_PROXY_SOURCE)
66
  result = client.predict(api_name="/get_proxies_api")
67
 
68
- if isinstance(result, str):
 
 
 
 
 
 
 
69
  all_proxies = eval(result) if "[" in result else result.splitlines()
70
- else:
 
71
  all_proxies = result
 
72
 
73
- fresh_proxies = [p for p in all_proxies if p not in used_proxies]
 
 
 
 
74
 
75
  if len(fresh_proxies) < needed_count:
76
- log_traffic(f"⚠️ Only found {len(fresh_proxies)} fresh proxies.")
 
 
77
  return fresh_proxies
78
 
79
  return fresh_proxies[:needed_count]
 
80
  except Exception as e:
81
- log_traffic(f"❌ Proxy API Error: {e}")
82
  return []
83
 
84
  def visitor_bot(proxy, target_url, dwell_time, thread_id):
85
  proxies = {"http": proxy, "https": proxy}
86
  try:
87
  # STEP 1: ARRIVAL
88
- # log_traffic(f"➡️ [T{thread_id}] Arriving...") # Reduced logging to avoid spam
89
  r1 = requests.get(target_url, proxies=proxies, timeout=15)
90
 
91
  if r1.status_code != 200:
92
- # log_traffic(f"❌ [T{thread_id}] Blocked ({r1.status_code})")
93
  save_proxy_as_used(proxy)
94
  return
95
 
@@ -106,7 +125,6 @@ def visitor_bot(proxy, target_url, dwell_time, thread_id):
106
 
107
  except Exception:
108
  pass # Silently fail to keep logs clean
109
- # save_proxy_as_used(proxy)
110
 
111
  def traffic_manager(target_url, duration_mins, concurrency):
112
  global traffic_active
@@ -123,25 +141,25 @@ def traffic_manager(target_url, duration_mins, concurrency):
123
 
124
  while time.time() < end_time and traffic_active:
125
  remaining = int((end_time - time.time()) / 60)
126
- log_traffic(f"🔄 Cycle Start (Time left: ~{remaining}m)")
127
 
128
  proxies = get_fresh_proxies(concurrency)
129
 
130
  if not proxies:
131
- log_traffic(" No proxies. Retrying in 10s...")
132
  time.sleep(10)
133
  continue
134
 
 
135
  threads = []
136
  for i, proxy in enumerate(proxies):
137
- if not traffic_active: break # Stop mid-launch if cancelled
138
  t = threading.Thread(target=visitor_bot, args=(proxy, target_url, dwell_time, i+1))
139
  threads.append(t)
140
  t.start()
141
  time.sleep(0.1)
142
 
143
- # Wait for this batch to finish before starting next batch
144
- # This prevents exploding thread counts
145
  for t in threads:
146
  t.join()
147
 
@@ -150,6 +168,8 @@ def traffic_manager(target_url, duration_mins, concurrency):
150
  return "Stopped"
151
 
152
  def start_traffic_thread(url, mins, conc):
 
 
153
  threading.Thread(target=traffic_manager, args=(url, mins, conc)).start()
154
  return "✅ Traffic Started (Check Logs)"
155
 
@@ -164,7 +184,7 @@ def stop_traffic():
164
  def get_proxy_batch():
165
  try:
166
  client = Client(PROXY_SPACE_URL)
167
- result = client.predict(api_name="/get_proxies_api")
168
  proxies = result[0]
169
  if proxies:
170
  random.shuffle(proxies)
@@ -176,7 +196,7 @@ def get_proxy_batch():
176
  def get_best_proxy():
177
  try:
178
  client = Client(PROXY_SPACE_URL)
179
- result = client.predict(api_name="/get_proxies_api")
180
  proxies = result[0]
181
  if proxies:
182
  return f"http://{random.choice(proxies)}"
 
30
  traffic_log_lock = threading.Lock()
31
 
32
  # =====================================================
33
+ # TRAFFIC BOT LOGIC (Fixed)
34
  # =====================================================
35
 
36
  def log_traffic(msg):
 
54
  f.write(f"{proxy}\n")
55
 
56
  def get_fresh_proxies(needed_count):
57
+ """
58
+ Fetches proxies and handles various API return formats
59
+ (Strings, Lists, or Nested Lists) to prevent 'unhashable type' errors.
60
+ """
61
  # 1. Load History
62
  used_proxies = set()
63
  if os.path.exists(HISTORY_FILE):
 
69
  client = Client(TRAFFIC_PROXY_SOURCE)
70
  result = client.predict(api_name="/get_proxies_api")
71
 
72
+ all_proxies = []
73
+
74
+ # --- FIX START: ROBUST PARSING ---
75
+ # Handle case where result is a nested list like [['p1', 'p2']]
76
+ if isinstance(result, (list, tuple)) and len(result) > 0 and isinstance(result[0], list):
77
+ all_proxies = result[0]
78
+ # Handle case where result is a string representation
79
+ elif isinstance(result, str):
80
  all_proxies = eval(result) if "[" in result else result.splitlines()
81
+ # Handle case where result is a simple list
82
+ elif isinstance(result, list):
83
  all_proxies = result
84
+ # --- FIX END ---
85
 
86
+ # 3. Filter (Ensure p is a string before checking set membership)
87
+ fresh_proxies = []
88
+ for p in all_proxies:
89
+ if isinstance(p, str) and p not in used_proxies:
90
+ fresh_proxies.append(p)
91
 
92
  if len(fresh_proxies) < needed_count:
93
+ # Only log if significantly low to avoid spamming "0 fresh"
94
+ if len(fresh_proxies) == 0:
95
+ log_traffic(f"⚠️ No fresh proxies found (History: {len(used_proxies)})")
96
  return fresh_proxies
97
 
98
  return fresh_proxies[:needed_count]
99
+
100
  except Exception as e:
101
+ log_traffic(f"❌ Proxy API Error: {str(e)[:50]}")
102
  return []
103
 
104
  def visitor_bot(proxy, target_url, dwell_time, thread_id):
105
  proxies = {"http": proxy, "https": proxy}
106
  try:
107
  # STEP 1: ARRIVAL
108
+ # log_traffic(f"➡️ [T{thread_id}] Arriving...")
109
  r1 = requests.get(target_url, proxies=proxies, timeout=15)
110
 
111
  if r1.status_code != 200:
 
112
  save_proxy_as_used(proxy)
113
  return
114
 
 
125
 
126
  except Exception:
127
  pass # Silently fail to keep logs clean
 
128
 
129
  def traffic_manager(target_url, duration_mins, concurrency):
130
  global traffic_active
 
141
 
142
  while time.time() < end_time and traffic_active:
143
  remaining = int((end_time - time.time()) / 60)
144
+ # log_traffic(f"🔄 Cycle Start (Time left: ~{remaining}m)")
145
 
146
  proxies = get_fresh_proxies(concurrency)
147
 
148
  if not proxies:
149
+ log_traffic(" Waiting for fresh proxies (10s)...")
150
  time.sleep(10)
151
  continue
152
 
153
+ log_traffic(f"⚡ Launching {len(proxies)} bots...")
154
  threads = []
155
  for i, proxy in enumerate(proxies):
156
+ if not traffic_active: break
157
  t = threading.Thread(target=visitor_bot, args=(proxy, target_url, dwell_time, i+1))
158
  threads.append(t)
159
  t.start()
160
  time.sleep(0.1)
161
 
162
+ # Wait for this batch to finish
 
163
  for t in threads:
164
  t.join()
165
 
 
168
  return "Stopped"
169
 
170
  def start_traffic_thread(url, mins, conc):
171
+ if traffic_active:
172
+ return "⚠️ Already Running"
173
  threading.Thread(target=traffic_manager, args=(url, mins, conc)).start()
174
  return "✅ Traffic Started (Check Logs)"
175
 
 
184
  def get_proxy_batch():
185
  try:
186
  client = Client(PROXY_SPACE_URL)
187
+ result = client.predict(api_name="/get_proxies")
188
  proxies = result[0]
189
  if proxies:
190
  random.shuffle(proxies)
 
196
  def get_best_proxy():
197
  try:
198
  client = Client(PROXY_SPACE_URL)
199
+ result = client.predict(api_name="/get_proxies")
200
  proxies = result[0]
201
  if proxies:
202
  return f"http://{random.choice(proxies)}"