feat: add recommendation command and expand test coverage

This commit is contained in:
luofengyuan committed 2026-05-18 19:27:41 +08:00
1 parent 702280a416
commit f9267352cc
9 files changed
+684 -4

No files matched your search

+103 -1
View File
@@ -83,6 +83,95 @@ def do_ban(client, args):
return {"provider": args.provider, "error": "ban_not_supported_by_default"}
def _to_float(v: Any, default: float = 0.0) -> float:
try:
return float(v)
except Exception:
return default
def recommend_smsvn(client, args):
if not args.country:
raise RuntimeError("recommend for smsvn requires --country")
data = client.services(int(args.country))
if not isinstance(data, list):
raise RuntimeError("unexpected smsvn services response")
items = []
for row in data:
if not isinstance(row, dict):
continue
sid = str(row.get("id", "")).strip()
if args.service and sid != args.service:
continue
price = _to_float(row.get("price"), default=1e18)
qty = int(_to_float(row.get("quantity"), default=0))
deliverability = _to_float(row.get("deliverability"), default=0.0)
if qty < args.min_qty:
continue
if args.max_price is not None and price > args.max_price:
continue
items.append(
{
"service": sid,
"name": row.get("name"),
"price": price,
"quantity": qty,
"deliverability": deliverability,
"country": int(args.country),
"provider": "smsvn",
}
)
items.sort(key=lambda x: (x["price"], -x["quantity"], -x["deliverability"], x["service"]))
return {"provider": "smsvn", "recommendations": items[: args.top_n], "total_candidates": len(items)}
def recommend_5sim(client, args):
if not args.country:
raise RuntimeError("recommend for 5sim requires --country")
raw = client.guest_products(args.country, args.operator)
if not isinstance(raw, dict):
raise RuntimeError("unexpected 5sim products response")
items = []
for service, row in raw.items():
if args.service and service != args.service:
continue
if not isinstance(row, dict):
continue
price = _to_float(row.get("Price"), default=1e18)
qty = int(_to_float(row.get("Qty"), default=0))
if qty < args.min_qty:
continue
if args.max_price is not None and price > args.max_price:
continue
items.append(
{
"service": service,
"category": row.get("Category"),
"price": price,
"quantity": qty,
"country": args.country,
"operator": args.operator,
"provider": "5sim",
}
)
items.sort(key=lambda x: (x["price"], -x["quantity"], x["service"]))
return {"provider": "5sim", "recommendations": items[: args.top_n], "total_candidates": len(items)}
def do_recommend(client, args):
if args.provider == "smsvn":
return recommend_smsvn(client, args)
if args.provider == "5sim":
return recommend_5sim(client, args)
return {
"provider": "grizzly",
"error": "recommend_not_supported",
"note": "grizzly public activation API does not provide a stable full discovery table here",
}
def main() -> int:
ap = argparse.ArgumentParser(description="SMS activation unified operator")
ap.add_argument("--provider", choices=["smsvn", "5sim", "grizzly"], required=True)
@@ -98,6 +187,14 @@ def main() -> int:
p_cov.add_argument("--operator", default="any")
p_cov.add_argument("--service")
p_rec = sub.add_parser("recommend")
p_rec.add_argument("--country")
p_rec.add_argument("--operator", default="any")
p_rec.add_argument("--service")
p_rec.add_argument("--max-price", type=float)
p_rec.add_argument("--min-qty", type=int, default=1)
p_rec.add_argument("--top-n", type=int, default=10)
p_buy = sub.add_parser("buy")
p_buy.add_argument("--country", required=True)
p_buy.add_argument("--operator", default="any")
@@ -123,10 +220,13 @@ def main() -> int:
args = ap.parse_args()
state = read_state(args.state_file)
exit_code = 0
try:
client = get_client(args.provider)
if args.cmd == "balance":
out = do_balance(client, args.provider)
elif args.cmd == "recommend":
out = do_recommend(client, args)
elif args.cmd == "discover":
if args.provider == "smsvn":
if not args.country:
@@ -171,14 +271,16 @@ def main() -> int:
out = {"ok": False, "provider": e.provider, "error_code": e.code, "error": e.message}
if args.debug_errors and e.payload is not None:
out["payload"] = e.payload
exit_code = 1
except Exception as e:
out = {"ok": False, "error": str(e)}
exit_code = 2
if args.raw:
print(json.dumps(out, ensure_ascii=False, indent=2, default=str))
else:
print(json.dumps(out, ensure_ascii=False, default=str))
return 0
return exit_code
if __name__ == "__main__":