Python email reminder script

Source: Internet
Author: User

Before February, the iPhone official website provided insufficient supply, so I wrote a reminder script to regularly refresh the page. If the conditions are met, I sent an email reminder,

The first time I did something with python, I was deeply impressed by the simplicity, development efficiency, and rich libraries of Python.

1 #! /Usr/bin/Python
2 #-*-coding: UTF-8 -*-
3 # import sys
4 Import httplib
5 import smtplib
6 Import time
7
8
9 # Read mailing list from specified file
10 def read_mailing_list (filename ):
11 mailing_list = []
12 F = file (filename, 'R ')
13 while true:
14 line = f. Readline ();
15 if Len (line)> 0:
16 mailing_list.append (line. Strip ())
17 else:
18 break
19 F. Close ()
20 return mailing_list;
21
22
23 # Check if there is some iPhone to buy
24 def check_iphone_status (resp_content ):
25 s = 'Estimated shipping time'
26 Pos = resp_content.find (s)
27 if pos! =-1:
28 tag1 = '<span>'
29 tag2 = '</span>'
30 pos1 = resp_content.find (tag1, POS)
31 pos2 = resp_content.find (tag2, POS)
32 If pos1! =-1 and pos2! =-1:
33 DST = resp_content [pos1 + Len (tag1): pos2]
34 DST = ''. Join (DST. Split ())
35 # print DST
36 if DST = 'unavailable temporarily ':
37 # print 'no iPhone 4S'
38 return-1
39 else:
40 print 'hello, iPhone 4S'
41 return 0
42 else:
43 return-1
44 else:
45 return-1
46
47
48 # send mail
49 def send_mail (_ from, _ to, _ MSG ):
50 mailsrv = smtplib. SMTP ('smtp .163.com ')
51 mailsrv. login ('xxx', 'xxxxxx ')
52 # mailsrv. EHLO ()
53 # mailsrv. starttls ()
54 # mailsrv. EHLO ()
55 mailsrv. Sendmail (_ from, _ to, _ MSG)
56 mailsrv. Quit ()
57
58
59 # script start to run
60 # Read mailing list
61 mailing_list = read_mailing_list ('mailing _ list ')
62 print 'mailing list: ', mailing_list
63 mail_from = 'xxxx @ 163.com'
64 # mail_to = ','. Join (mailing_list)
65 mail_msg = 'subject: iPhone 4S in sale \ n \ nplease visit: \ n \
66 http://www.apple.com.cn \ n \
67 http://store.apple.com/cnw.n \
68 http://store.apple.com/cn/browse/home/shop_iphone/family/iphone'
69 mail_msg2 = 'subject: iPhone 4S timed out \ n \ nplease visit: \ n \
70 http://www.apple.com.cn \ n \
71 http://store.apple.com/cnw.n \
72 http://store.apple.com/cn/browse/home/shop_iphone/family/iphone'
73
74 F = file ('log', 'w ')
75 F. Write ('mailing list: \ n ')
76 for item in mailing_list:
77 F. Write (item + '\ n ')
78 print item
79 F. Write ('\ n ')
80 f. Flush ()
81
82 # Send http request
83 while true:
84 conn = httplib. httpconnection ('store .apple.com ')
85 conn. Request ('get', '/CN/Browse/home/shop_iphone/family/iPhone/iphone4s ')
86 result = conn. getresponse ()
87 resp_status = result. Status
88 If resp_status = 200:
89 resp_content = result. Read ()
90 Status = check_iphone_status (resp_content)
91 If status = 0:
92 # send_mail (mail_from, mailing_list, mail_msg)
93 F. Write ('iphone 4S in sale \ n ')
94 F. Flush ()
95 print 'iphone 4S in sale'
96 else:
97 # send_mail (mail_from, mailing_list, mail_msg2)
98 # print 'send mail done'
99 F. Write ('iphone 4S timed out \ n ')
100 F. Flush ()
101 print 'iphone 4S timed out'
102 conn. Close ()
103 time. Sleep (60)
104
105 f. Close ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.