Ejemplo n.º 1
0
    def get(self):
        session = get_current_session()
        authorized_tokens = session.get('authorized_tokens', None)
        if authorized_tokens is None:
            self.redirect('/connect')

        twitter = Twython(
            twitter_token = CONSUMER_KEY,
            twitter_secret = CONSUMER_SECRET,
            oauth_token = authorized_tokens['oauth_token'],
            oauth_token_secret = authorized_tokens['oauth_token_secret']
        )

        twitter_id = authorized_tokens['user_id']
        username = authorized_tokens['screen_name']

        statistic = UserStatistic.get_by_key_name(twitter_id)
        if statistic is None:
            statistic = UserStatistic(
                                      key_name=twitter_id,
                                      twitter_id=long(twitter_id),
                                     )
            statistic.put()

        if statistic.statistics == None \
            or (statistic.updated + timedelta(hours=1)) < datetime.now():

            try:
                task = Task(
                            url = '/fetch',
                            params = {'twitter_id': twitter_id}
                           )

                queue = Queue(name='fetch-tweets')
                queue.add(task)

                statistic.updated = datetime.now()
                statistic.put()

                template_values = {
                    'username': username,
                    'updated': True,
                    'error': False,
                }

            except:
                # need to send to error page if error is happened
                logging.exception('something bad happened')

                template_values = {
                    'username': username,
                    'updated': False,
                    'error': True,
                }

        else:
            template_values = {
                'username': username,
                'updated': False,
                'error': False,
            }

        path = os.path.join(TEMPLATE, 'message.html')
        self.response.out.write(template.render(path, template_values))