123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2023/07/11 20:25:17
- @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
- @Desc :
- '''
- import argparse,time,json
- from email.mime.text import MIMEText
- from email.header import Header
- import os,sys,re,configparser,logging,requests
- import smtplib,random
- from fuck12306 import api
- class Crawl12306:
- ''' 12306 抢票助手 '''
- def __init__(self):
- self.sess=requests.Session()
- self.args=self.get_args()
- self.conf=self.get_conf()
- def get_conf(self)->dict:
- ''' read config.ini and return a dict'''
- cf = configparser.ConfigParser()
- cf.read("conf/config.ini")
- return cf
-
- def get_args(self):
- parser=argparse.ArgumentParser(description='12306 抢票助手')
- parser.add_argument('-d','--date',help='出发日期')
- parser.add_argument('-f','--from',help='出发地')
- parser.add_argument('-t','--to',help='目的地')
- parser.add_argument('-m','--mail',help='接收通知的邮箱')
- parser.add_argument('-c','--config',help='配置文件')
- return parser.parse_args()
- def get_code_pic(self, number=100):
- ''' get code picture '''
- print("............... get code pic ...........")
- for i in range(number):
- res= self.sess.get(api.pic_url)
- time.sleep(random.randint(3,5))
- if res.status_code == 200:
- with open("data/pic/pic_%s%s.png" % (int(time.time(),i),), "wb") as file:
- file.write(res.content)
- # 监控余票
- def monitor(self):
- print("余票监控中...")
- while True:
- try:
- res=self.sess.get(self.conf["url"]["left_ticket_url"])
- if res.status_code == 200:
- j = json.loads(r.text)
- k = j['data']
- str1 = ""
- strData = ["G6305","D7501","G6325","D7521","D7533","D7529","D2381","G6337","D7525","G6321","D7513","G6313","G1607","G6309","D7533","D7517","G6329","D7505","G6345","G6341"]
- for r in k['result']:
- for cs in strData:
- if "|" + cs+"|" in r:
- if r.split('|')[-7] != '无': #二等座
- str1 = str1 + cs + ',有票啦~~\n'
- if len(str1)>5:
- print(str1)
- self.send_mail(str1)
- sys.stdout.write('\r')
- sys.stdout.write('已查询%d次~' % self.num)
- sys.stdout.flush()
- self.num+=1
- else:
- self.send_mail("获取车票信息失败~~")
- except Exception as e:
- pass
- finally:
- pass
- time.sleep(5,10)
- self.send_mail('有票了')
- def send_mail(self,content:str):
- message = MIMEText(content, 'plain', 'utf-8')
- message['From'] = Header("肥肥", 'utf-8')
- message['To'] = Header("圆圆", 'utf-8')
- message['Subject'] = Header("余票提醒", 'utf-8')
- try:
- smtpObj = smtplib.SMTP()
- smtpObj.connect(self.conf["mail"]["host"], 25) # 25 为 SMTP 端口号
- smtpObj.login(self.conf["mail"]["user"], self.conf["mail"]["password"])
- smtpObj.sendmail(self.conf["mail"]["sender"], self.conf["mail"]["receiver"], message.as_string())
- print("邮件发送成功")
- except Exception as e:
- print("Error: 无法发送邮件")
- finally:
- pass
- @staticmethod
- def check_update():
- ''' check application update '''
- pass
-
|