lundi 5 juin 2017

Python and Google API :: error retrieving JSON (if,else)

I have my code to parse JSON from Google API:

def get_movietheaters_for(latitude, longitude):
    connection2 = pyodbc.connect('DRIVER={SQL Server};'                                  
                                 'SERVER=myserver;'                                     
                                 'DATABASE=worldcitiespop;'                             
                                 'UID=myuser;'                                      
                                 'PWD=mypassword')                                      
    cursor2 = connection2.cursor()                                                      
    #Google                                                                             

    url = 'http://ift.tt/1bGFprp?'                 
    params = {'location': '%s,%s' % (latitude,longitude),                                   
            'radius': '5000',                                               
            'type': 'movie_theater',
            'key': 'MY_KEY'}                                                        
    response = requests.get(url = url, params=params)                                                           
    response_data = response.json()                                                     

    sqlStatement = "INSERT INTO VistaGoogle (ID,Place_ID,Name,Full_Address,Latitude,Longitude,Types,Rating) values (?,?,?,?,?,?,?,?)" 
    # Here we go to store JSON elements for SQL
    if response_data['status'] == 'ZERO_RESULTS':
        print ('\nNothing found for','Lat:', latitude,'Lon:', longitude)

    elif 'results' in response_data:
        for SQL_element in response_data['results']:
            SQL_ID = SQL_element['id']                                                      
            SQL_Place_ID = SQL_element['place_id']
            SQL_Name = SQL_element['name']  
            SQL_Full_Address = SQL_element['formatted_address']
            SQL_Latitude = SQL_element['geometry']['location']['lat']
            SQL_Longitude = SQL_element['geometry']['location']['lng']
            SQL_Types = SQL_element['types'][0]
            SQL_Rating = SQL_element['rating']

            if args.do == 'show':   
                print (SQL_Name,SQL_Full_Address,SQL_Latitude,SQL_Longitude,SQL_Types,SQL_Rating)
            elif args.do == 'save':    
                cursor2.execute(sqlStatement, SQL_ID,SQL_Place_ID,SQL_Name,SQL_Full_Address,SQL_Latitude,SQL_Longitude,SQL_Types,SQL_Rating)
                connection2.commit()

        if args.do == 'show':
            print ('\nTotal Cinemas found: ' , len(response_data['results']),' for Latitude and Longitude ', latitude, longitude)
        elif args.do == 'save':
            print ('\nTotal Cinemas found and saved in database: ' , len(response_data['results']),'Lat:', latitude,'Lon:', longitude)



def SQLQuery():
    # We connect to SQL Server Management Studio
    connection = pyodbc.connect('DRIVER={SQL Server};'                                  
                                'SERVER=myserver;'                                      
                                'DATABASE=worldcitiespop;'                              
                                'UID=myuser;'                                           
                                'PWD=mypassword')                                           
    cursor = connection.cursor()
    try:    
        cursor.execute(args.SQL)
        for latitude, longitude in cursor:
            get_movietheaters_for(latitude, longitude)
    finally:
        cursor.close()
        connection.close()  
SQLQuery()

I submit my query for Latitude and Longitude -36.86666700 174.76666700 I have 2 problems

enter image description here

Problem 1

Python can retrieve the rating without problem till the 14th element, then says KeyError: 'rating'. Why it had no problem retrieving the previous 13 cinema rating?

This is because sometimes Cinemas has no ratings. what to do in those case?

Problem 2

I save the cinemas in the database and Python says:

Total Cinemas found and saved in database:  0 Lat: -36.86666700 Lon: 174.76666700

But it was finding those cinemas?!

Aucun commentaire:

Enregistrer un commentaire