예제 #1
0
def main():
   if not has_internet():
       print "You must be connected to the Internet."
       return 1

   if len(sys.argv) != 4:
      print "usage: ./google_places.py <location> <keyword> <radius in mi>"
      print "   ex: ./google_places.py \"Somerville, MA\" \"Indian\" 1"
      return 1
   google_places = GooglePlaces(YOUR_API_KEY)

   #
   # Convert location to lat/long
   #
   #lat_long = google_places.geocode_location("Brighton, MA", sensor=False)
   #for k in lat_long:
   #   print(k)

   #exit()

   # You may prefer to use the text_search API, instead.
   query_result = google_places.nearby_search(
           location=sys.argv[1], keyword=sys.argv[2],
           radius=(float(sys.argv[3])*1609.34), types=[types.TYPE_FOOD])

   if query_result.has_attributions:
       print query_result.html_attributions


   for place in query_result.places:
       # Returned places from a query are place summaries.
       print place.name
       #print place.geo_location
       print "Lat/Lng [%s, %s]" % (place.geo_location['lat'], place.geo_location['lng'])
       #print place.place_id

       # The following method has to make a further API call.
       place.get_details()
       # Referencing any of the attributes below, prior to making a call to
       # get_details() will raise a googleplaces.GooglePlacesAttributeError.
       #print place.details # A dict matching the JSON response from Google.
       print place.formatted_address
       print place.local_phone_number
       print place.international_phone_number
       print "Website: %s" % place.website
       print "URL:     %s" % place.url
       print ""
예제 #2
0
def main():
    if not has_internet():
        print "You must be connected to the Internet."
        return 1

    parser = OptionParser()
    parser.add_option(
        "-x",
        "--reset-db",
        action="store_true",
        dest="reset_db",
        default=False,
        help="Reset the places collection in the db. Requires confirmation.",
    )
    parser.add_option(
        "-a",
        "--add-location",
        dest="add_location",
        help="Add some places to the db. Arg is a string of keywords. Used with -r.",
    )
    parser.add_option(
        "-q",
        "--query-location",
        dest="query_location",
        help="Query some places from the db. Arg is address. Used with -r.",
    )
    parser.add_option(
        "-r",
        "--radius",
        dest="radius",
        action="store",
        type="float",
        default=1.0,
        help="The radius (float) to use when adding to or querying the db.",
    )
    parser.add_option("-k", "--keywords", dest="keywords", help="The keywords to use for adding places to the db.")
    parser.add_option(
        "-d", "--debug", dest="debug", action="store_true", default=False, help="Enable debugging output."
    )
    (options, args) = parser.parse_args()

    if options.debug:
        print "Option parser debugging help:"
        print "reset_db       %s" % options.reset_db
        print "add_location   %s" % options.add_location
        print "query_location %s" % options.query_location
        if options.radius != None:
            print "radius         %f" % options.radius
        print "keywords       %s" % options.keywords
        print "debug          %s" % str(options.debug)

    if (
        options.reset_db == False
        and options.add_location == None
        and options.query_location == None
        and options.keywords == None
    ):
        help_them("You must specify something.")
        return 1

    if options.add_location == None and options.query_location == None and options.radius != 1.0:
        help_them("--radius must be used with --add-location and --query-location.")
        return 1

    if (options.add_location != None or options.query_location != None) and options.radius == None:
        help_them("--add-location and --query-location commands are used in conjunction with" " --radius.")
        return 1

    if options.add_location != None and options.query_location != None:
        help_them("--add-location and --query-location cannot both be specified.")
        return 1

    #
    # They have specified some legitmate arguements so connect to database.
    #
    fg = FoodGeospacial()
    if not fg.connect_db():
        print "Error connecting to database."
        assert False

    if options.reset_db:
        print "Are you sure you want to reset the places collection in the database?"
        proceed = raw_input("Type 'yes': ")
        if proceed.upper() == "YES":
            fg.reset_db()
            return 0

    if options.add_location != None:
        if options.radius == None:
            help_them("--add-location needs the --radius arguement.")
            return 1
        if options.keywords == None:
            help_them("--add-location needs the --keywords arguement.")
            return 1
        fg.load_db(options.add_location, options.keywords, options.radius)
        return 0

    if options.query_location != None and options.keywords == None:
        if options.radius == None:
            help_them("--query-location needs the --radius argument.")
            return 1
        fg.query_db_addr(options.query_location, options.radius)
        return 0

    if options.query_location != None:
        if options.radius == None:
            help_them("--query-location needs the --radius argument.")
            return 1
        fg.query_db_addr_keywords(options.query_location, options.radius, options.keywords)
        return 0
예제 #3
0
def main():
    if not has_internet():
        print "You must be connected to the Internet."
        return 1

    db = connect_db()
    if db == None:
        print "Error creating database client"
        return 1

    parser = OptionParser()
    parser.add_option("-x", "--reset-db", action="store_true", dest="reset_db", default=False,
                      help="Reset the places collection in the db. Requires confirmation.")
    parser.add_option("-a", "--add-location", dest="add_location",
                      help="Add some places to the db. Arg is a string of keywords. Used with -r.")
    parser.add_option("-q", "--query-location", dest="query_location",
                      help="Query some places from the db. Arg is address. Used with -r.")
    parser.add_option("-r", "--radius", dest="radius", action="store", type="float", default=1.0,
                      help="The radius (float) to use when adding to or querying the db.")
    parser.add_option("-k", "--keywords", dest="keywords",
                      help="The keywords to use for adding places to the db.")
    (options, args) = parser.parse_args()

   
    #
    # For debugging command line args.
    #
    #print "reset_db %s" % options.reset_db
    #print "add_location %s" % options.add_location
    #print "query_location %s" % options.query_location
    #if options.radius != None:
    #    print "radius %f" % options.radius
    #print "keywords %s" % options.keywords

    if (options.reset_db == False and options.add_location == None and
        options.query_location == None and options.keywords == None):
           help_them("You must specify something.")
           return 1

    if (options.add_location == None and
        options.query_location == None and
        options.radius != None):
           help_them("--radius must be used with --add-location and --query-location.")
           return 1

    if (options.add_location == None and
        options.keywords != None):
           help_them("--keywords must be used with --add-location only.")
           return 1
 
    if (options.add_location != None or options.query_location != None) and options.radius == None:
        help_them("--add-location and --query-location commands are used in conjunction with"
                  " --radius.")
        return 1

    if options.reset_db:
        print "Are you sure you want to reset the places collection in the database?"
        proceed = raw_input("Type 'yes': ")
        if proceed.upper() == "YES":
            reset_db(db)
            return 0

    if options.add_location != None and options.query_location != None:
        help_them("--add-location and --query-location cannot both be specified.")
        return 1

    if options.add_location != None:
        if options.radius == None:
            help_them("--add-location needs the --radius arguement.")
            return 1
        if options.keywords == None:
            help_them("--add-location needs the --keywords arguement.")
            return 1
        load_db(db, options.add_location, options.keywords, options.radius) 

    if options.query_location != None:
        if options.radius == None:
            help_them("--query-location needs the --radius argument.")
            return 1
        query_db_addr(db, options.query_location, options.radius)
예제 #4
0
def main():
    if not has_internet():
        print "You must be connected to the internet to use this."
        return 1
    #
    # Confirm they are using the commands right.
    #
    if len(sys.argv) == 1:
        print_usage_str()
        return 1
    if len(sys.argv) > 2:
        cmd = sys.argv[1].upper()
    if cmd.upper() == "DEFINE":
        if len(sys.argv) != 4:
            print_usage_str()
            return 1
        collection_file = sys.argv[2]
    elif cmd.upper() == "ANALYZE":
        if len(sys.argv) != 3:
            print_usage_str()
            return 1
        collection_file = sys.argv[2]
    else:
        print "Unrecognized command '%s'" % cmd
        return 1

    #
    # Make sure the definitions file exists and is a definitions/groups file.
    #
    assert collection_file != ""
    if not os.path.isfile(collection_file):
        #
        # TODO: Once we have meta information in the json files, check that for the file type.
        #
        print "That word definitions/group file doesn't exist!"
        return 1
    if "GROUPS" not in collection_file.upper() and "DEFINITIONS" not in collection_file.upper():
        print "This isn't a word definitions/groups file."
        print "It doesn't have 'groups' or 'definitions' in the name"
        return 1

    #
    # Open the file and build the collection structure. Duplicates will be highlighted.
    #
    collection = {}
    all_words = []
    with open(collection_file) as data_file:
        collection = json.load(data_file)
        modified = False
        duplicates = False
        for group in collection:
            for word in collection[group]:
                if word not in all_words:
                    all_words.append(word)
                else:
                    duplicates = True
                    group_it_belongs_to = ""
                    for group_i in collection:
                        if word in collection[group_i]:
                            print "WARNING: word '%s' was found in multiple groups including '%s'" % (word, group_i)
    if duplicates:
        print "There were duplicate words across word groups."

    if cmd == "DEFINE":
        word = sys.argv[3]
        definition_full = define_dictionary_full(word)
        if definition_full == "":
            print "It looks like there was an error getting the definition. Did you spell the word correctly?"
            return 1
        if word not in all_words:
            print "This word isn't anywhere in the collection, let's add it."
            definition = definition_prompt(word)
            words_in_group = []
            words_in_group.append(word)
            if definition in collection:
                print "WARNING: that definition/group already exists! Not adding the word!"
                return 1
            collection[definition] = words_in_group
            modified = True
        else:
            print "I think I've seen this word before in that file"
            found = False
            for group in collection:
                if word in collection[group]:
                    print "I found this word already under '%s'" % group
                    found = True
            assert found

    elif cmd == "ANALYZE":
        for group in collection:
            #
            # If we have a definition request in the answer. define the word and offer up our best
            # parsed version. If they don't like it, they can reject it and type it in themselves.
            #
            if "DEFINEME" in group:
                word_list = collection[group]
                first_word = word_list[0]
                print ""
                if len(word_list) > 1:
                    print "WARNING: Found a request to define word group that starts with word '%s'" % first_word
                    print "This is a word group, so we will attempt to define the group with the first word's definition."
                else:
                    print "I found a request to define the word '%s'" % first_word
                #
                # Let them decide if they want the Dictionary.com definition or their own custom definition.
                #
                definition = definition_prompt(first_word)
                if definition in collection:
                    print "WARNING: that definition/group already exists! Not adding the word!"
                    return 1
                collection.pop(group, None)
                collection[definition] = word_list
                modified = True
    else:
        print "Unknown command '%s'" % cmd

    if modified:
        with open(collection_file, "w") as outfile:
            json.dump(collection, outfile, indent=4, sort_keys=True)
        print ""
        print "Successfully saved changes to file %s" % collection_file
    else:
        print ""
        print "There were no modifications to the file."