Spaces:
Runtime error
Runtime error
Download brm_api.py from CLASSZhao2653/yahoo_2_test: direct link, hf CLI and curl.
- Browser
- Download file 8.25 kB
-
https://huggingface.co/spaces/CLASSZhao2653/yahoo_2_test/resolve/main/brm_api.py
- Command line
-
hf download hf://spaces/CLASSZhao2653/yahoo_2_test/brm_api.py
-
curl -L -o brm_api.py https://huggingface.co/spaces/CLASSZhao2653/yahoo_2_test/resolve/main/brm_api.py
8.25 kB
| """ | |
| 霸王猫小程序网络请求客户端 (稳定直连版) | |
| """ | |
| import requests | |
| import urllib.parse | |
| import urllib3 | |
| from config_manager import load_location | |
| from brm_cipher import CIPHER | |
| urllib3.disable_warnings() | |
| class BrmApi: | |
| BASE_URL = "https://brmpro.brmbwc.com" | |
| def __init__(self, auth_token: str, token_id: str): | |
| self.session = requests.Session() | |
| self.headers = { | |
| "Host": "brmpro.brmbwc.com", | |
| "Connection": "keep-alive", | |
| "Authorization": auth_token, | |
| "Tokenid": token_id, | |
| "Clienttype": "1", | |
| "Version": "4.1", | |
| "Content-Type": "application/json", | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541f0d) XWEB/25715", | |
| "Referer": "https://servicewechat.com/wx26bec66f47529a64/125/page-frame.html", | |
| "Accept": "*/*" | |
| } | |
| def get_user_info(self): | |
| """查询用户信息与余额""" | |
| url = f"{self.BASE_URL}/brm_applet/user/info" | |
| try: | |
| res = self.session.get(url, headers=self.headers, params={"wechatUserId": 1}, timeout=4, verify=False).json() | |
| if res.get("code") == 200: | |
| data = res.get("data", {}) | |
| return True, { | |
| "nickname": data.get("wechatNickname", "未知"), | |
| "phone": data.get("phone", "未知"), | |
| "balance": float(data.get("balance", 0.0)), | |
| "earnings": float(data.get("earnings", 0.0)), | |
| "score": data.get("userScore", 100) | |
| } | |
| return False, res.get("message", "Token失效或参数错误") | |
| except Exception as e: | |
| return False, f"网络错误: {e}" | |
| def search_shops(self, keyword="", platform="", page=1, max_pages=3, page_size=10, loc_info=None, **kwargs): | |
| """ | |
| 根据传入的定位信息进行搜索,支持动态切换多地址 | |
| """ | |
| # 如果前端指定了地址就用指定的,否则读取默认地址 | |
| loc = loc_info if loc_info else load_location() | |
| url = f"{self.BASE_URL}/brm_applet/index/search" | |
| # 提取定位密文 | |
| lat_val = urllib.parse.unquote(loc.get("latitude", "5DEmLDs6KpULOXaswS+2wQ==")).strip() | |
| lng_val = urllib.parse.unquote(loc.get("longitude", "O/pelUGr/FxAfejSqqVVgA==")).strip() | |
| sign_val = urllib.parse.unquote(loc.get("sign", "qG80UEs3WulAJ1uPqUBwC8r1LYgK90b7gdaqUbD0mhA=")).strip() | |
| city_name = loc.get("cityName", "乌鲁木齐市") | |
| all_parsed = [] | |
| # ========== 翻页只递增 current,其他参数完全固定 ========== | |
| for curr_page in range(page, page + max_pages): | |
| params = { | |
| "activityName": keyword, | |
| "platform": platform, | |
| "sortBy": "1", | |
| "classifyId": "1", # 严格锁定你的过滤条件: 1 (外卖美食) | |
| "pagesize": "10", | |
| "current": str(curr_page), # 【唯一变动的翻页参数】: 1 -> 2 -> 3 ... | |
| "now": "0", | |
| "pagePvId": "", | |
| "cityName": city_name, | |
| "latitude": lat_val, | |
| "longitude": lng_val, | |
| "sign": sign_val | |
| } | |
| try: | |
| resp = self.session.get(url, headers=self.headers, params=params, timeout=6, verify=False) | |
| res = resp.json() | |
| if res.get("code") != 200: | |
| if curr_page == page: | |
| return False, res.get("message", "搜索失败") | |
| break | |
| raw_list = res.get("data", {}).get("resultList", []) | |
| if not raw_list: | |
| break # 当前页没有更多店铺了,跳出 | |
| for item in raw_list: | |
| # 兼容抓取活动 ID(普通活动取 id,部分活动在 planList/poiEventId) | |
| act_id = item.get("id") | |
| plan_list = item.get("planList") or [] | |
| if not act_id: | |
| if plan_list and plan_list[0].get("poiEventId"): | |
| act_id = plan_list[0].get("poiEventId") | |
| elif item.get("poiEventId"): | |
| act_id = item.get("poiEventId") | |
| if not act_id: | |
| continue | |
| dist_m = item.get("distance") | |
| dist_str = f"{round(float(dist_m)/1000, 2)}km" if dist_m else item.get("distanceStr", "未知") | |
| p_code = item.get("platform", 1) | |
| p_name = "美团" if p_code == 1 else ("饿了么/淘" if p_code == 2 else "美团闪购") | |
| rebate_val = float(item.get("rebate") or 0.0) | |
| if rebate_val == 0.0: | |
| commission = item.get("userMaxCommission") or (plan_list[0].get("userMaxCommission") if plan_list else 0) | |
| if commission: | |
| rebate_val = round(float(commission) / 100.0, 2) | |
| rebate_full = float(item.get("rebateFull") or 0.0) | |
| all_parsed.append({ | |
| "活动ID": str(act_id), | |
| "店铺名称": item.get("activityName", "未知"), | |
| "平台": p_name, | |
| "门槛": rebate_full, | |
| "返利": rebate_val, | |
| "剩余库存": int(item.get("inventory") or 0), | |
| "开抢时间": item.get("activityTimeStart", "全天"), | |
| "距离": dist_str, | |
| "店铺ID": str(item.get("storeId", "")) | |
| }) | |
| # 如果当前页不满 10 条,说明已达末尾,终止后续翻页 | |
| if len(raw_list) < 10: | |
| break | |
| except Exception as e: | |
| if curr_page == page: | |
| return False, f"网络请求失败: {e}" | |
| break | |
| return True, all_parsed | |
| def sign_up(self, activity_id: str): | |
| """【步骤1】报名抢单""" | |
| url = f"{self.BASE_URL}/brm_applet/order/signUp" | |
| try: | |
| resp = self.session.post(url, headers=self.headers, json={"activityId": str(activity_id)}, timeout=2.5, verify=False) | |
| res = resp.json() | |
| if res.get("code") == 200: | |
| order_id = res.get("data", {}).get("orderId") | |
| return True, order_id, "抢单成功" | |
| return False, None, res.get("message", "报名拦截") | |
| except Exception as e: | |
| return False, None, str(e) | |
| def pay_order(self, order_id: str, pay_way: int = 1): | |
| """【步骤2】余额自动秒付 (payWay=1)""" | |
| url = f"{self.BASE_URL}/brm_applet/payment/pay" | |
| try: | |
| resp = self.session.post(url, headers=self.headers, json={"orderId": str(order_id), "payWay": pay_way}, timeout=2.5, verify=False) | |
| res = resp.json() | |
| if res.get("code") == 200: | |
| return True, "支付成功" | |
| return False, res.get("message", "支付失败") | |
| except Exception as e: | |
| return False, str(e) | |
| def cancel_order(self, order_id: str): | |
| """取消订单""" | |
| url = f"{self.BASE_URL}/brm_applet/order/cancel" | |
| try: | |
| res = self.session.post(url, headers=self.headers, json={"orderId": str(order_id)}, timeout=3, verify=False).json() | |
| return res.get("code") == 200, res.get("message", "") | |
| except Exception as e: | |
| return False, str(e) |