Last active
December 11, 2015 19:19
-
-
Save relax-more/4647757 to your computer and use it in GitHub Desktop.
URの気に入った部屋の空き部屋を検索して、空きがあればメールで連絡くれるツール
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
#!/usr/bin/env python | |
import sys | |
import urllib2 | |
from BeautifulSoup import BeautifulSoup | |
import smtplib | |
from email.MIMEText import MIMEText | |
from email.Utils import formatdate | |
from_addr = '[email protected]' | |
to_addr = '[email protected]' | |
password = 'password' | |
targetUrls = 'targetUrl.txt' | |
opener = urllib2.build_opener() | |
def checkURL(): | |
for url in open(targetUrls, 'r'): | |
soup = BeautifulSoup(opener.open(url)) | |
result =soup.findAll('div',{'class':'detail_links'}) | |
try: | |
result[0] | |
print 'data exist: ' + ''.join(soup.find('title',)) | |
msg = create_message(from_addr, to_addr, "[UR Searcher] exist enpty room:" + "".join(soup.find('title',)) ,"URL :" + url) | |
send_via_gmail(from_addr, to_addr, msg) | |
except IndexError: | |
print 'no data: ' + ''.join(soup.find('title', )) | |
def send(from_addr, to_addr, msg): | |
s = smtplib.SMTP() | |
s.sendmail(from_addr, [to_addr], msg.as_string()) | |
s.close() | |
def send_via_gmail(from_addr, to_addr, msg): | |
s = smtplib.SMTP('smtp.gmail.com', 587) | |
s.ehlo() | |
s.starttls() | |
s.ehlo() | |
s.login(from_addr, password) | |
s.sendmail(from_addr, to_addr, msg.as_string()) | |
s.close() | |
def create_message(from_addr, to_addr, subject, body): | |
msg = MIMEText(body) | |
msg['Subject'] = subject | |
msg['From'] = from_addr | |
msg['To'] = to_addr | |
msg['Date'] = formatdate() | |
return msg | |
if __name__ == '__main__': | |
checkURL() | |
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
■Pythonでファイルを開く | |
http://osksn2.hep.sci.osaka-u.ac.jp/~taku/osx/python/readfile.html | |
■Gmailの送信方法 | |
http://unoh.github.com/2007/06/13/python_2.html | |
■beautiflsoupで div タグとClassを指定してデータをとる | |
http://d.hatena.ne.jp/hs_31/20111230/1325258547 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment