Last active
August 29, 2015 14:08
-
-
Save seungjin/510466565de62b2d6fa2 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
#!/bin/env python27 | |
# -- coding: utf-8 -- | |
import requests | |
import dataset | |
from sqlalchemy.exc import OperationalError | |
import time | |
from bs4 import BeautifulSoup | |
import smtplib, os | |
#from email.MIMEMultipart import MIMEMultipart | |
#from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.Utils import COMMASPACE, formatdate | |
#from email import Encoders | |
import json | |
from local_storage import Local_storage | |
def get_aqicn_beijing(): | |
resource_url = 'http://aqicn.org/city/beijing/' | |
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0'} | |
return requests.get(resource_url, headers=headers).text.encode('utf-8') | |
def get_aqi_val(html): | |
soup = BeautifulSoup(html) | |
api_table = soup.find_all(class_="api")[0] | |
api_table.find_all(class_="aqivalue") | |
value = api_table.find_all(class_="aqivalue")[0].get_text() | |
description = api_table.find_all(class_="aqivalue")[0] | |
updated_time = api_table.find_all(style="font-size:16px;font-weight:light;;")[0].get_text().split(" ")[3:][0] | |
return value, description['title'], updated_time | |
def send_email(text): | |
send_from = "" | |
send_to = [""] | |
subject = text | |
#msg = MIMEMultipart() | |
msg = MIMEText(None) | |
msg['From'] = send_from | |
msg['To'] = COMMASPACE.join(send_to) | |
msg['Date'] = formatdate(localtime=True) | |
msg['Subject'] = subject | |
#msg.attach( MIMEText(text) ) | |
smtp = smtplib.SMTP("localhost") | |
smtp.sendmail(send_from, send_to, msg.as_string()) | |
smtp.close() | |
if __name__ == "__main__": | |
value, description, updated_time = get_aqi_val(get_aqicn_beijing()) | |
print value, description, updated_time | |
if int(value) > 100 : | |
lc = Local_storage() | |
if lc.is_it_new(name='beijing_aqi',value='{0} {1} {2}'.format(value,description,updated_time)): | |
#send_email("Current Beijing AQI is {0} ({1}); Updated at: {2}".format(value, description, updated_time)) | |
print "email sent" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment