Created
April 10, 2018 14:54
-
-
Save tookdes/6ca29ab3e08166c3691109584e67d926 to your computer and use it in GitHub Desktop.
Convert 12306 SMS Text to .ics file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import imaplib, email, re | |
| import os, time, uuid | |
| # First, you should use IFTTT to forward your SMS text to your email inbox. | |
| # That is because sometimes 12306 did not send emails to you but only SMS. | |
| # This is for those SMS text like: | |
| #【铁路客服】订单XXXXXX,XXX您已购X月X日XXXX次XX车XX号,XXX站XX:XX开。 | |
| username = '' # Your Email. | |
| passwd = '' # Your Password. | |
| mail_title = '' # Whatever you define in IFTTT. | |
| template = u"""BEGIN:VCALENDAR | |
| PRODID:-//TOOKDES.ORG//123062ICS//EN | |
| VERSION:2.0 | |
| CALSCALE:GREGORIAN | |
| METHOD:PUBLISH | |
| X-WR-CALNAME:12306 | |
| X-WR-TIMEZONE:Asia/Shanghai | |
| X-WR-CALDESC:TOOKDES.ORG 短信自动添加到 ICS 日历服务 | |
| END:VCALENDAR""" | |
| template_add = u"""BEGIN:VEVENT | |
| DTSTART:%(DTSTART)s | |
| DTEND:%(DTEND)s | |
| DTSTAMP:%(DTSTART)s | |
| UID:%(UID)s | |
| CREATED:19000101T120000Z | |
| DESCRIPTION:%(DESC)s | |
| LAST-MODIFIED:%(DTSTART)s | |
| LOCATION:%(LOC)s | |
| SEQUENCE:0 | |
| STATUS:CONFIRMED | |
| SUMMARY:%(SUMMARY)s | |
| TRANSP:OPAQUE | |
| END:VEVENT | |
| END:VCALENDAR""" | |
| def add_ics(sender,order_time_stamp,order_time_stamp_2,order_num,order_class,order_location): | |
| uid = str(uuid.uuid1()) | |
| lines = open(sender+'.ics',encoding='utf-8').readlines() | |
| icsfile = open(sender+'.ics','w',encoding='utf-8') | |
| icsfile.writelines([item for item in lines[:-1]]) | |
| icsfile.close() | |
| icsfile = open(sender+'.ics','a',encoding='utf-8') | |
| icsfile.write(template_add % dict(DTSTART=order_time_stamp,DTEND=order_time_stamp_2,DESC=order_num,SUMMARY=order_class,UID=uid,LOC=order_location) + '\n') | |
| icsfile.close() | |
| def scan_mail(): | |
| mail = imaplib.IMAP4_SSL('imap.exmail.qq.com') # Change it with your address. | |
| mail.login(username, passwd) | |
| mail.list() | |
| mail.select("inbox") | |
| result, data = mail.search(None, "ALL") | |
| ids = data[0] | |
| id_list = ids.split() | |
| for email_id in id_list: | |
| result, data = mail.fetch(email_id, "(RFC822)") | |
| raw_email = data[0][1] | |
| msg = email.message_from_bytes(raw_email) | |
| for part in msg.walk(): | |
| if part.get_content_type() == 'text/plain':# or part.get_content_type()=="text/html": | |
| content = part.get_payload(decode=True).decode("gb2312") | |
| title = email.header.decode_header(msg.get('Subject'))[0][0] | |
| if mail_title in title: | |
| sender = email.header.decode_header(msg.get("from"))[1][0] | |
| sender = re.compile(u"<(.*)>").findall(str(sender))[0] | |
| # Or you can choose not to show your email address. | |
| order_baseinfo = re.compile(u"订单(.*)您").findall(content)[0].split(",")[:2] | |
| order_num = order_baseinfo[0] | |
| # name = order_baseinfo[1] | |
| order_time_mouth = str(int(re.compile(u"已购(.*)月").findall(content)[0])) | |
| if int(order_time_mouth) < 10: | |
| order_time_mouth = str('0') + str(int(order_time_mouth)) | |
| order_time_day = str(int(re.compile(u"月(.*)日").findall(content)[0])) | |
| if int(order_time_day) < 10: | |
| order_time_day = str('0') + str(int(order_time_day)) | |
| order_date = str(time.strftime("%Y")) + order_time_mouth + order_time_day | |
| order_time = re.compile(u"站(.*)开").findall(content)[0].split(":")[:2] | |
| order_time[0] = str(int(order_time[0])) | |
| order_time[1] = str(int(order_time[1])) | |
| if int(order_time[0]) - 8 < 0: | |
| order_time[0] = str(int(order_time[0]) + 24 - 8) | |
| else: | |
| order_time[0] = str(int(order_time[0]) - 8) | |
| if int(order_time[0]) < 10: | |
| order_time[0] = str('0') + str(int(order_time[0])) | |
| if int(order_time[1]) < 10: | |
| order_time[1] = str('0') + str(int(order_time[1])) | |
| order_time_stamp = order_date + "T" + order_time[0] + order_time[1] +"00Z" | |
| order_time_stamp_2 = order_date + "T" + order_time[0] + order_time[1] +"30Z" | |
| order_location = re.compile(u"号,(.*)站").findall(content)[0] | |
| order_class = re.compile(u"日(.*)号,").findall(content)[0] + u"座" | |
| if os.path.isfile(sender+'.ics') == True: | |
| add_ics(sender,order_time_stamp,order_time_stamp_2,order_num,order_class,order_location) | |
| else: | |
| icsfile = open(sender+'.ics','w',encoding='utf-8') | |
| icsfile.write(template+'\n') | |
| icsfile.close() | |
| add_ics(sender,order_time_stamp,order_time_stamp_2,order_num,order_class,order_location) | |
| else:pass | |
| # mail.store(email_id, '+FLAGS', '\\Deleted') # commit this if you are debugging | |
| # mail.expunge() | |
| mail.logout() | |
| if __name__=="__main__": | |
| scan_mail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment