Пример #1
0
def finish_process(request, user, last_updated):
    user_entity = User.get_by_id(user, namespace=DS_VERSION)
    bu_entity = BusyUser.get_by_id(user, namespace=DS_VERSION)

    # possible as same tasks may be executed more than once, see
    # https://developers.google.com/appengine/docs/python/taskqueue/overview-push#Python_Using_push_queues
    if bu_entity is None:
        logging.warning("Processing %s who wasn't registered as BusyUser", user)
    else:
        bu_entity.worker_count -= 1
        if bu_entity.worker_count == 0:
            user_entity.last_updated = last_updated
            user_entity.put()

            # do the shoutin'
            if bu_entity.shout:
                msg = ('Your tag visualizations are ready at %s/history?user=%s'
                        ' and %s/tag_graph?tp=20&user=%s'
                        % (request.host_url, urllib.quote(user),
                            request.host_url, urllib.quote(user)))

                try:
                    shout_resp = lfm_api.user_shout(user, msg)
                    if 'status' in shout_resp and shout_resp['status'] == 'ok':
                        logging.info('Shouted to ' + user)
                    else:
                        logging.error('Error shouting to %s: %s',
                            user, shout_resp)
                except lastfm.InvalidSessionError:
                    logging.error('Could not shout. Last.fm session invalid.')

            bu_entity.key.delete()

        else:
            bu_entity.put()
Пример #2
0
def finish_process(request, user, last_updated):
    user_entity = User.get_by_id(user, namespace=DS_VERSION)
    bu_entity = BusyUser.get_by_id(user, namespace=DS_VERSION)

    # possible as same tasks may be executed more than once, see
    # https://developers.google.com/appengine/docs/python/taskqueue/overview-push#Python_Using_push_queues
    if bu_entity is None:
        logging.warning("Processing %s who wasn't registered as BusyUser",
                        user)
    else:
        bu_entity.worker_count -= 1
        if bu_entity.worker_count == 0:
            user_entity.last_updated = last_updated
            user_entity.put()

            # do the shoutin'
            if bu_entity.shout:
                msg = (
                    'Your tag visualizations are ready at %s/history?user=%s'
                    ' and %s/tag_graph?tp=20&user=%s' %
                    (request.host_url, urllib.quote(user), request.host_url,
                     urllib.quote(user)))

                try:
                    shout_resp = lfm_api.user_shout(user, msg)
                    if 'status' in shout_resp and shout_resp['status'] == 'ok':
                        logging.info('Shouted to ' + user)
                    else:
                        logging.error('Error shouting to %s: %s', user,
                                      shout_resp)
                except lastfm.InvalidSessionError:
                    logging.error('Could not shout. Last.fm session invalid.')

            bu_entity.key.delete()

        else:
            bu_entity.put()
Пример #3
0
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        self.response.headers['Cache-Control'] = \
            'no-transform,public,max-age=300,s-maxage=900'
        user = self.request.get('user')

        if not user:
            self.response.write(
                json.dumps({'error': 'No user specified.'}))
            return
        else:
            user = user.lower()

        gwi_json = {}

        try:
            gwi_json = lfm_api.user_getweekintervals(user)
        except lastfm.ExceedRateLimitError:
            gwi_json['error'] = 29
            gwi_json['message'] = 'Rate limit exceeded'
        except lastfm.SuspendedAPIKeyError:
            gwi_json['error'] = 26
            gwi_json['message'] = 'Suspended API key'
        except lastfm.ServiceOfflineError:
            gwi_json['error'] = 11
            gwi_json['message'] = 'Service temporarily offline. ' + \
                                        'Please try again later.'
        except lastfm.TemporaryError:
            gwi_json['error'] = 16
            gwi_json['message'] = 'There was a temporary error processing your ' + \
                                        'request. Please try again.'
        except DeadlineExceededError:
            self.response.write(json.dumps(
                {'error': 'Could not reach Last.fm. Please try again later.'}))
            return


        if 'error' in gwi_json:
            error_msg = ''
            if int(gwi_json['error']) == 6:
                error_msg = 'User does not exist'
            else:
                error_msg = 'Last.fm error: ' + gwi_json['message']

            self.response.write(json.dumps({'error': error_msg}))
            return

        weeks = [int(week['to']) for week in gwi_json['weeklychartlist']['chart']]

        try:
            user_entity = User.get_by_id(user, namespace=DS_VERSION)
        except apiproxy_errors.OverQuotaError as e:
            logging.critical(e)
            return {'error': 'AppEngine error. Go tell ' + \
                    '[email protected] to buy more Google resources.'}

        if (user_entity is not None and
                user_entity.last_updated >= weeks[-1]):
            self.response.write(json.dumps(
                self.build_response(user, self.request)))
        else:
            user_data = lfm_api.user_getinfo(user)['user']
            register_date = int(user_data['registered']['unixtime'])

            # ensure User entity exists before start building data
            # so each data fragment can use this entity as the parent
            if user_entity is None:
                user_entity = User(id=user, namespace=DS_VERSION)
                user_entity.put()

            bu_entity = BusyUser.get_by_id(user, namespace=DS_VERSION)
            if bu_entity is None:
                intervals = get_worker_intervals(user_entity,
                    register_date, weeks)

                if intervals:
                    bu_entity = BusyUser(id=user, shout=False,
                        worker_count=len(intervals), namespace=DS_VERSION)
                    bu_entity.put()

                for interval in intervals:
                    add_worker(user, interval[0], interval[1], append_to=interval[2])

            self.response.headers['Cache-Control'] = \
                'no-transform,public,max-age=30'

            resp_data = {'status': 1, 'text': 'Data still processing'}

            last_updated = self.get_last_updated(user)
            if last_updated is not None:
                resp_data['last_updated'] = last_updated

            if weeks[-1] <= register_date:
                resp_data = {'error': 'Your account is too new for Last.fm to have data.'}

            self.response.write(json.dumps(resp_data))
Пример #4
0
    def get(self):
        self.response.headers["Content-Type"] = "application/json"
        self.response.headers["Cache-Control"] = "no-transform,public,max-age=300,s-maxage=900"
        user = self.request.get("user")

        if not user:
            self.response.write(json.dumps({"error": "No user specified."}))
            return
        else:
            user = user.lower()

        gwi_json = {}

        try:
            gwi_json = lfm_api.user_getweekintervals(user)
        except lastfm.ExceedRateLimitError:
            gwi_json["error"] = 29
            gwi_json["message"] = "Rate limit exceeded"
        except lastfm.SuspendedAPIKeyError:
            gwi_json["error"] = 26
            gwi_json["message"] = "Suspended API key"
        except lastfm.ServiceOfflineError:
            gwi_json["error"] = 11
            gwi_json["message"] = "Service temporarily offline. " + "Please try again later."
        except lastfm.TemporaryError:
            gwi_json["error"] = 16
            gwi_json["message"] = "There was a temporary error processing your " + "request. Please try again."
        except DeadlineExceededError:
            self.response.write(json.dumps({"error": "Could not reach Last.fm. Please try again later."}))
            return

        if "error" in gwi_json:
            error_msg = ""
            if int(gwi_json["error"]) == 6:
                error_msg = "User does not exist"
            else:
                error_msg = "Last.fm error: " + gwi_json["message"]

            self.response.write(json.dumps({"error": error_msg}))
            return

        weeks = [int(week["to"]) for week in gwi_json["weeklychartlist"]["chart"]]

        try:
            user_entity = User.get_by_id(user, namespace=DS_VERSION)
        except apiproxy_errors.OverQuotaError as e:
            logging.critical(e)
            return {"error": "AppEngine error. Go tell " + "[email protected] to buy more Google resources."}

        if user_entity is not None and user_entity.last_updated >= weeks[-1]:
            self.response.write(json.dumps(self.build_response(user, self.request)))
        else:
            user_data = lfm_api.user_getinfo(user)["user"]
            register_date = int(user_data["registered"]["unixtime"])

            # ensure User entity exists before start building data
            # so each data fragment can use this entity as the parent
            if user_entity is None:
                user_entity = User(id=user, namespace=DS_VERSION)
                user_entity.put()

            bu_entity = BusyUser.get_by_id(user, namespace=DS_VERSION)
            if bu_entity is None:
                intervals = get_worker_intervals(user_entity, register_date, weeks)

                if intervals:
                    bu_entity = BusyUser(id=user, shout=False, worker_count=len(intervals), namespace=DS_VERSION)
                    bu_entity.put()

                for interval in intervals:
                    add_worker(user, interval[0], interval[1], append_to=interval[2])

            self.response.headers["Cache-Control"] = "no-transform,public,max-age=30"

            resp_data = {"status": 1, "text": "Data still processing"}

            last_updated = self.get_last_updated(user)
            if last_updated is not None:
                resp_data["last_updated"] = last_updated

            if weeks[-1] <= register_date:
                resp_data = {"error": "Your account is too new for Last.fm to have data."}

            self.response.write(json.dumps(resp_data))