Skip to content

Instantly share code, notes, and snippets.

@alfondotnet
Created September 24, 2016 00:04
Show Gist options
  • Save alfondotnet/5fddd7ae68e766781f5e0c65c8383a2f to your computer and use it in GitHub Desktop.
Save alfondotnet/5fddd7ae68e766781f5e0c65c8383a2f to your computer and use it in GitHub Desktop.
Discover Yelp Businesses
# -*- encoding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from recommendations.models import *
import httplib, json, urllib2
from optparse import make_option
import yelp_api
# YOUR API DETAILS
consumer_key = 'XXXXX'
consumer_secret = 'XXXXX'
token = 'XXXXX'
token_secret = 'XXXXX'
'''
######################
Discover Reviews
This is a command that goes through each item and get the reviews done by users
######################
'''
class Command(BaseCommand):
# List of options of our discover command
option_list = BaseCommand.option_list + (
make_option('--start_id',
type='long',
help='App to start',
metavar='number'),
make_option('--number',
type='long',
help='Number of apps to get',
metavar='number'),
)
def handle(self, *args, **options):
for offset in xrange(700,800,20):
# Make then a for with offet and limit.
url_params = {}
# One of these three required
#url_params['ll'] = point
url_params['location'] = 'Sevilla'
#url_params['bounds'] = bounds
# Optional
# url_params['term'] = term
url_params['offset'] = offset
url_params['limit'] = 20
url_params['cc'] = 'ES'
# url_params['lang'] = lang
# url_params['cll'] = current_location
# url_params['radius_filter'] = radius
url_params['category_filter'] = 'restaurants'
# url_params['deals_filter'] = deals
# url_params['sort'] = sort
response = yelp_api.yelp_request('search',url_params, consumer_key, consumer_secret, token, token_secret)
json_string = json.dumps(response, sort_keys=True, indent=2)
#json_string = open('recommendations/management/commands/yelp.json','r')
json_results = json.loads(json_string)
for restaurant in json_results['businesses']:
avg_rating = restaurant['rating']
item_id = restaurant['id']
name = restaurant['name']
item = Item(item_id=item_id, name=name, avg_rating=avg_rating)
item.save()
if 'image_url' in restaurant:
restaurant_image_url = restaurant['image_url']
else:
restaurant_image_url = 'nopic'
display_address = ""
dis = restaurant['location']['display_address']
for a in dis:
display_address += a + ", "
try:
postal_code = restaurant['location']['postal_code']
except:
postal_code = ""
# Now we have to get the geolocation
google_url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address="+ urllib2.quote(display_address[:-2].replace(" ", "+").encode('utf8'))
print google_url
try:
google_api = urllib2.urlopen(google_url)
google_json = json.loads(google_api.read())
google_location = google_json['results'][0]['geometry']['location']
latitude,longitude = google_location['lat'],google_location['lng']
except:
# In the case of not finding the location
# NaN is set for further inspection
latitude,longitude = 'NaN','NaN'
print latitude,longitude
# DOWN HERE, INSTEAD OF SAVING THE INFO ON A DJANGO MODEL, SAVE IT TO A FILE OR SOMETHING
yelp_bus = YelpBusiness(business_id=item, image_url=restaurant_image_url, rating_url=restaurant['rating_img_url_large'], item_location=display_address[:-2], item_postal_code=postal_code, item_coords=str(latitude)+","+str(longitude))
yelp_bus.save()
google_api.close()
print ("saved one.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment