Kirim SMS jika ada film baru /人 ◕ ‿‿ ◕ 人\
from bs4 import BeautifulSoup
from ConfigParser import SafeConfigParser
from twilio.rest import Client
import hashlib
import json
import logging
import os
import redis
import requests
logPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "log.log")
logging.basicConfig(filename=logPath,
level=logging.DEBUG,
format='%(asctime)s %(message)s')
config = SafeConfigParser()
configPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.ini")
config.read(configPath)
CINEMA21URL = config.get("cinema", "URL")
# Google Search API Constant
G_SEARCH_URL = config.get("google", "URL")
G_SEARCH_CX = config.get("google", "CX")
G_SEARCH_KEY = config.get("google", "KEY")
# Twilio API Constant
T_USER = config.get("twilio", "USER_ID")
T_TOKEN = config.get("twilio", "TOKEN_AUTH")
T_FROM = config.get("twilio", "NO_FROM")
T_TO = config.get("twilio", "NO_TO")
def sendSMS(content):
client = Client(T_USER, T_TOKEN)
message = client.messages.create(
to=T_TO,
from_=T_FROM,
body=content)
# print(message.sid)
def UTF8(data):
return data.encode('utf-8').strip()
# Get source from cinema21's website
# and passing source to html parser
r = requests.get(CINEMA21URL)
soup = BeautifulSoup(UTF8(r.text), "html.parser")
movies = soup.find_all(class_="light")
movies.extend(soup.find_all(class_="dark"))
redisCon = redis.StrictRedis(host='localhost', port=6379, db=0)
upRating = list()
downRating = list()
noRating = list()
for movie in movies:
title_tag = movie.td.a.next_sibling
title = UTF8(title_tag.text.lower().replace(" ", "+"))
title_real = UTF8(title_tag.text)
title_hash = UTF8(hashlib.md5(title).hexdigest())
# print title, title_hash, title_real
data = redisCon.get(title_hash)
if data == None:
# Save movie title in redis for 4 weeks
redisCon.set(title_hash, title, 2419200)
URL = (G_SEARCH_URL + "q=" + title + "&cx=" +
G_SEARCH_CX + "&key=" + G_SEARCH_KEY)
result = json.loads(requests.get(URL).text)
pagemap = result["items"][0]["pagemap"]
if "aggregaterating" in pagemap:
rating = UTF8(pagemap["aggregaterating"][0]["ratingvalue"])
if float(rating) > 7.3:
time = UTF8(movie.td.next_sibling.div.text).replace("\xc2\xa0", "")
time = time.split(" ")
if len(time) > 3:
time = " ".join(time[-3:])
else:
time = " ".join(time)
logging.info(title_real + ":Good movie, maybe :)")
upRating.append(title_real + " (" + rating + ") " + time)
elif float(rating) > 6.0:
logging.info(title_real + ":Movie has rating below standard :(")
downRating.append(title_real + " (" + rating + ")")
else:
logging.info(title_real + ":I don't wanna watch this movie")
else:
logging.info(title_real + ":Movie has no rating, yet")
noRating.append(title_real)
else:
logging.info(title_real + ":Skip, notification was already sent")
# Construct, at least-maybe, movies worth watching
message = ("\n".join(upRating)) + "\n"
if downRating:
message += "\n".join(downRating) + "\n"
if noRating:
message += "\n".join(noRating)
if len(message) > 1:
logging.info(":Send SMS:" + message)
sendSMS(message)
0 10 * * * /usr/bin/python PATH/movie.py
[cinema]
URL = http://www.21cineplex.com/theater/bioskop-kuningan-city-xxi,319,JKTKUCI.htm
[google]
URL = https://www.googleapis.com/customsearch/v1?
CX = 0********************:***_****-i
KEY = AI*************************************
[twilio]
USER_ID = AC********************************
TOKEN_AUTH = 32******************************
NO_FROM = +*********94
NO_TO = +***********18