Monday, 30 March 2015

Py: Temperature info update speaker out after some time automatic in mac os x

Basically the concept is simple, we are going to write few lines of code in python to get temperature from some URL (openweathermap.org). Then make a string which should speak out by speaker.
Then finally make this python script into cron job into your Mac OS X.

Step 1: Write simple python script code

import urllib # Web service fetch
import json   # JSON parse 
import os     # text to speech convert

## get data from server
url = 'http://api.openweathermap.org/data/2.5/weather?q=Jaipur,In&units=metric'

u = urllib.urlopen(url)

## json parse 
data = json.load(u)


## make final string which should speak out
finalMessage = 'In '+data['name']+ ' temprature is '+ str(data['main']['temp']) + ' celcius. Forcasting says it is expacting '+data['weather'][0]['main']


### finally text to speak out
os.system("say "+ finalMessage)
#os.system("echo "+ finalMessage)

Step 2 : Check the program is working
python weather.py



Step 3 : Make a crone job

$ crontab -l // list all running cron jobs
$ crontab -e // add / edit cron job list
$ crontab -r // remove cron job

3.1-> open terminal

3.2-> crontab -e
If you want to change the editor VI to NANO; use this command
env EDITOR=nano crontab -e

3.3-> write 15 * * * * python /Users/username/Desktop/weather.py
may be timer separator use tab. Above line says that this job will run in every 15 min. for every minute use * five time with tab separator. for more detail about cron you can google or read from here
15 * * * * python /Users/username/Desktop/weather.py

3.4-> press (VI) :x and you got a message "crontab: installing new crontab"
If you are using NANO use control+x then enter then y. [^x ; enter ; y]

Thats it, Voila...


 

No comments:

Post a Comment