Telegram优惠监控/推送机器人

前天凑巧看见ion双十一活动,买了一台洛杉矶的机器,感觉挺不错的,不过当我想再买一台圣何塞的机器时,发现已经没货了,所以我想着黑五的时候再买一台,因为这机器还算是比较抢手的,所以有没有更方法提前得知机器开卖呢?

电报优惠监控与消息推送机器人

前天凑巧看见ion双十一活动,买了一台洛杉矶的机器,感觉挺不错的,不过当我想再买一台圣何塞的机器时,发现已经没货了,所以我想着黑五的时候再买一台,因为这机器还算是比较抢手的,所以有没有更方法提前得知机器开卖呢?

晚上花了一两个小时,捡起来差不多两年没用过的python,实现了一个简单的机器人,用来监控优惠信息的更新并推送到tg群组里面。

不过目前机器人还只有个雏形,只有最基本的功能,未来有空了我可能会扩展这个机器人的功能,不过那可能是一段时间以后的事情啦,机器人到底有没有用我也不知道,先接受黑五的检验吧。

代码实现

目前分为两部分 bot.py 和 ion.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
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
Licensed under CC BY-NC-SA 4.0
最后更新于 Nov 11, 2020 18:04 UTC
comments powered by Disqus
本站访客数:
Built with Hugo
主题 StackJimmy 设计