#!/usr/bin/python

from BeautifulSoup import BeautifulSoup
from urllib2 import *
from sys import *
import re

rawurl = "http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl?search_query=%s&go.x=0&go.y=0"

def dispChoice(soup):
	"""Function called when there are more than one search matches or no search matches
	Takes soup and returns string"""

	if len(soup.findAll("tbody")) == 1:
		print "No locations matched your search expression"
		exit()

	(cityURLs,cityNames) = ([],[])
	print "The following locations match your search expression: "
	for obj in soup.findAll("tbody")[1].findAll("tr"):
		cityURLs.append("http://www.bbc.co.uk%s" % (obj.td.a['href'],))
		cityNames.append(obj.td.a.contents[0].contents[0])
		print "%d. %s | %s" %(len(cityURLs),cityURLs[len(cityURLs)-1],cityNames[len(cityNames)-1])
	iChoice = input("Enter your choice: ")
	return cityURLs[iChoice-1]
	

def findRSS(url):
	"""Given a page url, extract the RSS URL and return it
	Takes and returns strings"""

	file = urlopen(url)
	pageURL = file.geturl()
	if "search" in pageURL:
		pageURL = dispChoice(BeautifulSoup(file.read()))
	pageID = re.search("=[0-9]*",pageURL).group(0)[1:]
	return "http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/%s.xml" % (pageID,)

def wForecast(url):
	"""Scrapes out weather information from given RSS url
	url fed from findRSS() function return
	returns None"""

	soup = BeautifulSoup(urlopen(url).read())
	for item in soup.findAll("item"):
		title = item.title.contents[0]
		titleDic = title.split(",")
		(d1,d2,d3) = (titleDic[0].split(": "),titleDic[1].split(": "),titleDic[2].split(": "))
		(day,forecast,maxRaw,minRaw) = (d1[0],d1[1],d2[1],d3[1])
		intre = re.compile("^[+-]?\d+")
		(maxtemp,mintemp) = (intre.search(maxRaw).group(0),intre.search(minRaw).group(0))
		print "%s | %s | %s | %s" % (day,forecast,maxtemp,mintemp)

def main():
	"""Handles command-line arguments and makes calls to functions
	Takes and returns None"""

	if(len(argv)) == 1:
		print "Usage: <program name> [options] <location>"
	else:
		iUrl = (rawurl % (argv[1],)).replace(' ','+')
		wForecast(findRSS(iUrl))

if __name__ == "__main__": main()
