crawl_12306.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/07/11 20:25:17
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc :
  8. '''
  9. import argparse,time,json
  10. from email.mime.text import MIMEText
  11. from email.header import Header
  12. import os,sys,re,configparser,logging,requests
  13. import smtplib,random
  14. from fuck12306 import api
  15. class Crawl12306:
  16. ''' 12306 抢票助手 '''
  17. def __init__(self):
  18. self.sess=requests.Session()
  19. self.args=self.get_args()
  20. self.conf=self.get_conf()
  21. def get_conf(self)->dict:
  22. ''' read config.ini and return a dict'''
  23. cf = configparser.ConfigParser()
  24. cf.read("conf/config.ini")
  25. return cf
  26. def get_args(self):
  27. parser=argparse.ArgumentParser(description='12306 抢票助手')
  28. parser.add_argument('-d','--date',help='出发日期')
  29. parser.add_argument('-f','--from',help='出发地')
  30. parser.add_argument('-t','--to',help='目的地')
  31. parser.add_argument('-m','--mail',help='接收通知的邮箱')
  32. parser.add_argument('-c','--config',help='配置文件')
  33. return parser.parse_args()
  34. def get_code_pic(self, number=100):
  35. ''' get code picture '''
  36. print("............... get code pic ...........")
  37. for i in range(number):
  38. res= self.sess.get(api.pic_url)
  39. time.sleep(random.randint(3,5))
  40. if res.status_code == 200:
  41. with open("data/pic/pic_%s%s.png" % (int(time.time(),i),), "wb") as file:
  42. file.write(res.content)
  43. # 监控余票
  44. def monitor(self):
  45. print("余票监控中...")
  46. while True:
  47. try:
  48. res=self.sess.get(self.conf["url"]["left_ticket_url"])
  49. if res.status_code == 200:
  50. j = json.loads(r.text)
  51. k = j['data']
  52. str1 = ""
  53. strData = ["G6305","D7501","G6325","D7521","D7533","D7529","D2381","G6337","D7525","G6321","D7513","G6313","G1607","G6309","D7533","D7517","G6329","D7505","G6345","G6341"]
  54. for r in k['result']:
  55. for cs in strData:
  56. if "|" + cs+"|" in r:
  57. if r.split('|')[-7] != '无': #二等座
  58. str1 = str1 + cs + ',有票啦~~\n'
  59. if len(str1)>5:
  60. print(str1)
  61. self.send_mail(str1)
  62. sys.stdout.write('\r')
  63. sys.stdout.write('已查询%d次~' % self.num)
  64. sys.stdout.flush()
  65. self.num+=1
  66. else:
  67. self.send_mail("获取车票信息失败~~")
  68. except Exception as e:
  69. pass
  70. finally:
  71. pass
  72. time.sleep(5,10)
  73. self.send_mail('有票了')
  74. def send_mail(self,content:str):
  75. message = MIMEText(content, 'plain', 'utf-8')
  76. message['From'] = Header("肥肥", 'utf-8')
  77. message['To'] = Header("圆圆", 'utf-8')
  78. message['Subject'] = Header("余票提醒", 'utf-8')
  79. try:
  80. smtpObj = smtplib.SMTP()
  81. smtpObj.connect(self.conf["mail"]["host"], 25) # 25 为 SMTP 端口号
  82. smtpObj.login(self.conf["mail"]["user"], self.conf["mail"]["password"])
  83. smtpObj.sendmail(self.conf["mail"]["sender"], self.conf["mail"]["receiver"], message.as_string())
  84. print("邮件发送成功")
  85. except Exception as e:
  86. print("Error: 无法发送邮件")
  87. finally:
  88. pass
  89. @staticmethod
  90. def check_update():
  91. ''' check application update '''
  92. pass