forked from westnestling/N95-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_push.py
184 lines (158 loc) · 5.85 KB
/
wechat_push.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import sys
import tinify
import json
from urllib.request import Request, urlopen
import time
import requests
import sys
import random
from PIL import Image
from selenium import webdriver
# 导入模块
basedir = os.path.abspath(os.path.dirname(__file__))
from wxpy import *
import itchat
def login():
# bot = Bot(cache_path=True)
bot = Bot()
groups = bot.groups()
group1 = bot.groups().search(keywords='口罩消毒液放货监控群')
group = bot.groups().search(keywords='口罩消毒液放货监控群')[0]
print(group)
while True:
group.send('有货啦')
return bot
def get_bot():
bot = Bot('bot.pkl', qr_path=os.path.join(
basedir, 'QR.png'), console_qr=None)
# bot.enable_puid()
# bot.messages.max_history = 0
return bot
# bot = get_bot()
def save_jd_url(url):
try:
fo = open("src/jdurl.txt", "r")
lines = fo.readlines()
fo.close()
fo = open("src/jdurl.txt", "w")
lines.append("\"" + url + "\",\n")
fo.writelines(lines)
fo.close()
except Exception:
print("读取data失败")
def save_tm_url(url):
try:
fo = open("src/tmurl.txt", "r")
lines = fo.readlines()
fo.close()
fo = open("src/tmurl.txt", "w")
lines.append("\"" + url + "\",\n")
fo.writelines(lines)
fo.close()
except Exception:
print("读取data失败")
def get_outfile(infile, outfile):
if outfile:
return outfile
dir, suffix = os.path.splitext(infile)
outfile = '{}-out{}'.format(dir, suffix)
return outfile
def resize_image(infile, outfile='', x_s=1124):
"""修改图片尺寸
:param infile: 图片源文件
:param outfile: 重设尺寸文件保存地址
:param x_s: 设置的宽度
:return:
"""
im = Image.open(infile)
x, y = im.size
y_s = int(y * x_s / x)
out = im.resize((x_s, y_s), Image.ANTIALIAS)
outfile = get_outfile(infile, outfile)
out.save(outfile)
# 推送data.txt中的数据 并且通过接口查询是否有货
if __name__ == '__main__':
print(os.path.join(os.path.dirname(__file__)))
browser = webdriver.Chrome(os.path.join(os.path.dirname(__file__) + "/src", "chromedriver"))
bot = Bot()
# 修改群名
group = bot.groups().search(keywords='口罩消毒液放货监控群')[0]
group2 = bot.groups().search(keywords='口罩消毒液监控群')[0]
url = "http://invoice.pinhai.com.cn/index/maskloglist"
# 包装头部
firefox_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
# 构建请求
pre_time = ""
i = 0
data_map = {}
while (1):
if (i + 1) % 5 == 0:
print("已为您成功您监控" + str(i + 1) + "次")
try:
# 读取data.txt中的数据并推送
file = open("data.txt")
try:
for line in file:
if line[21:50] not in data_map:
group2.send("发现有货!!!\n" + line)
# 压缩图片到500k以下
file_name = "src/imgs/"+line[:19]
try:
resize_image(file_name + ".png", file_name + "_copy.png")
group.send("发现有货!!!\n" + line)
group2.send_image(file_name+"_copy.png", media_id=None)
group.send_image(file_name+"_copy.png", media_id=None)
except Exception:
data_map[line[21:50]] = line[21:50]
continue
data_map[line[21:50]] = line[21:50]
print("读取data.txt结束")
except Exception:
print("读取data失败")
finally:
file.close()
# 注释开始
request = Request(url, headers=firefox_headers)
html = urlopen(request)
data = html.read()
strs = str(data, 'UTF-8')
list = json.loads(strs)['data']['maskloglists']
data_json = list[-1]
title = data_json["title"]
if pre_time != data_json["ctime"] and data_json["title"] not in data_map:
print(data_json)
data_map[data_json["title"]] = data_json['url']
data = "商品名:【" + data_json["title"] + "】\n" + data_json["card_text"] + "\n购买链接:" + data_json[
'url']
print(data)
# 再次检查
browser.get(data_json['url'])
time.sleep(3)
find_flag = False
for keyword in ['无货', '下柜', '已下架', '不支持销售', '售完', '卖光','京东(JD.COM)-正品低价']:
if keyword in browser.page_source:
find_flag = keyword
break
if find_flag is not False:
continue
# 发送微信信息
img_name = "imgs/" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
browser.save_screenshot(img_name+ "B.png")
group2.send(data)
resize_image(img_name + "B.png", img_name + "B_copy.png")
group2.send_image(img_name + "B_copy.png", media_id=None)
# time.sleep(1)
group.send(data)
url_save = data_json['url']
if "jd" in url_save:
save_jd_url(url_save)
elif "tmall" in url_save:
save_tm_url(url_save)
print("推送成功")
except Exception:
print("第" + str(i + 1) + "次推送挂掉了")
i = i - 1
sleep_time = 10 + int(random.random() * 10)
time.sleep(sleep_time)
i = i + 1