def get_info(postcode):
    """Get info from postcode

    Key arguments:
    postcode   --  The postcode given (string)

    Return a dict with the info, and error message if the code is invalid
    """

    if is_valid_postcode(postcode):
        return PostCoder().get(postcode)
    return {'error': 'Your postcode is invalid'}
Esempio n. 2
0
def PostcodeInfo(Postcode):
    Postcode = Postcode.upper()
    pc = PostCoder()
    result = pc.get(Postcode)

    if str(result) != "None":
        location = result['geo']
        for key, value in location.items():
            if (key == "lat"):
                loc_lat = value
            if (key == "lng"):
                loc_long = value

        location = result['administrative']
        for key, value in location.items():
            if (key == "constituency"):
                for key1, value1 in value.items():
                    if (key1 == "title"):
                        loc_const = value1
            if (key == "district"):
                for key2, value2 in value.items():
                    if (key2 == "title"):
                        loc_dist = value2

        return (json.dumps(
            {
                "Postcode": Postcode,
                "lat": loc_lat,
                "lng": loc_long,
                "const": loc_const,
                "coun": loc_dist,
                "IP": socket.gethostbyname(socket.gethostname())
            },
            sort_keys=True))
        # write_to_file = Postcode + ", " + str(loc_lat) + ", " + str(loc_long) + ", " + str(loc_const) + ", " + str(loc_dist) + "\n"
    else:
        return (json.dumps({"Postcode": Postcode, "lat": "N/A"}))
Esempio n. 3
0
import os
from postcodes import PostCoder

pc = PostCoder()


def postcode_to_coords(n):  #Find coordinates from postcode
    result = pc.get(n)
    if result is None:
        return None, None
    result = result['geo']
    latitude = result['lat']
    longitude = result['lng']
    return latitude, longitude


for file in os.listdir('.'):
    if file.endswith('.csv'):
        handle = open(file, 'r')
        data = ''
        latitude = longitude = price = 0
        for row in handle:
            split = row.split(',')
            price = split[1]
            postcode = split[3]
            latitude, longitude = postcode_to_coords(postcode)
            print price, latitude, longitude
            if (not latitude is None) and (not longitude is None) and (
                    not price is None):
                data += price + ',' + latitude + ',' + longitude + '\n'
        if data != '':
Esempio n. 4
0
def get_geo(postcode):
    pc = PostCoder()
    result = pc.get(postcode)
    return result['geo']['lat'], result['geo']['lng'], result[
        'administrative']['council']['title']
Esempio n. 5
0
 def setUp(self):
     self.pc = PostCoder()