Last active
September 20, 2015 00:18
-
-
Save noexpect/3b57ab363d6ad97caf34 to your computer and use it in GitHub Desktop.
週次でその週のコミック発売日をメール通知する雑pythonスクリプト(sendgrid使用)
This file contains 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 codecs | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('shift_jis') | |
from BeautifulSoup import BeautifulSoup | |
import urllib2 | |
import re | |
import datetime | |
import locale | |
import sendgrid | |
import os | |
class scrape_web: | |
url_base = "http://www.taiyosha.co.jp/comic/comic" | |
url_tail1 = "_date1.html" | |
url_tail2 = "_date2.html" | |
book_list = "" | |
def __init__(self, yymmdd): | |
yymm = yymmdd[0] | |
dd = yymmdd[1] | |
soup = BeautifulSoup(urllib2.urlopen(self.create_url(yymm, dd))) | |
texts = soup.find("table", {"class":"table_box_new_book"}) | |
ts = unicode(texts).splitlines() | |
book_list = "" | |
for i in range(1,len(ts)-8): | |
if i % 8 == 0: | |
date = re.sub("<[^>]*?>", "", ts[i+2])[3:5] | |
publisher = re.sub("<[^>]*?>", "", ts[i+3])[:10] | |
raw_title = re.sub("<span(.*?)span>", "", ts[i+4]) | |
title = re.sub("<[^>]*?>", "", raw_title) | |
auther = re.sub("<[^>]*?>", "", ts[i+5])[:15] | |
if date == dd: | |
book_list += "<tr><td>" + title + "</td><td>" + auther + "</td><td>" + publisher + "</td></tr>" | |
self.book_list = book_list | |
def get_book_list(self): | |
return self.book_list.encode("utf_8") | |
def create_url(self, yymm, dd): | |
if int(dd) < 16: | |
return self.url_base + yymm + self.url_tail1 | |
else: | |
return self.url_base + yymm + self.url_tail2 | |
def sg_post(subject, body): | |
sg_user="hoge@mail.com" | |
sg_pass="hoge" | |
sg_to="hogehoge@mail.com" | |
sg_from="fugafuga@mail.com" | |
sg = sendgrid.SendGridClient(sg_user, sg_pass) | |
message = sendgrid.Mail() | |
message.add_to(sg_to) | |
message.set_from(sg_from) | |
message.set_subject(subject) | |
message.set_html(body) | |
sg.send(message) | |
if __name__ == '__main__': | |
today = datetime.datetime.today() | |
day_1st = today.strftime("%y%m %d") | |
day_2nd = (today + datetime.timedelta(1)).strftime("%y%m %d") | |
day_3rd = (today + datetime.timedelta(2)).strftime("%y%m %d") | |
day_4th = (today + datetime.timedelta(3)).strftime("%y%m %d") | |
day_5th = (today + datetime.timedelta(4)).strftime("%y%m %d") | |
day_6th = (today + datetime.timedelta(5)).strftime("%y%m %d") | |
day_7th = (today + datetime.timedelta(6)).strftime("%y%m %d") | |
s1 = scrape_web(day_1st.split(" ")) | |
s2 = scrape_web(day_2nd.split(" ")) | |
s3 = scrape_web(day_3rd.split(" ")) | |
s4 = scrape_web(day_4th.split(" ")) | |
s5 = scrape_web(day_5th.split(" ")) | |
s6 = scrape_web(day_6th.split(" ")) | |
s7 = scrape_web(day_7th.split(" ")) | |
tbl_start = "<hr><table border=1 cellspacing=0 cellpadding=3 ><tr><td colspan=3 >" | |
tbl_middle = " books on sale</td></tr>" | |
tbl_end = "</table>" | |
body = "<html><body>" | |
body += today.strftime("%Y/%m/%d") + "'s weekly books on sale" | |
body += tbl_start + day_1st[2:].replace(' ', '/') + tbl_middle + s1.get_book_list() + tbl_end | |
body += tbl_start + day_2nd[2:].replace(' ', '/') + tbl_middle + s2.get_book_list() + tbl_end | |
body += tbl_start + day_3rd[2:].replace(' ', '/') + tbl_middle + s3.get_book_list() + tbl_end | |
body += tbl_start + day_4th[2:].replace(' ', '/') + tbl_middle + s4.get_book_list() + tbl_end | |
body += tbl_start + day_5th[2:].replace(' ', '/') + tbl_middle + s5.get_book_list() + tbl_end | |
body += tbl_start + day_6th[2:].replace(' ', '/') + tbl_middle + s6.get_book_list() + tbl_end | |
body += tbl_start + day_7th[2:].replace(' ', '/') + tbl_middle + s7.get_book_list() + tbl_end | |
body += "</body></html>" | |
subject = "Book released this week" | |
sg_post(subject, body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment