Friday, July 13, 2012

Python Script to fetch the current weather information


Here is a python script that will get the current weather information of any place. This script makes use of Google API.

How to run the script
python script_name   place_name/postal_code

Example:
Download the code and save it in get_weather.py file
Now to get the present weather information of London, execute the following command
python get_weather.py london

To get the weather information of Bonn, whose pin code is 53175, execute the following
python get_weather.py 53175

Following is the code:

import sys
import urllib2
from xml.dom.minidom import parse, parseString
from time import gmtime, strftime

url = "http://www.google.com/ig/api?weather="
url = url+sys.argv[1]

u = urllib2.urlopen(url)
filename="weather_report_"+sys.argv[1]+".xml"
localFile = open(filename, 'w')
localFile.write(u.read())
localFile.close()

#Parse the XML file to extract important information
doc=parse(filename)

# location info
fi = doc.getElementsByTagName("forecast_information")[0]

city = fi.childNodes[0]
print "City: " + city.attributes["data"].value

pc = fi.childNodes[1]
print "Postal Code: " + pc.attributes["data"].value

#weather info
cc=doc.getElementsByTagName("current_conditions")[0]

cond = cc.childNodes[0]
print "Type: " + cond.attributes["data"].value

temp_f=cc.childNodes[1]
temp_c=cc.childNodes[2]
print "Temperature: " + temp_f.attributes["data"].value + " F / " +temp_c.attributes["data"].value + " C"

humidity = cc.childNodes[3]
print humidity.attributes["data"].value

wc = cc.childNodes[5]
print wc.attributes["data"].value

Tuesday, January 3, 2012

Python Script to fetch the facebook profile picture

Here is the code

# load a given picture from a web page and save it to a file
# (you have to be on the internet to do this)
# tested with Python24     vegaseat     19sep2006

import urllib2
import webbrowser
import os
import sys

# find yourself a picture on a web page you like
# (right click on the picture, look under properties and copy the address)
id = sys.argv[1];
picture_page = "https://graph.facebook.com/"+id+"/picture?type=large"

#webbrowser.open(picture_page)  # test

# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 = opener1.open(picture_page)
my_picture = page1.read()

# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = "my_image" + picture_page[-4:]
print filename  # test
fout = open(filename, "wb")
fout.write(my_picture)
fout.close()

# was it saved correctly?
# test it out ...
webbrowser.open(filename)

# or ...
# on Windows this will display the image in the default viewer
#os.startfile(filename)


How to run:
1. download the code and save it as picture.py
2. open the terminal and naviage to the folder containing the file picture.py
3. execute the command
    python picture.py zuck
    where "zuck" is the username of Mark Zuckerberg.

Cheers!!!!