# coding: utf-8

# kind.py
# Uppflettingar á ja.is
# Version: 0.5c
#
# Copyright 2009 Benedikt Kristinsson
#
# This program is distributed under the terms of the GPL
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import urllib
from BeautifulSoup import BeautifulSoup
import sys

class kind:
    def __init__(self, simanr):
        # Athuga hvort að símanúmerið sé nokkurnvegin valid (7 tölustafir)
        try:
            numerint = int(simanr)
            if not len(simanr) == 7:
                print "Það er vitlaus lengd á símanúmerinu"
                sys.exit()
        except:
            print "Það vantar símanúmerið"
            sys.exit()

        # Skipta um version
        class AppURLopener(urllib.FancyURLopener):
            version = "kind/0.5c"

        # Mobile síðan dömpuð því hún var 10x stærri.
        # Mobile: 26kb, full síða: 2.8kb
        params = urllib.urlencode({'q': simanr})
        f = urllib.urlopen("http://www.ja.is/hradleit/?%s" % params)

        # Súpan sigtuð
        self.soup = BeautifulSoup(f)

    def get_nafn(self):
        return ''.join(self.soup.find("span", {"class" : "fn"}))
        
    def get_addr(self):
         street = ''.join(self.soup.find("a", {"class" : "street-address"}))
         locality = ''.join(self.soup.find("i", {"class" : "locality"}))  
        
         returnme = street + "\n" + locality
         return returnme
         
         
if __name__ == "__main__":
    #
    
    numer = sys.argv[1]
       
    x = kind(numer)
    print x.get_nafn()
  
    try:
        allt = sys.argv[2]
        if allt[:2] == "-a":
            print x.get_addr()
    except:
        pass

