4
\$\begingroup\$

I am pretty new to Python as a frontend dev and I loved it, so I started to build a tool fetching the Facebook Places API.

I don't know where to optimize it but I need you to take a look at my code as a better pythonist and tell me how my code is looking.

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

import requests
from time import sleep
import random
import json
import unicodecsv as csv
import facebook
import urllib2

facebook_api_key = ""
google_api_key = ''


### Facebook Page Finder
def fbPlaceFinder(geocode, query,radius):
    graph = facebook.GraphAPI(access_token=facebook_api_key, version=2.7)
    places = graph.request('/search?type=place&q=' + query + '&center=' + geocode + '&distance='+radius+'&fields=name,checkins,picture')

    x = random.randint(30,120)
    print('resting %s sec' % (x))
    sleep(x)

    allplaces = []

    while True:
        try:
            for place in places['data']:
                allplaces.append(place)
            places = requests.get(places['paging']['next']).json() # add next page of results to json

            x = random.randint(30,120)
            print('resting %s sec' % (x))
            sleep(x)

        except KeyError:
            break
    return allplaces

def getAllPlacesDetailed(places):
    place_count = len(places)

    print('Company found: ', place_count)

    all_places_list = []
    # while True:
    try:
        for x in range(place_count):
            pageid = places[x]['id']
            # get details about page

            try:
                page = requests.get('https://graph.facebook.com/v2.7/' + pageid + '?access_token=' + facebook_api_key + '&fields=link,id,name,contact_address,single_line_address,location,phone,whatsapp_number,emails,website,engagement')
            except:
                print('error in details')

            page_details_single = page.json()
            print('Getting company details: ' + (page_details_single['name']))
            all_places_list.append(page_details_single)
            x = random.randint(30,120)
            print('details: resting %s sec' % (x))
            sleep(x)
    except:
        print("Error")
    finally:
        return all_places_list



def citiesCountry(country_code, file):
    with open(file) as json_file:  ## open city dictionary
        input = json.load(json_file)
        json_file.close()
        # output = [city for city in input if city["country"] == country_code] #  Old line to use with cities_of_turkey.json file
        output = [city for city in input if city["country"] == country_code]
        return output



###### LET THE SHOW BEGIN ######



query = 'irrigation'
countries = {
    'RO':'irrigatii',
    'SI':'Namakalni',
    "SK": "Zavlažovací",
    'BG':'Напоителна',
    'GR':'αρδευση',
    'RS':'наводњавање',
    'AL':'ujitjes',
    'MK':'наводнување',
    'HR':'navodnjavanja'
}


for k,v in countries.items():
    country = k
    a = v.decode("utf8")
    b = unicode(a)
    # print(b)


    for country in countries:

        city_list = citiesCountry(country, 'input/world_cities_lang.json')

        outputFolder = 'output/' + country + '_' + query
        all_country_list = []

        try:
            for city in city_list:
                city_geo = city['lat'] + ',' + city['lng']
                name = city['name']
                # lang_local = city["language"]
                # query_local = translateText(query, lang_local)

                status = 'Trying to find results for: ' + b + ' in ' + name + ', ' + country + ', ' + city_geo
                print(status)

                cityplaces = fbPlaceFinder(city_geo, b, '10000')
                city_places_list = getAllPlacesDetailed(cityplaces)
                # with open(outputFolder + '_' + city + '.json', 'w') as outfile:
                #     json.dump(city_places_list, outfile)
                all_country_list.extend(city_places_list)
                print ('-' * 25)

        except KeyError:
            print('error: ' + KeyError)
        finally:
            with open(outputFolder + '.json', 'w') as outfile:
                json.dump(all_country_list, outfile)
            print('country completed')
\$\endgroup\$
  • 1
    \$\begingroup\$ problem is solved: there were lots of running screen processes in background which i forgot to terminate. forgot 10 scripts running at same time and that was why i was getting api rate limit error. \$\endgroup\$ – fatihkoc Mar 19 '18 at 4:31