|
| 1 | +import httpx |
| 2 | +import mail |
| 3 | +import datetime |
| 4 | +from bs4 import BeautifulSoup as bs4 |
| 5 | +import re |
| 6 | +import config as conf |
| 7 | +import os |
| 8 | + |
| 9 | +class BjBusClock(): |
| 10 | + def __init__(self, logger, config=None, ): |
| 11 | + self.logger = logger |
| 12 | + import json |
| 13 | + if config is None: |
| 14 | + with open(r"C:\config.json", "r", encoding='UTF-8') as config_file: |
| 15 | + config = json.load(config_file) |
| 16 | + if config is None: |
| 17 | + raise "没有提供配置且没找到配置文件" |
| 18 | + |
| 19 | + self.config = conf.Config(config) # 整体配置 |
| 20 | + |
| 21 | + if self.config.mailConfig is None: |
| 22 | + raise "没有发送通知的邮箱配置" |
| 23 | + self.config.alertMail = config.get("alertMail", None) |
| 24 | + if self.config.alertMail is None: |
| 25 | + raise "没有接收通知的邮箱地址" |
| 26 | + self.mailClient = mail.MailSent(self.config.mailConfig) |
| 27 | + self.logger.info("BusClock 初始化完毕") |
| 28 | + |
| 29 | + |
| 30 | + def run(self): |
| 31 | + self.logger.info("BusClock 执行运算") |
| 32 | + for line in self.config.lines: |
| 33 | + self.logger.info("BusClock 执行线路:" + "@" + line['line']) |
| 34 | + if self.onTime(line['begin'], line['end']): |
| 35 | + self.logger.info("BusClock 达到提示时间 " + "@" + line['line']) |
| 36 | + if line.get('needSentMail', True): |
| 37 | + self.logger.info("BusClock 需要发提醒 " + "@" + line['line']) |
| 38 | + bustime = self.getBusTime(line) |
| 39 | + if bustime is None: |
| 40 | + self.logger.info("BusClock 尚未发车: " + "@" + line['line']) |
| 41 | + else: |
| 42 | + self.logger.info("BusClock 得到车辆时间: " + bustime + "@" + line['line']) |
| 43 | + if int(bustime) <= int(line.get('latestLeaveMinute', self.config.latestLeaveMinute)): |
| 44 | + self.logger.info("BusClock 可以发邮件提醒了" + "@" + line['line']) |
| 45 | + self.mailClient.send_mail(self.config.alertMail, '班车提醒: '+line['line'], '车辆即将到站,现在出发正当时') |
| 46 | + self.logger.info("BusClock 邮件提醒已发送" + "@" + line['line']) |
| 47 | + line['needSentMail'] = False # 发送通知后,不必再发了 |
| 48 | + else: |
| 49 | + self.logger.info("BusClock 没到发邮件的时候" + "@" + line['line']) |
| 50 | + else: |
| 51 | + self.logger.info("BusClock 已经发过邮件了" + "@" + line['line']) |
| 52 | + else: |
| 53 | + line['needSentMail'] = True |
| 54 | + self.logger.info("BusClock 未到时间窗口" + "@" + line['line']) |
| 55 | + |
| 56 | + |
| 57 | + def onTime(self, begin, end): |
| 58 | + d_time = datetime.datetime.strptime( |
| 59 | + str(datetime.datetime.now().date())+begin, '%Y-%m-%d%H:%M') |
| 60 | + d_time1 = datetime.datetime.strptime( |
| 61 | + str(datetime.datetime.now().date())+end, '%Y-%m-%d%H:%M') |
| 62 | + n_time = datetime.datetime.now() |
| 63 | + if n_time > d_time and n_time < d_time1: |
| 64 | + return True |
| 65 | + else: |
| 66 | + return False |
| 67 | + |
| 68 | + def getBusTime(self, line): |
| 69 | + url = 'http://www.bjbus.com/home/ajax_rtbus_data.php?act=busTime&selBLine=%(line)s&selBDir=%(dir)s&selBStop=%(stop)s' % line |
| 70 | + result = None |
| 71 | + try: |
| 72 | + r = httpx.get(url).json() |
| 73 | + b = bs4(r.get('html'), 'html.parser') |
| 74 | + info = b.find('article') |
| 75 | + i = info.find_all('p')[1] |
| 76 | + ret = re.search(r'\d+(?=\s分钟)', i.text) |
| 77 | + result = None |
| 78 | + if ret: |
| 79 | + result = ret.group() |
| 80 | + except BaseException: |
| 81 | + pass |
| 82 | + |
| 83 | + return result |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + import logging |
| 88 | + logging.basicConfig(filename=r'D:\busclock.log', level=logging.INFO) |
| 89 | + bus = BjBusClock(logging, { |
| 90 | + "loopWaitSeconds": 60, |
| 91 | + "spurtWaitSeconds": 10, |
| 92 | + "latestLeaveMinute": 5, |
| 93 | + "mailConfig": { |
| 94 | + "FROM": "tom@example.com", |
| 95 | + "HOST": "smtp.example.com", |
| 96 | + "PORT": "465", |
| 97 | + "USER": "tom", |
| 98 | + "PASS": "password", |
| 99 | + "SSL": True |
| 100 | + }, |
| 101 | + "alertMail": "lily@example.com", |
| 102 | + "lines": [{ |
| 103 | + "line": "835快", |
| 104 | + "dir": "5066222788346588777", |
| 105 | + "stop": "13", |
| 106 | + "begin": "08:00", |
| 107 | + "end": "08:30" |
| 108 | + }, { |
| 109 | + "line": "835快", |
| 110 | + "dir": "4997908670784162973", |
| 111 | + "stop": "3", |
| 112 | + "begin": "19:00", |
| 113 | + "end": "20:30" |
| 114 | + }] |
| 115 | + }) |
| 116 | + print(bus.onTime("12:00", "13:00")) |
| 117 | + print(bus.getBusTime(bus.config.lines[0])) |
| 118 | + print(bus.run()) |
0 commit comments