Skip to content

Commit

Permalink
fix: 解决获取版本号乱码的问题
Browse files Browse the repository at this point in the history
1. 重点解决返回500的问题。因为之前没有获取的网页没有正确解码,所以版本号是乱码,导致header中的版本号异常
2. 优化代码

BREAKING CHANGE: 1. 添加正确的解码
2. 用bs替换之前的正则方式获取版本号

Closes 397179459#1 接口返回500
  • Loading branch information
397179459 committed Aug 17, 2023
1 parent 3db2966 commit ac842af
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 65 deletions.
71 changes: 17 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# i茅台预约脚本
# i茅台预约脚本----GitHub Actions版
## 原理:
### 1、登录获取验证码
### 2、输入验证码获取TOKEN
Expand All @@ -13,46 +13,9 @@
pip3 install --no-cache-dir -r requirements.txt
```

### 2、(可选)修改config.py
```python
import os

ITEM_MAP = {
"10213": "53%vol 500ml贵州茅台酒(癸卯兔年)",
"10214": "53%vol 375ml×2贵州茅台酒(癸卯兔年)",
"10056": "53%vol 500ml茅台1935",
"2478": "53%vol 500ml贵州茅台酒(珍品)"
}

# 需要预约的商品(默认只预约2个兔茅)
########################
ITEM_CODES = ['10213', '10214']

# push plus 微信推送,具体使用参考 https://www.pushplus.plus
# 例如: PUSH_TOKEN = '123456'
########################
# 不填不推送消息,一对一发送
PUSH_TOKEN = os.environ.get("PUSHPLUS_KEY")
########################

# credentials 路径,例如:CREDENTIALS_PATH = /home/user/.imoutai/credentials
# 不配置,使用默认路径,在宿主目录
# 例如: CREDENTIALS_PATH = '/home/user/.imautai/credentials'
########################
CREDENTIALS_PATH = None
########################

# 预约规则配置
########################
# 预约本市出货量最大的门店
MAX_ENABLED = False
# 预约你的位置附近门店
DISTANCE_ENABLED = True
########################
### 2、(可选)修改config.py,按照你的需求修改相关配置


```

### 3、按提示输入 预约位置、手机号、验证码 等,生成的token等 配置文件会保存在 $HOME/.imaotai/credentials, 很长时间不再需要登录。支持多账号
```shell
python3 login.py
Expand Down Expand Up @@ -84,26 +47,26 @@ province = 北京市
lat = 45.042259
lng = 115.344116

[1861164****]
city = 太原市
token = 6INvrtyGOTdpsvFmiw0I4FoFNDyG-ekt2WFsQsU9nBU
userid = 10677****
province = 山西省
lat = 45.042259
lng = 115.344116
```

### 4、python3 main.py ,执行预约操作
```shell
python3 main.py
```

## 注意:
### 1、可以配置一个定时任务,执行每日自动预约,建议每天多执行2次
### 2、注意服务器的时区是UTC+8,中国区域
```shell
# imaotai
10,40,50 9 * * * root python3 /home/mobian/app/imaotai/main.py >> /var/log/imaotai.log
```
##### 感谢提供的文档:https://blog.csdn.net/weixin_47481826/article/details/128893239
### 5、配置 Github actions,每日自动预约,省去自己买服务器的成本。

## 请开发者喝咖啡
- 欢迎使用支付宝或微信请我喝咖啡(O.o)

![](resources/imgs/wxqr.png)

![](resources/imgs/zfbqr.jpg)


## 特别感谢
技术思路:https://blog.csdn.net/weixin_47481826/article/details/128893239

初版代码:https://github.com/tianyagogogo/imaotai


8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/python3
import logging
import sys

Expand All @@ -12,10 +11,11 @@
stream=sys.stdout,
datefmt=DATE_FORMAT)

print("================== startRun")
# 获取当日session id
print("====== MainStartRun =======")

process.get_current_session_id()

# 校验配置文件是否存在
configs = login.config
if len(configs.sections()) == 0:
logging.error("配置文件未找到配置")
Expand Down Expand Up @@ -48,7 +48,7 @@
source_data=source_data,
lat=lat,
lng=lng)
print(f'max shop id : {max_shop_id}')
# print(f'max shop id : {max_shop_id}')
if max_shop_id == '0':
continue
shop_info = source_data.get(str(max_shop_id))
Expand Down
39 changes: 32 additions & 7 deletions process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from encrypt import Encrypt
import requests
import hashlib

from bs4 import BeautifulSoup
import logging

AES_KEY = 'qbhajinldepmucsonaaaccgypwuvcjaa'
Expand All @@ -21,9 +21,27 @@

CURRENT_TIME = str(int(time.time() * 1000))
headers = {}
mt_version = "".join(re.findall('new__latest__version">(.*?)</p>',
requests.get('https://apps.apple.com/cn/app/i%E8%8C%85%E5%8F%B0/id1600482450').text,
re.S)).replace('版本 ', '')


# 获取茅台APP的版本号,暂时没找到接口,采用爬虫曲线救国
# 用bs获取指定的class更稳定,之前的正则可能需要经常改动
def get_mt_version():
# apple商店 i茅台 url
apple_imaotai_url = "https://apps.apple.com/cn/app/i%E8%8C%85%E5%8F%B0/id1600482450"
response = requests.get(apple_imaotai_url)
# 用网页自带的编码反解码,防止中文乱码
response.encoding = response.apparent_encoding
html_text = response.text
soup = BeautifulSoup(html_text, "html.parser")
elements = soup.find_all(class_="whats-new__latest__version")
# 获取p标签内的文本内容
version_text = elements[0].text
# 这里先把没有直接替换“版本 ”,因为后面不知道空格会不会在,所以先替换文字,再去掉前后空格
latest_mt_version = version_text.replace("版本", "").strip()
return latest_mt_version


mt_version = get_mt_version()

header_context = f'''
MT-Lat: 28.499562
Expand Down Expand Up @@ -51,6 +69,7 @@
'''


# 初始化请求头
def init_headers(user_id: str = '1', token: str = '2', lat: str = '29.83826', lng: str = '119.74375'):
for k in header_context.rstrip().lstrip().split("\n"):
temp_l = k.split(': ')
Expand All @@ -74,9 +93,7 @@ def signature(data: dict):
return md5


print()


# 获取登录手机验证码
def get_vcode(mobile: str):
params = {'mobile': mobile}
md5 = signature(params)
Expand All @@ -88,6 +105,7 @@ def get_vcode(mobile: str):
f'get v_code : params : {params}, response code : {responses.status_code}, response body : {responses.text}')


# 执行登录操作
def login(mobile: str, v_code: str):
params = {'mobile': mobile, 'vCode': v_code, 'ydToken': '', 'ydLogId': ''}
md5 = signature(params)
Expand All @@ -102,6 +120,7 @@ def login(mobile: str, v_code: str):
return responses.json()['data']['token'], responses.json()['data']['userId']


# 获取当日的session id
def get_current_session_id():
# print("===============get_current_session_id")
day_time = int(time.mktime(datetime.date.today().timetuple())) * 1000
Expand All @@ -116,6 +135,7 @@ def get_current_session_id():
dict.update(headers, {'current_session_id': str(current_session_id)})


# 获取最近或者出货量最大的店铺
def get_location_count(province: str,
city: str,
item_code: str,
Expand All @@ -138,6 +158,7 @@ def get_location_count(province: str,
return distance_shop(city, item_code, p_c_map, province, shops, source_data, lat, lng)


# 获取距离最近的店铺
def distance_shop(city,
item_code,
p_c_map,
Expand Down Expand Up @@ -171,6 +192,7 @@ def distance_shop(city,
return '0'


# 获取出货量最大的店铺
def max_shop(city, item_code, p_c_map, province, shops):
max_count = 0
max_shop_id = '0'
Expand Down Expand Up @@ -231,6 +253,7 @@ def send_email(msg: str):
logging.info(f'通知推送结果:{r.status_code, r.text}')


# 执行预约
def reservation(params: dict, mobile: str):
params.pop('userId')
responses = requests.post("https://app.moutai519.com.cn/xhr/front/mall/reservation/add", json=params,
Expand All @@ -249,6 +272,7 @@ def reservation(params: dict, mobile: str):
# send_email(f'预约 : mobile:{mobile} : response code : {responses.status_code}, response body : {responses.text}')


# 用高德api获取地图信息
def select_geo(i: str):
resp = requests.get(f"https://restapi.amap.com/v3/geocode/geo?key={AMAP_KEY}&output=json&address={i}")
geocodes: list = resp.json()['geocodes']
Expand Down Expand Up @@ -290,6 +314,7 @@ def get_map(lat: str = '28.499562', lng: str = '102.182324'):
return p_c_map, dict(r.json())


# 领取耐力和小茅运
def getUserEnergyAward(mobile: str):
"""
领取耐力
Expand Down
Binary file added resources/imgs/wxqr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/imgs/zfbqr.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ac842af

Please sign in to comment.