Example #1
0
 def __init__(self):
     self._log = logging.getLogger('ldext.google')
     #TODO: Load from config
     self._ext_rating_typcd = "GOOGL"
     self._apikey = "yourapikey"
     self._rvdb = RateVailDb()
     self._gapi = GooglePlacesAPI(self._apikey)
Example #2
0
class GoogleExtRatingsJob(object):

    def __init__(self):
        self._log = logging.getLogger('ldext.google')
        #TODO: Load from config
        self._ext_rating_typcd = "GOOGL"
        self._apikey = "yourapikey"
        self._rvdb = RateVailDb()
        self._gapi = GooglePlacesAPI(self._apikey)

    def purge_comments(self):
        self._log.info("Purging comments")
        cnt = self._rvdb.delete_external_rating_comments_by_type(self._ext_rating_typcd)
        self._log.debug("Purged %s records" % cnt)

    def load_comments(self):
        self._log.info("Loading comments")

        self._log.info("Retrieving external source ids from rvdb")
        ext_src_ids = self._rvdb.get_external_ratings_by_type(self._ext_rating_typcd)
        self._log.info("Retrieved %s external source idse" % len(ext_src_ids))

        for ext_rec in ext_src_ids:
            place_dtl = self._gapi.fetch_place_detail(ext_rec['externalsourceid'])
            if place_dtl['status'] == 'OK':
                #TODO: Need to update externalrating
                if 'reviews' in place_dtl['result']:
                    reviews = place_dtl['result']['reviews']
                    for review in reviews:
                        rating = review['rating']
                        comment = review['text']
                        username = review['author_name']
                        self._log.info("Inserting externalratingid: %s, rating: %s, author_name: %s" %
                                       (ext_rec['externalratingid'], rating, username))
                        if self._rvdb.insert_external_rating_comment(ext_rec['externalratingid'], rating,
                                                                     comment, username) == 1:
                            self._log.info("  -Insert complete")
                        else:
                            self._log.critical("  -Insert failed for externalratingid: %s" %
                                               ext_rec['externalratingid'])
                else:
                    self._log.info("No reviews found for extratingid: %s" % ext_rec['externalratingid'])
            else:
                self._log.critical("Invalid request received")
        return 0
Example #3
0
class SearchAndLoadGoogleVenues(object):


    def __init__(self, query, google_venue_types):
        self.google_venue_types = google_venue_types
        self._query = query
        self._apikey = "YourKey"
        self._gapi = GooglePlacesAPI(self._apikey)
        self._db = RateVailDb()

    def run(self):
        print "Searching query: " + str(self._query)

        search_page = self._gapi.search_places(self._query)
        while search_page:
            if search_page['status'] != "OK":
                raise Exception("Search Page response returned status: %s" % search_page['status'])

            self._proc_results(search_page['results'])

            next_token = search_page.get("next_page_token")
            if next_token:
                print "Processing next search page"
                search_page = self._gapi.search_places(self._query, pagetoken=next_token)
            else:
                print "Finished processing all pages"
                break


    def _proc_results(self, results):
        print "Processing Result"
        for result in results:
            detail_page = self._gapi.fetch_place_detail(result['reference'])
            if detail_page['status'] != "OK":
                raise Exception("Detail Page returned bad status")

            #Venue Properties
            venue_name = escape_str(result['name'])
            venue_shortname = super_sanitize_str(result['name'])
            venue_shortname = venue_shortname.replace(" ", "").lower()
            venue_address = result['formatted_address']
            venue_phone = detail_page['result'].get('formatted_phone_number')
            g_venue_types = result['types']
            venue_externalsourceid = result['reference']
            externalrating_uniqid = result['id']
            venue_description = "Description for this venue is coming soon"
            #Rating Val
            if "rating" in result.keys():
                venue_rating = result['rating']
            else:
                venue_rating = 0
            #Price point
            if "price_level" in result.keys():
                venue_pricepoint = result['price_level']
            else:
                venue_pricepoint = 0
            #Convert google venue types to rv venue types
            rv_venue_types = []
            for typ in g_venue_types:
                if typ in self.google_venue_types:
                    rv_venue_types.append(self.google_venue_types[typ])

            rv_venue_types = set(rv_venue_types) #remove duplicates
            if len(rv_venue_types) == 0:
                print "WARNING: This venue doesn't ahve any matching venue types and will be skipped"
                print "WARNING: Skipping venue: " + str(venue_name)
                continue

            #
            # Check if externalsourceid already exists. If it does then just assign types. Have to insert ignore though
            #
            ext_src_rec = self._db.externalrating_uniqid_exists(externalrating_uniqid)
            if len(ext_src_rec) == 1:
                print "External Sourceid exists"
                print "Assigning types"
                for typ in rv_venue_types:
                    self._db.assign_ignore_venue_venuetyp(ext_src_rec[0]['venueid'], typ)
                print "Continuing to next record"
                continue
            elif len(ext_src_rec) > 1:
                raise Exception("Externa Source ID is linked to more than one venue. %s" % str(ext_src_rec))
            else:
                print "External Source id does NOT exist. Creating venue"
                

            #Create Phone Number
            if venue_phone:
                phone_parts = venue_phone.split(" ")
                area_code = phone_parts[0][1:4]
                phone_parts = phone_parts[1].split("-")
                exchange = phone_parts[0]
                subscriberno = phone_parts[1]
                self._db.insert_phone("pri", area_code, exchange, subscriberno, commit=False)
                phoneid = self._db._last_insert_id()
            else:
                phoneid = None
            #Create Address
            # TODO: Find zip properly
            zipcd = "81657"

            addr_parts = venue_address.split(",")
            if len(addr_parts) == 3:
                self._db.insert_addr("PHYS", "", addrline2=None, city=addr_parts[0],
                                     state=addr_parts[1], zipcd=zipcd, commit=False)
            if len(addr_parts) == 4:
                self._db.insert_addr("PHYS", addr_parts[0], addrline2=None, city=addr_parts[1],
                                     state=addr_parts[2], zipcd=zipcd, commit=False)
            elif len(addr_parts) == 5:
                self._db.insert_addr("PHYS", addr_parts[0], addrline2=addr_parts[1], city=addr_parts[2],
                                     state=addr_parts[3], zipcd=zipcd, commit=False)
            addrid = self._db._last_insert_id()

            #Create venue
            resortid = 1
            venueid = self._db.create_venue(resortid, venue_name, venue_shortname, venue_description,
                                            rv_venue_types, pricepointval=venue_pricepoint, phoneid=phoneid,
                                            addrids=[addrid], logoimageid=1)

            #Create External Rating
            self._db.insert_external_rating("GOOGL", venue_externalsourceid, venue_name,
                                            pricepointval=venue_pricepoint,
                                            ratingval=venue_rating,
                                            uniqid=externalrating_uniqid,
                                            commit=False)

            externalratingid = self._db._last_insert_id()
            self._db.assign_venue_external_rating(externalratingid, venueid, commit=True)
Example #4
0
 def __init__(self, query, google_venue_types):
     self.google_venue_types = google_venue_types
     self._query = query
     self._apikey = "YourKey"
     self._gapi = GooglePlacesAPI(self._apikey)
     self._db = RateVailDb()