def add_finances(entity, finances, ftype):
    # Add revenues and expenses to an entity.
    if finances:
        for f in finances:
            if ftype == 'revenue':
                years = [r.year for r in entity.revenues]
                if f['year'] not in years:
                    # Add only if year doesn't exist to avoid duplicates.
                    revenue = Revenue(f['amount'], f['year'])
                    entity.revenues.append(revenue)
                    db.flush()
                else:
                    # Update amount if year exists.
                    oldrevenue = entity.revenues.filter_by(year=f['year']).first()
                    if oldrevenue: oldrevenue.amount = f['amount']
            elif ftype == 'expenses':
                years = [e.year for e in entity.expenses]
                if f['year'] not in years:
                    # Add only if year doesn't exist to avoid duplicates.
                    expense = Expense(f['amount'], f['year'])
                    entity.expenses.append(expense)
                    db.flush()
                else:
                    # Update amount if year exists.
                    oldexpense = entity.expenses.filter_by(year=f['year']).first()
                    if oldexpense: oldexpense.amount = f['amount']
def add_finances(entity, finances, ftype):
    # Add revenues and expenses to an entity.
    if finances:
        for f in finances:
            if ftype == 'revenue':
                years = [r.year for r in entity.revenues]
                if f['year'] not in years:
                    # Add only if year doesn't exist to avoid duplicates.
                    revenue = Revenue(f['amount'], f['year'])
                    entity.revenues.append(revenue)
                    db.flush()
                else:
                    # Update amount if year exists.
                    oldrevenue = entity.revenues.filter_by(
                        year=f['year']).first()
                    if oldrevenue: oldrevenue.amount = f['amount']
            elif ftype == 'expenses':
                years = [e.year for e in entity.expenses]
                if f['year'] not in years:
                    # Add only if year doesn't exist to avoid duplicates.
                    expense = Expense(f['amount'], f['year'])
                    entity.expenses.append(expense)
                    db.flush()
                else:
                    # Update amount if year exists.
                    oldexpense = entity.expenses.filter_by(
                        year=f['year']).first()
                    if oldexpense: oldexpense.amount = f['amount']
def add_location(entity, locationtext):
    def query(location):
        url = 'http://dev.virtualearth.net/REST/v1/Locations'
        params = {
            'query': location,
            'key':
            'Ai58581yC-Sr7mcFbYTtUkS3ixE7f6ZuJnbFJCVI4hAtW1XoDEeZyidQz2gLCCyD',
            'incl': 'ciso2'
        }
        r = requests.get(url, params=params)
        responsedata = json.loads(r.text)
        return responsedata['resourceSets'][0]['resources']

    if locationtext:
        locations = locationtext.split(';')
        for location in locations:
            tries = 0
            time.sleep(0.1)

            resources = query(location)
            while len(resources) == 0 and tries < 20:
                #print 'NO RESULTS FOR ' + location + ' TRYING AGAIN'
                resources = query(location)
                tries += 1

            if len(resources):
                locationdata = resources[0]
                newlocation = Location()
                if 'formattedAddress' in locationdata['address']:
                    newlocation.full_address = locationdata['address'][
                        'formattedAddress']
                if 'addressLine' in locationdata['address']:
                    newlocation.address_line = locationdata['address'][
                        'addressLine']
                if 'locality' in locationdata['address']:
                    newlocation.locality = locationdata['address']['locality']
                if 'adminDistrict' in locationdata['address']:
                    newlocation.district = locationdata['address'][
                        'adminDistrict']
                if 'postalCode' in locationdata['address']:
                    newlocation.postal_code = locationdata['address'][
                        'postalCode']
                if 'countryRegion' in locationdata['address']:
                    newlocation.country = locationdata['address'][
                        'countryRegion']
                if 'countryRegionIso2' in locationdata['address']:
                    newlocation.country_code = locationdata['address'][
                        'countryRegionIso2']
                if 'point' in locationdata:
                    newlocation.latitude = locationdata['point'][
                        'coordinates'][0]
                    newlocation.longitude = locationdata['point'][
                        'coordinates'][1]
                entity.locations.append(newlocation)
                db.flush()
                #print 'GOT LOCATION FOR ', location
            else:
                print 'NO DATA FOUND FOR ' + location + ' ' + entity.name + ' ' + str(
                    entity.id)
                print responsedata['resourceSets']
def add_location(entity, locationtext):

    def query(location):
        url = 'http://dev.virtualearth.net/REST/v1/Locations'
        params = {'query': location, 'key': 'Ai58581yC-Sr7mcFbYTtUkS3ixE7f6ZuJnbFJCVI4hAtW1XoDEeZyidQz2gLCCyD', 'incl': 'ciso2'}
        r = requests.get(url, params=params)
        responsedata = json.loads(r.text)
        return responsedata['resourceSets'][0]['resources']
    if locationtext:
        locations = locationtext.split(';')
        for location in locations:
            tries = 0
            time.sleep(0.1)

            resources = query(location)
            while len(resources) == 0 and tries < 20:
                #print 'NO RESULTS FOR ' + location + ' TRYING AGAIN'
                resources = query(location)
                tries+=1

            if len(resources):
                locationdata = resources[0]
                newlocation = Location()
                if 'formattedAddress' in locationdata['address']:
                    newlocation.full_address = locationdata['address']['formattedAddress']
                if 'addressLine' in locationdata['address']:
                    newlocation.address_line = locationdata['address']['addressLine']
                if 'locality' in locationdata['address']:
                    newlocation.locality = locationdata['address']['locality']
                if 'adminDistrict' in locationdata['address']:
                    newlocation.district = locationdata['address']['adminDistrict']
                if 'postalCode' in locationdata['address']:
                    newlocation.postal_code = locationdata['address']['postalCode']
                if 'countryRegion' in locationdata['address']:
                    newlocation.country = locationdata['address']['countryRegion']
                if 'countryRegionIso2' in locationdata['address']:
                    newlocation.country_code = locationdata['address']['countryRegionIso2']
                if 'point' in locationdata:
                    newlocation.latitude = locationdata['point']['coordinates'][0]
                    newlocation.longitude = locationdata['point']['coordinates'][1]
                entity.locations.append(newlocation)
                db.flush()
                #print 'GOT LOCATION FOR ', location
            else:
                print 'NO DATA FOUND FOR ' + location + ' ' + entity.name + ' ' + str(entity.id)
                print responsedata['resourceSets']
def create_entity(node):
    entity = Entity(node['name'])
    entity.nickname = node['nickname']
    
    entity.entitytype = node['type']
    entity.influence = node['influence']
    if entity.influence:
        entity.influence = entity.influence.capitalize()
    entity.employees = node['employees']
    entity.url = node['url']
    entity.twitter_handle = node['twitter_handle']
    entity.followers = node['followers']
    categorize(entity, node['categories'])
    add_finances(entity, node['revenue'], 'revenue')
    add_finances(entity, node['expenses'], 'expenses')
    #add_relations(entity, node['relations'])
    connect_key_people(entity, node['key_people'])
    db.add(entity)
    db.flush()
    add_location(entity, node['location'])
    old_to_new[node['ID']] = entity.id