<commit_before>#!/usr/bin/python
import argparse
import urllib2
from urllib import quote
import json
from math import radians, sin, cos, sqrt, asin

import asciiweather as aw


DEBUG = False

APIKEY = '<KEY>'
APIURL = "http://api.wunderground.com/api/" + APIKEY
icons = dict(clear=aw.clear, cloudy=aw.cloudy, partlycloudy=aw.partlycloudy, mostlycloudy=aw.partlycloudy,
             rain=aw.rainy, tstorms=aw.storm)  # TODO: make a config file with this stuff


def haversine(lat1, lon1, lat2, lon2):  # http://rosettacode.org/wiki/Haversine_formula#Python

    r = 6372.8  # Earth radius in kilometers
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlat = lat2 - lat1
    dlon = lon2 - lon1


    a = sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2
    c = 2*asin(sqrt(a))

    return r * c


def loadjson(url):
    req = urllib2.Request(url)
    opener = urllib2.build_opener()
    f = opener.open(req)
    data = json.loads(f.read())
    return data


def geoip():
    response = loadjson("http://ip-api.com/json")
    if response["status"] == "success":
        return response


def conditions(locurl):
    cond = loadjson(APIURL + "/conditions" + locurl)
    try:
        return cond['current_observation']
    except:
        raise ValueError


def geolookup(loc):
    loc = loc.lower()
    url = APIURL + "/geolookup/q/" + quote(loc) + '.json'
    response = loadjson(url)
    if 'results' in response['response'].keys():  # Time to disambiguate!
        options = response['response']['results']
        locality = geoip()
        disambiguated = []
        strval = lambda x: {str(y) for y in x.values()}
        lc = strval(locality)
        with open("clean.json") as f:
            bigcities = json.load(f)
        for option in options:
            op = strval(option)
            if loc in bigcities.keys():
                weighted = 0
                best = []
                for entry in bigcities[loc]:
                    distance = haversine(entry['Longitude'], entry['Latitude'], locality['lat'], locality['lon'])
                    population = entry['Population']
                    if (population/distance) > weighted:
                        weighted = (population/distance)
                        best = entry
                return geolookup(' '.join([loc, best['Country']]))
            elif len(op & lc) > len(disambiguated):  # checks for raw similarity
                disambiguated = option
                return geolookup(disambiguated['zmw'])
    else:
        r = loadjson(url)

        return r['location']['l'] + '.json'

def forecast(locurl):
    fc = loadjson(APIURL + "/forecast/q/" + locurl)
    return fc['forecast']['simpleforecast']['forecastday']





def parseresponse(r):  # TODO: check the flow of this function
    temperature = list(str(r['temp_f']).split('.')[0])
    asciitemp = []
    for x in range(6):
        line = []
        for y in range(len(temperature)):
            row = aw.numbers[int(temperature[y])][x]
            line.append(row)
        asciitemp.append(''.join(line))
    parsed = dict(temp=tuple(asciitemp),
                  icon=tuple(icons[r['icon']]),
                  time=("Local time: " + r['local_time_rfc822'],),
                  wind=("Wind: " + r['wind_string'],),
                  humidity=("Humidity: " + r['relative_humidity'],),
                  name=(r['display_location']['full'],)
                  )

    return parsed


def rowbuild(rowdict):
    splitlocs = {}
    for obj in rowdict:
        xpos = rowdict[obj][0]
        ypos = rowdict[obj][1]
        if isinstance(type(obj), unicode):
            splitlocs[(xpos, ypos)] = obj
        else:
            for i, line in enumerate(obj):
                splitlocs[(xpos, ypos+i)] = line
    return splitlocs


def gridfill(rowdict):
    offset = max(line[0] for line in rowdict)
    width = 70
    length = max([y[1] for y in rowdict.keys()]) + 1
    grid = [[" " for x in range(width)] for y in range(length)]
    for y in range(length):
        for x in range(width):
            if (x, y) in rowdict.keys():
                for index, item in enumerate(rowdict[(x, y)]):
                    try:
                        grid[y][x+index] = item
                    except IndexError:
                        print "Index error"

    return [''.join(x) for x in grid]
# TODO: call these functions to draw the image


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('time', default='now', choices=['now','tomorrow','week'])
    parser.add_argument('location', default='here',
    args = parser.parse_args()
    location = ' '.join(args.location)
    time = args.time
    if location == "here":
        geo = geoip()
        location = geo['city'] + geo['region'] + geo['country']

    if time == "now":  # TODO: move this stuff to its own time parsing function
        disp = parseresponse(conditions(geolookup(location)))
        offsets = {disp['icon']: (0, 0), disp['temp']: (23, 1), disp['name']: (21, 0), disp['time']: (23, 9), disp['wind']: (24, 10)}
        for line in gridfill(rowbuild(offsets)):
            print line
    elif time == "tomorrow":
        disp = forecast(geolookup(location))[0]
    else:
        disp = forecast(geolookup(location))

    # TODO: add in the argparse stuff
<commit_msg>it wooooorks<commit_after>#!/usr/bin/python
import argparse
import urllib2
from urllib import quote
import json
from math import radians, sin, cos, sqrt, asin

import asciiweather as aw


DEBUG = False

APIKEY = '<KEY>'
APIURL = "http://api.wunderground.com/api/" + APIKEY
icons = dict(clear=aw.clear, cloudy=aw.cloudy, partlycloudy=aw.partlycloudy, mostlycloudy=aw.partlycloudy,
             rain=aw.rainy, tstorms=aw.storm)  # TODO: make a config file with this stuff


def haversine(lat1, lon1, lat2, lon2):  # http://rosettacode.org/wiki/Haversine_formula#Python

    r = 6372.8  # Earth radius in kilometers
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlat = lat2 - lat1
    dlon = lon2 - lon1


    a = sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2
    c = 2*asin(sqrt(a))

    return r * c


def loadjson(url):
    req = urllib2.Request(url)
    opener = urllib2.build_opener()
    f = opener.open(req)
    data = json.loads(f.read())
    return data


def geoip():
    response = loadjson("http://ip-api.com/json")
    if response["status"] == "success":
        return response


def conditions(locurl):
    cond = loadjson(APIURL + "/conditions" + locurl)
    try:
        return cond['current_observation']
    except:
        raise ValueError


def geolookup(loc):
    loc = loc.lower()
    url = APIURL + "/geolookup/q/" + quote(loc) + '.json'
    response = loadjson(url)
    if 'results' in response['response'].keys():  # Time to disambiguate!
        options = response['response']['results']
        locality = geoip()
        disambiguated = []
        strval = lambda x: {str(y) for y in x.values()}
        lc = strval(locality)
        with open("clean.json") as f:
            bigcities = json.load(f)
        for option in options:
            op = strval(option)
            if loc in bigcities.keys():
                weighted = 0
                best = []
                for entry in bigcities[loc]:
                    distance = haversine(entry['Longitude'], entry['Latitude'], locality['lat'], locality['lon'])
                    population = entry['Population']
                    if (population/distance) > weighted:
                        weighted = (population/distance)
                        best = entry
                return geolookup(' '.join([loc, best['Country']]))
            elif len(op & lc) > len(disambiguated):  # checks for raw similarity
                disambiguated = option
                return geolookup(disambiguated['zmw'])
    else:
        r = loadjson(url)

        return r['location']['l'] + '.json'

def forecast(locurl):
    fc = loadjson(APIURL + "/forecast/q/" + locurl)
    return fc['forecast']['simpleforecast']['forecastday']





def parseresponse(r):  # TODO: check the flow of this function
    temperature = list(str(r['temp_f']).split('.')[0])
    asciitemp = []
    for x in range(6):
        line = []
        for y in range(len(temperature)):
            row = aw.numbers[int(temperature[y])][x]
            line.append(row)
        asciitemp.append(''.join(line))
    parsed = dict(temp=tuple(asciitemp),
                  icon=tuple(icons[r['icon']]),
                  time=("Local time: " + r['local_time_rfc822'],),
                  wind=("Wind: " + r['wind_string'],),
                  humidity=("Humidity: " + r['relative_humidity'],),
                  name=(r['display_location']['full'],)
                  )

    return parsed


def rowbuild(rowdict):
    splitlocs = {}
    for obj in rowdict:
        xpos = rowdict[obj][0]
        ypos = rowdict[obj][1]
        if isinstance(type(obj), unicode):
            splitlocs[(xpos, ypos)] = obj
        else:
            for i, line in enumerate(obj):
                splitlocs[(xpos, ypos+i)] = line
    return splitlocs


def gridfill(rowdict):
    offset = max(line[0] for line in rowdict)
    width = 70
    length = max([y[1] for y in rowdict.keys()]) + 1
    grid = [[" " for x in range(width)] for y in range(length)]
    for y in range(length):
        for x in range(width):
            if (x, y) in rowdict.keys():
                for index, item in enumerate(rowdict[(x, y)]):
                    try:
                        grid[y][x+index] = item
                    except IndexError:
                        print "Index error"

    return [''.join(x) for x in grid]
# TODO: call these functions to draw the image


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
   #  parser.add_argument('time', default='now', choices=['now','tomorrow','week'])
    parser.add_argument('location', default='here', nargs='+')
    args = parser.parse_args()
    location = ' '.join(args.location)
    time = "now"  # args.time
    if location == "here":
        geo = geoip()
        location = geo['city'] + geo['region'] + geo['country']

    if time == "now":  # TODO: move this stuff to its own time parsing function
        disp = parseresponse(conditions(geolookup(location)))
        offsets = {disp['icon']: (0, 0), disp['temp']: (23, 1), disp['name']: (21, 0), disp['time']: (23, 9), disp['wind']: (24, 10)}
        for line in gridfill(rowbuild(offsets)):
            print line
    elif time == "tomorrow":
        disp = forecast(geolookup(location))[0]
    else:
        disp = forecast(geolookup(location))

    # TODO: add in the argparse stuff

{'fraction_non_alphanumeric': 0.1439259328532685, 'fraction_numerical': 0.01122229495931918, 'mean_word_length': 2.297563983965464, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7501408', 'n_tokens_mistral': 3503, 'n_tokens_neox': 3308, 'n_words': 847}