| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """登录 + 钱包 + 市场 render 取价,结果写入 _tmp_session.json(读 config.json)"""
- import json
- import sys
- from pathlib import Path
- import requests
- ROOT = Path(__file__).resolve().parent
- CFG = ROOT / "config.json"
- OUT = ROOT / "_tmp_session.json"
- def load_cfg():
- with CFG.open("r", encoding="utf-8") as f:
- return json.load(f)
- def resolve_mafile(mafile: str) -> str:
- p = Path(mafile)
- if not p.is_absolute():
- p = ROOT / p
- return str(p.resolve())
- def normalize_proxy(proxy: str):
- raw = (proxy or "").strip()
- if not raw:
- return None
- if raw.lower().startswith(("http://", "https://", "socks5://", "socks4://", "socks://")):
- return raw
- return f"http://{raw}"
- def main() -> int:
- cfg = load_cfg()
- api = (cfg.get("api_base") or "http://localhost:5000").rstrip("/")
- mafile = resolve_mafile(cfg["mafile"])
- market = (cfg.get("market_url") or "").strip()
- if not market:
- print("config.json 需要 market_url", file=sys.stderr)
- return 2
- proxy = normalize_proxy(cfg.get("proxy") or "")
- proxies = {"http": proxy, "https": proxy} if proxy else None
- body = {
- "Username": cfg["username"],
- "Password": cfg["password"],
- "MaFilePath": mafile,
- }
- if proxy:
- body["ProxyUrl"] = proxy
- r = requests.post(f"{api}/login", json=body, timeout=90)
- print("login", r.status_code)
- r.raise_for_status()
- d = r.json()
- sess = d["session"]
- cookies = {c["name"]: c["value"] for c in d.get("cookies") or []}
- sls = cookies.get("steamLoginSecure") or f"{sess['steamId']}%7C%7C{sess['accessToken']}"
- sid = cookies.get("sessionid") or sess.get("sessionId")
- print("steamId", sess.get("steamId"), "sessionId", sid)
- wr = requests.get(
- "https://store.steampowered.com/api/getfundwalletinfo/?cc=us&l=english",
- headers={"Cookie": f"steamLoginSecure={sls}"},
- timeout=20,
- proxies=proxies,
- )
- print("wallet_http", wr.status_code, wr.text[:400])
- url = market.rstrip("/") + "/render/?query=&start=0&count=10&country=US&language=schinese¤cy=1"
- cookie = f"timezoneOffset=28800,0; sessionid={sid}; steamLoginSecure={sls}; bMarketOptOut=1"
- rr = requests.get(
- url,
- headers={"User-Agent": "Mozilla/5.0", "Cookie": cookie, "Referer": market},
- timeout=30,
- proxies=proxies,
- )
- print("render", rr.status_code)
- page = rr.json()
- info = page.get("listinginfo") or {}
- print("listing_count", len(info))
- rows = []
- for lid, L in info.items():
- sub = int(L.get("converted_price") or 0)
- sf = int(L.get("converted_steam_fee") or 0)
- pf = int(L.get("converted_publisher_fee") or 0)
- rows.append((str(lid), sub, sf, pf, sub + sf + pf))
- rows.sort(key=lambda x: x[4])
- print(json.dumps(rows[:5], indent=2, ensure_ascii=False))
- OUT.write_text(
- json.dumps(
- {
- "session": sess,
- "steamLoginSecure": sls,
- "sessionId": sid,
- "mafile": mafile,
- "cheapest": rows[0] if rows else None,
- "market": market,
- },
- ensure_ascii=False,
- indent=2,
- ),
- encoding="utf-8",
- )
- print("saved", OUT)
- return 0
- if __name__ == "__main__":
- try:
- sys.stdout.reconfigure(encoding="utf-8")
- except Exception:
- pass
- raise SystemExit(main())
|