wispbyte parser fleet: farm + deploy + dashboard + stdlib parsers
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""forum_lite.py — stdlib forum/JSON feed parser for Wispbyte free servers.
|
||||
Sources: nodeloc.com latest.json, v2ex hot.json. Keyword filter (freebies/AI/abuse).
|
||||
Dedup via state file, new items -> Telegram.
|
||||
"""
|
||||
import json, time, urllib.request, urllib.error, os, re
|
||||
|
||||
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"}
|
||||
STATE = "forum_state.json"
|
||||
KEYWORDS = re.compile(r"免费|白嫖|福利|羊毛|AI|GPT|Claude|Gemini|proxy|VPN|VPS|api|key|注册|羊毛党|free|credit", re.I)
|
||||
|
||||
SOURCES = {
|
||||
"nodeloc": "https://www.nodeloc.com/latest.json",
|
||||
"v2ex": "https://www.v2ex.com/api/topics/hot.json",
|
||||
}
|
||||
|
||||
|
||||
def fetch(url, timeout=25):
|
||||
req = urllib.request.Request(url, headers=UA)
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode("utf-8", "replace"))
|
||||
|
||||
|
||||
def load_state():
|
||||
try:
|
||||
return json.load(open(STATE, encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def save_state(st):
|
||||
tmp = STATE + ".tmp"
|
||||
json.dump(st, open(tmp, "w", encoding="utf-8"))
|
||||
os.replace(tmp, STATE)
|
||||
|
||||
|
||||
def parse_nodeloc(data):
|
||||
out = []
|
||||
for t in (data.get("topic_list") or {}).get("topics", [])[:40]:
|
||||
out.append({
|
||||
"id": "nl-%s" % t.get("id"),
|
||||
"title": t.get("title", ""),
|
||||
"url": "https://www.nodeloc.com/t/%s" % t.get("slug", t.get("id")),
|
||||
"src": "nodeloc",
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def parse_v2ex(data):
|
||||
out = []
|
||||
if not isinstance(data, list):
|
||||
return out
|
||||
for t in data[:40]:
|
||||
out.append({
|
||||
"id": "vx-%s" % t.get("id"),
|
||||
"title": t.get("title", ""),
|
||||
"url": t.get("url", ""),
|
||||
"src": "v2ex",
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
PARSERS = {"nodeloc": parse_nodeloc, "v2ex": parse_v2ex}
|
||||
|
||||
|
||||
def collect():
|
||||
st = load_state()
|
||||
seen = set(st.get("seen", []))
|
||||
fresh = []
|
||||
for name, url in SOURCES.items():
|
||||
try:
|
||||
data = fetch(url)
|
||||
for it in PARSERS[name](data):
|
||||
if it["id"] in seen:
|
||||
continue
|
||||
seen.add(it["id"])
|
||||
if KEYWORDS.search(it["title"]):
|
||||
fresh.append(it)
|
||||
except Exception as e:
|
||||
print("forum src %s err: %r" % (name, e), flush=True)
|
||||
st["seen"] = list(seen)[-4000:]
|
||||
st["last_run"] = int(time.time())
|
||||
save_state(st)
|
||||
return fresh
|
||||
|
||||
|
||||
def run(cfg, send_tg):
|
||||
first = True
|
||||
while True:
|
||||
try:
|
||||
fresh = collect()
|
||||
if fresh and not first:
|
||||
lines = ["<b>FORUM LITE</b> (%s)" % cfg.get("server_label", "?")]
|
||||
for it in fresh[:10]:
|
||||
lines.append('- <a href="%s">%s</a> <i>%s</i>' % (it["url"], it["title"][:90].replace("<", "<"), it["src"]))
|
||||
send_tg("\n".join(lines))
|
||||
elif fresh and first:
|
||||
print("first run: seeded %d items (silent)" % len(fresh), flush=True)
|
||||
first = False
|
||||
except Exception as e:
|
||||
print("forum loop err: %r" % e, flush=True)
|
||||
time.sleep(cfg.get("forum_interval_min", 20) * 60)
|
||||
@@ -0,0 +1,68 @@
|
||||
"""main.py — supervisor for Wispbyte parser fleet (stdlib only, Python 3.11 egg).
|
||||
Uploads sit next to this file: forum_lite.py, proxy_collector.py, config.json.
|
||||
Boot message -> Telegram; then parser loops in threads; auto-restart loops on crash.
|
||||
"""
|
||||
import json, threading, time, socket, sys, urllib.request, traceback
|
||||
|
||||
|
||||
def load_cfg():
|
||||
try:
|
||||
return json.load(open("config.json", encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
CFG = load_cfg()
|
||||
|
||||
|
||||
def send_tg(text, tries=3):
|
||||
tok = CFG.get("tg_bot_token", "")
|
||||
chat = CFG.get("tg_chat_id")
|
||||
if not tok or not chat:
|
||||
print("[tg] not configured:", text[:120], flush=True)
|
||||
return False
|
||||
url = "https://api.telegram.org/bot%s/sendMessage" % tok
|
||||
payload = {"chat_id": chat, "text": text[:4000], "parse_mode": "HTML",
|
||||
"disable_web_page_preview": True}
|
||||
th = CFG.get("tg_thread_id")
|
||||
if th:
|
||||
payload["message_thread_id"] = th
|
||||
data = json.dumps(payload).encode()
|
||||
for i in range(tries):
|
||||
try:
|
||||
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
|
||||
with urllib.request.urlopen(req, timeout=20) as r:
|
||||
return r.status == 200
|
||||
except Exception as e:
|
||||
print("[tg] err %r" % e, flush=True)
|
||||
time.sleep(5 + i * 5)
|
||||
return False
|
||||
|
||||
|
||||
def loop_guard(name, fn):
|
||||
while True:
|
||||
try:
|
||||
fn()
|
||||
except Exception:
|
||||
print("[supervisor] %s crashed, restart in 30s\n%s" % (name, traceback.format_exc()[-600:]), flush=True)
|
||||
time.sleep(30)
|
||||
|
||||
|
||||
def main():
|
||||
label = CFG.get("server_label", socket.gethostname())
|
||||
send_tg("<b>[fleet]</b> %s UP py=%s host=%s" % (label, sys.version.split()[0], socket.gethostname()))
|
||||
import forum_lite, proxy_collector
|
||||
t1 = threading.Thread(target=loop_guard, args=("forum", lambda: forum_lite.run(CFG, send_tg)), daemon=True)
|
||||
t2 = threading.Thread(target=loop_guard, args=("proxy", lambda: proxy_collector.run(CFG, send_tg)), daemon=True)
|
||||
t1.start(); t2.start()
|
||||
hb = 0
|
||||
while True:
|
||||
time.sleep(60)
|
||||
hb += 1
|
||||
if hb % (CFG.get("heartbeat_hours", 6) * 60) == 0:
|
||||
send_tg("<b>[fleet]</b> %s heartbeat ok (forum=%s proxy=%s)" % (
|
||||
label, t1.is_alive(), t2.is_alive()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,77 @@
|
||||
"""proxy_collector.py — stdlib free-proxy harvester + liveness checker for Wispbyte servers.
|
||||
Sources: TheSpeedX, proxyscrape. Checks sample concurrently, posts digest to Telegram,
|
||||
keeps live list in proxies_live.txt on server disk.
|
||||
"""
|
||||
import json, time, urllib.request, re, os
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/131.0.0.0 Safari/537.36"}
|
||||
LIVE_FILE = "proxies_live.txt"
|
||||
|
||||
SOURCES = [
|
||||
("thespeedx", "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt"),
|
||||
("proxyscrape", "https://api.proxyscrape.com/v4/free-proxy/https-proxy/list?protocol=http&country=all&proxy_type=http&format=txt"),
|
||||
]
|
||||
IPPORT = re.compile(r"^\d{1,3}(?:\.\d{1,3}){3}:\d{2,5}$")
|
||||
|
||||
|
||||
def fetch_text(url, timeout=25):
|
||||
req = urllib.request.Request(url, headers=UA)
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return r.read().decode("utf-8", "replace")
|
||||
|
||||
|
||||
def candidates(limit_per_source=300):
|
||||
out = []
|
||||
for name, url in SOURCES:
|
||||
try:
|
||||
txt = fetch_text(url)
|
||||
got = [l.strip() for l in txt.splitlines() if IPPORT.match(l.strip())][:limit_per_source]
|
||||
out.extend(got)
|
||||
print("proxy src %s: %d" % (name, len(got)), flush=True)
|
||||
except Exception as e:
|
||||
print("proxy src %s err: %r" % (name, e), flush=True)
|
||||
return list(dict.fromkeys(out))
|
||||
|
||||
|
||||
def check_one(px, timeout=7):
|
||||
proxy_handler = urllib.request.ProxyHandler({"http": "http://" + px, "https": "http://" + px})
|
||||
opener = urllib.request.build_opener(proxy_handler)
|
||||
t0 = time.time()
|
||||
try:
|
||||
r = opener.open("http://api.ipify.org/", timeout=timeout)
|
||||
body = r.read(64).decode().strip()
|
||||
if IPPORT.match(body) or re.match(r"^\d+\.\d+", body):
|
||||
return px, round(time.time() - t0, 2)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def run(cfg, send_tg):
|
||||
sample_cap = cfg.get("proxy_sample", 150)
|
||||
while True:
|
||||
try:
|
||||
cands = candidates()
|
||||
sample = cands[:sample_cap] if cands else []
|
||||
live = []
|
||||
if sample:
|
||||
with ThreadPoolExecutor(max_workers=25) as ex:
|
||||
futs = [ex.submit(check_one, p) for p in sample]
|
||||
for f in as_completed(futs, timeout=180):
|
||||
r = f.result()
|
||||
if r:
|
||||
live.append(r)
|
||||
live.sort(key=lambda x: x[1])
|
||||
if live:
|
||||
with open(LIVE_FILE, "a", encoding="utf-8") as f:
|
||||
for px, lat in live:
|
||||
f.write("%s\n" % px)
|
||||
top = "\n".join("%s (%.1fs)" % (px, lat) for px, lat in live[:30])
|
||||
send_tg("<b>PROXY DIGEST</b> (%s)\nlive %d/%d checked\n<pre>%s</pre>" % (
|
||||
cfg.get("server_label", "?"), len(live), len(sample), top))
|
||||
else:
|
||||
print("no live proxies this cycle (%d candidates)" % len(sample), flush=True)
|
||||
except Exception as e:
|
||||
print("proxy loop err: %r" % e, flush=True)
|
||||
time.sleep(cfg.get("proxy_interval_min", 180) * 60)
|
||||
Reference in New Issue
Block a user