예제 #1
0
def search():
    """Search for places that match query"""
    #get location
    qs = request.args.get("q")
    eprint(qs)

    qs = qs.split(',')
    for i in range(len(qs)):
        qs[i] = qs[i] + "%"

    if len(qs) == 1:
        results = db.execute(
            "SELECT * FROM places WHERE postal_code LIKE :q0 OR place_name LIKE :q0 OR admin_name1 LIKE :q0",
            q0=qs[0])
    elif len(qs) == 2:
        results = db.execute(
            "SELECT * FROM places WHERE place_name LIKE :q0 AND (admin_name1 LIKE :q1 OR admin_code1 LIKE :q1)",
            q0=qs[0],
            q1=qs[1])
    elif len(qs) == 3:
        results = db.execute(
            "SELECT * FROM places WHERE place_name LIKE :q0 AND (admin_name1 LIKE :q1 OR admin_code1 LIKE :q1) AND country_code LIKE :q2",
            q0=qs[0],
            q1=qs[1],
            q2=qs[2])
    return jsonify(results)
예제 #2
0
def lookup(geo):
    """Look up articles for geo"""

    # Check cache
    try:
        if geo in lookup.cache:
            return lookup.cache[geo]
    except AttributeError:
        lookup.cache = {}

    # Replace special characters
    escaped = urllib.parse.quote(geo, safe="")
    eprint(escaped)
    # Get feed from Google
    feed = feedparser.parse(
        f"https://news.google.com/news/rss/local/section/geo/{escaped}")
    eprint(feed)

    # If no items in feed, get feed from Onion
    if not feed["items"]:
        feed = feedparser.parse("http://www.theonion.com/feeds/rss")

    # Cache results
    lookup.cache[geo] = [{
        "link": item["link"],
        "title": item["title"]
    } for item in feed["items"]]

    # Return results
    return lookup.cache[geo]
예제 #3
0
def businesses():
    """Get business details for location"""

    # Ensure parameters are present
    location = request.args.get("location")
    if not location:
        raise RuntimeError("missing location parameter")

    # location = str(location)
    location = json.loads(location)
    location = str(location)
    eprint(location)

    # if business details are in database, no need to look up on yelp
    business_details = db.execute(
        "SELECT * FROM businesses WHERE location = :location",
        location=location)
    if len(business_details) >= 1:
        eprint("we have details for location")

        # success
        return jsonify(business_details)

    else:

        # we need location businesses
        raise RuntimeError("missing location businesses")
예제 #4
0
def articles():
    """Look up articles for geo"""

    query = request.args.get("geo")
    eprint(query)
    news = lookup(query)
    eprint(news)
    return jsonify(news)
예제 #5
0
def articles():
    """Look up articles for geo"""
    geo = request.args.get("geo")
    if not geo:
        raise RuntimeError("missing geo")

    article = lookup(geo)
    eprint(geo)
    if len(article) > 10:
        article = article[:10]
    return jsonify(article)
예제 #6
0
def search():
    """Search for places that match query"""

    query = request.args.get("q")
    eprint(f"%{query}%")
    matches = db.execute("""
        SELECT * FROM places WHERE postal_code LIKE :postal_code OR place_name LIKE :place_name OR admin_name1 LIKE :admin_name1""",
                         postal_code='%' + query + '%',
                         place_name='%' + query + '%',
                         admin_name1='%' + query + '%')
    eprint(matches)
    return jsonify(matches)
예제 #7
0
def lookup_details(str_location, location_businesses):
    """Look up business details for location businesses"""

    str_location = str_location

    eprint("Looking up business details for location")

    # for id in location_businesses, look up business details from database
    for business in location_businesses:

        # look up business details on yelp
        response = yelp_api.business_query(id=business["id"])

        # check that the business details aren't already in the database
        details = db.execute("SELECT * FROM businesses WHERE id = :id",
                             id=response["id"])

        # if not already in database, add to database
        if len(details) == 0:
            db.execute(
                '''INSERT INTO businesses (id, name, url, price, rating, review_count, phone, photos, categories, coordinates, location)
                                              VALUES (:id, :name, :url, :price, :rating, :review_count, :phone, :photos, :categories, :coordinates, :location)''',
                id=response["id"],
                name=response["name"],
                url=response["url"],
                price=response["price"],
                rating=response["rating"],
                review_count=response["review_count"],
                phone=response["display_phone"],
                photos=str(response["photos"]),
                categories=str(response["categories"]),
                coordinates=str(response["coordinates"]),
                location=str_location)

    # query database for  business details
    business_details = db.execute(
        "SELECT * FROM businesses WHERE location = :location",
        location=str_location)

    # return results
    return business_details
예제 #8
0
def index():
    query = request.args.get("q")
    eprint(query)
    rows = db.execute("SELECT * FROM Album WHERE Title = :q", q=query)
    return render_template("index.html", albums=rows)
예제 #9
0
def location():
    """Get current location of user and store businesses for that location"""

    location = request.get_json()
    location = {
        "latitude": location["latitude"],
        "longitude": location["longitude"]
    }

    # convert location dictionary to string
    str_location = str(location)
    eprint("str_location = " + str_location)

    # query database for location
    location_businesses = db.execute(
        "SELECT * FROM location_businesses WHERE location = :location",
        location=str_location)

    # if location is in database, no need to look up businesses in this location on yelp
    if len(location_businesses) == 1:

        # location must already be in database
        location_businesses = location_businesses[0]["businesses"]
        location_businesses = ast.literal_eval(location_businesses)

        # if business details are in database, no need to look up on yelp
        business_details = db.execute(
            "SELECT * FROM businesses WHERE location = :location",
            location=str_location)
        if len(business_details) >= 1:
            eprint("we have details for location")

            # The server successfully processed the request and is not returning any content
            return ("", 204)

        else:

            eprint("LOOKING UP ON YELP????")

            # lookup location details and return results to the front end
            business_details = lookup_details(str_location,
                                              location_businesses)

            # The server successfully processed the request and is not returning any content
            return ("", 204)

    # look up businesses for this location
    else:

        # call yelp api for businesses in this location
        response = yelp_api.search_query(categories='restaurants',
                                         latitude=location["latitude"],
                                         longitude=location["longitude"],
                                         radius=16000,
                                         sort_by='rating')
        location_businesses = response["businesses"]
        str_location_businesses = str(location_businesses)

        # add businesses for this location to the database
        db.execute(
            "INSERT INTO location_businesses (location, businesses) VALUES (:location, :businesses)",
            location=str_location,
            businesses=str_location_businesses)

        # query database for location
        location_businesses = db.execute(
            "SELECT * FROM location_businesses WHERE location = :location",
            location=str_location)

        # if location is in database, no need to look up businesses in this location on yelp
        if len(location_businesses) == 1:

            # location must already be in database
            location_businesses = location_businesses[0]["businesses"]
            location_businesses = ast.literal_eval(location_businesses)

            # if business details are in database, no need to look up on yelp
            business_details = db.execute(
                "SELECT * FROM businesses WHERE location = :location",
                location=str_location)
            if len(business_details) >= 1:
                eprint("we have details for location")

                # The server successfully processed the request and is not returning any content
                return ("", 204)

            else:

                # lookup location details and return results to the front end
                business_details = lookup_details(str_location,
                                                  location_businesses)

                # The server successfully processed the request and is not returning any content
                return ("", 204)