Exemple #1
0
class Yelp_API(object):
    def __init__(self, data):
        #authorization
        with io.open('config_secret.json') as cred:
            creds = json.load(cred)
            auth = Oauth1Authenticator(**creds)
            self.client = Client(auth)
            self.data = data
        self.food_per_business = data['food_per_business']

    def call_API(self):
        #  return self.client.search('SF', self.data)
        #  return SearchResponse (
                #  self.client._make_request(SEARCH_PATH, self.data)
        #  )
        response = SearchResponse(
                self.client._make_request(SEARCH_PATH, self.data)
                )

        list_to_be_returned = []
        #  for bus in response.businesses:
            #  list_to_be_returned += Crawler.limit("http://www.yelp.com/biz_photos/" + bus.id + "?tab=food&start=0", self.food_per_business)
        dict_of_urls = {}
        for bus in response.businesses:
            #  pprint(vars(bus))
            #  pprint(bus.categories[0].name)
            #  pprint(vars(bus.location.coordinate))
            url = "http://www.yelp.com/biz_photos/"+bus.id+"?tab=food&start=0"
            #  list_of_urls.append({url: [bus.location, bus.name]})
            #  list_of_urls.append({url: 
            category_list = []
            for category in bus.categories:
                category_list.append(category.name)

            dict_of_urls= dict(address=bus.location.address,
                    restaurant_name=bus.name,
                    restaurantId = bus.id,
                    city=bus.location.city,
                    state=bus.location.state_code,
                    postal_code=bus.location.postal_code,
                    display_address=bus.location.display_address,
                    latitude=bus.location.coordinate.latitude,
                    longitude=bus.location.coordinate.longitude,
                    category=category_list
                    )

            list_to_be_returned.append(dict_of_urls)
            #  print dict_of_urls

            #  pprint(list_of_urls)
            #  print (list_of_urls)
        #  Crawler.limit(list_of_urls, 1)
        # return Crawler(dict_of_urls).limit(self.food_per_business)
            #  return dict_of_urls

        return list_to_be_returned
class Yelp_API(object):
    def __init__(self, data):
        #authorization
        with io.open('config_secret2.json') as cred:
            creds = json.load(cred)
            auth = Oauth1Authenticator(**creds)
            self.client = Client(auth)
            self.data = data

        # own parameters being passed in
        self.food_per_business = data['food_per_business']

    def call_API(self):
        response = SearchResponse(
            self.client._make_request(SEARCH_PATH, self.data))

        dict_of_urls = {}
        for bus in response.businesses:
            #  url = "http://www.yelp.com/biz_photos/"+bus.id+"?tab=food&start=0"
            category_list = []
            if bus.categories:
                for category in bus.categories:
                    category_list.append(category.name)

            dict_of_urls[bus.id] = dict(
                address=bus.location.address,
                city=bus.location.city,
                state=bus.location.state_code,
                postal_code=bus.location.postal_code,
                display_address=bus.location.display_address,
                restaurant_name=bus.name,
                restaurantId=bus.id,
                latitude=bus.location.coordinate.latitude,
                longitude=bus.location.coordinate.longitude,
                category=category_list)

        print(vars(response))
        if response.total == 0:
            raise RuntimeError("Yelp returns no businesses")

        if 'query_method' in self.data and self.data['query_method'] == 1:
            print("DB")
            food_list = DB(dict_of_urls).query(self.food_per_business)
        else:
            print("Yelp")
            food_list = Crawler(dict_of_urls).query(self.food_per_business)

        random.shuffle(food_list)
        return food_list