Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 62 lines (49 sloc) 2.01 KB
#! /usr/bin/python
# python script for phatIO webometer example
# instructions at http://www.phatio/com/ideas/webometer/
import imaplib
import sys
import json
import urllib2
from xml.dom import minidom
import datetime
# returns the number of unread messages in the imap account
def num_unread_messages(server,port,login,password):
obj = imaplib.IMAP4_SSL(server, port)
obj.login(login, password)'Inbox')
typ, msgnums =,'UnSeen')
# if string of message ids is not zero length split and count
if len(msgnums[0]):
return len(msgnums[0].rsplit(' '))
return 0
# returns the named json element
def get_json_element(url,element):
webdata = urllib2.urlopen(url)
jsondata = json.load(webdata)
return jsondata[element]
# returns the xml dom from the specified url
def get_xml(url):
webdata = urllib2.urlopen(url)
return minidom.parse(webdata)
# returns the temperature of the given citycode in the degrees units 'c' or 'f'
# see for description
def get_yahoo_temp(citycode,units):
WEATHER_URL = '{}&w={}'.format(units,citycode)
dom = get_xml(WEATHER_URL)
condition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
return int(condition.getAttribute('temp'))
if len(sys.argv) == 2:
file = open(sys.argv[1], 'w')
timenow =
# change the following for your details
unread = num_unread_messages('',993,'YOUR_EMAIL_ADDRESS','YOUR_IMAP_PASSWORD')
followers = get_json_element('', 'followers_count')
temp = get_yahoo_temp(26194557,'c') # helsinki temperature
file.write('{:02d}{:02d}{:4d}{:4d}{:4d}'.format(timenow.hour, timenow.minute, unread, followers, temp))
print "usage {} <outputfilename> ".format(sys.argv[0])
print " for example {} /mntpoint/io/dev/number ".format(sys.argv[0])