Created
October 21, 2017 21:03
-
-
Save kakakaya/1c796f482be6195c89d540cd5119babf to your computer and use it in GitHub Desktop.
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 python3 | |
# -*- coding:utf-8 -*- | |
from tempfile import TemporaryDirectory | |
import datetime | |
import tweepy | |
import requests | |
import dateparser as dp | |
from bs4 import BeautifulSoup | |
CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
CONSUMER_SEC = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
ACCESS_KEY = "XXXXXXXXXXXXX-XXXXXXXXXXXXXXXX" | |
ACCESS_SEC = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
SOURCE_DOMAIN = "http://www.aeoncinema.com" | |
SOURCE_URL = "http://www.aeoncinema.com/cinema2/chofu/movie/comingsoon.html" # 公開予定作品 | |
TWEET_FORMAT = "【新着】 {movie[cinema_title]} ({start_date}より上映予定)\n{movie[cinema_url]}" | |
TEMPORARY_DIR = TemporaryDirectory() | |
def tweet_new_movies(api, new_movie): | |
message = TWEET_FORMAT.format(movie=new_movie, start_date=new_movie['start_date'].strftime("%Y年%m月%d日")) | |
if new_movie['cinema_thumbnail_path']: | |
api.update_with_media(new_movie['cinema_thumbnail_path'], status=message) | |
else: | |
api.update_status(message) | |
def get_new_items(latest_item_date): | |
r = requests.get(SOURCE_URL) | |
if not r.ok: | |
r.status_code, r.reason | |
r.encoding = 'utf-8' | |
soup = BeautifulSoup(r.text, 'html.parser') | |
found_cinemas = [] | |
newCinema = soup.find(id='conNewCinema') | |
cDateBlocks = newCinema.find_all(class_='cDateBlock') | |
for cdb in cDateBlocks: | |
start_date = dp.parse(cdb.find(class_='startDate').text).date() | |
cinemas = cdb.find_all(class_='cinemaBlock') | |
if start_date <= latest_item_date: | |
continue | |
for cinema in cinemas: | |
cinema_thumbnail_url = SOURCE_DOMAIN + cinema.img.attrs.get('src') | |
if cinema_thumbnail_url.find("images/no_images") == -1: | |
r = requests.get(cinema_thumbnail_url) | |
with open(TEMPORARY_DIR.name + '/' + '_'.join(cinema_thumbnail_url.split('/')[-2:]), 'w+b') as f: | |
f.write(r.content) | |
cinema_thumbnail_path = f.name | |
else: | |
cinema_thumbnail_path = "" | |
this_cinema = { | |
"start_date": start_date, | |
"cinema_thumbnail_url": cinema_thumbnail_url, | |
"cinema_thumbnail_path": cinema_thumbnail_path, | |
"cinema_title": cinema.find(class_='cbTitle').text, | |
"cinema_url": SOURCE_DOMAIN + cinema.find(class_='cbTitle').a.attrs.get('href'), | |
} | |
found_cinemas.append(this_cinema) | |
return found_cinemas | |
def get_latest_date(): | |
try: | |
with open("latest.dat") as f: | |
s = f.readline() | |
return datetime.datetime.strptime(s, '%Y-%m-%d').date() | |
except FileNotFoundError: | |
return datetime.date.today() # 初回起動時は、実行時を最終取得日とする | |
def update_latest_date(latest_date): | |
with open("acc_latest.dat", "w") as f: | |
f.write(latest_date.strftime('%Y-%m-%d')) | |
def main(): | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SEC) | |
auth.set_access_token(ACCESS_KEY, ACCESS_SEC) | |
api = tweepy.API(auth) | |
latest_item_date = get_latest_date() | |
new_items = get_new_items(latest_item_date) | |
if not new_items: | |
return # nothing new | |
new_latest_date = max(new_items, key=lambda x: x['start_date'])['start_date'] | |
for new_item in new_items: | |
tweet_new_movies(api, new_item) | |
update_latest_date(new_latest_date) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment