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
| import telegram
import logging
import time
import datetime
from threading import Timer
from ion import getIndexPromo,getPromoContent
status_dic = {"ion":""}
def getIONPromo():
index_promo = getIndexPromo()
if status_dic['ion'] != index_promo:
bot.send_message(chat_id='@IWannaBuy', text="检测到ION优惠信息发生变化\n原优惠信息:" + status_dic['ion'] + "\n现优惠信息:" + index_promo , parse_mode=telegram.ParseMode.HTML)
bot.send_message(chat_id='@IWannaBuy', text= getPromoContent() ,parse_mode=telegram.ParseMode.HTML)
bot.send_message(chat_id='@IWannaBuy', text= "[前往购买-国际版链接](https://ion.krypt.com/promotion.php) [国内链接](https://ion.krypt.asia/promotion.php)" ,parse_mode=telegram.ParseMode.MARKDOWN)
status_dic['ion'] = index_promo
Timer(60,getIONPromo).start()
#机器人开始运行
bot = telegram.Bot(token = "")
bot.send_message(chat_id='@IWannaBuy', text="机器人开始运行" ,parse_mode=telegram.ParseMode.HTML)
getIONPromo()
#定时获取ION促销页面信息,如果发现变化则推送
#ion.py
from urllib import request
from bs4 import BeautifulSoup
#定时查询。返回给机器人
index_url="https://ion.krypt.com/index.php"
promo_url="https://ion.krypt.com/promotion.php"
headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Mobile Safari/537.36'}
req = request.Request(url = index_url, headers = headers)
index_data = ""
promo_data = ""
def getIndexPromo():
with request.urlopen(req) as response:
index_data = response.read()
# print(index_data)
#解析页面
soup = BeautifulSoup(index_data,'lxml')
notification = soup.find(id="notification")
#notification 类型是Tag
#index页面的优惠信息
index_promo = notification.find("div","container").find("p").text
return index_promo
def getPromoContent():
req = request.Request(url = promo_url,headers = headers)
with request.urlopen(req) as response:
promo_data = response.read()
soup = BeautifulSoup(promo_data,"lxml")
promo_raw_content = soup.find(id="content")
promo_contents = promo_raw_content.text.splitlines()
while "" in promo_contents:
promo_contents.remove("")
promo_content = "\n".join(promo_contents)
return promo_content
|