Exemplo n.º 1
0
    def get(self):
        keyword_id = self.request.get('keyword_id')

        search_keywords = SearchKeywords.get_by_id(int(keyword_id))
        if search_keywords is not None:
            logging.info('Feed: %s' % search_keywords.keyword)

            user_prefs = UserPrefs().get_by_id(
                search_keywords.user_id.key().id())
            if user_prefs is not None:
                logging.info('Keyword owner name: %s' %
                             user_prefs.google_id.nickname())
                if user_prefs.oauth_access_token_key is not None:
                    oauth_access_token = OAuthAccessToken.get_by_key_name(
                        user_prefs.oauth_access_token_key.key().name())
                    if oauth_access_token is not None:
                        logging.info(
                            'Twitter Account: %s' %
                            user_prefs.oauth_access_token_key.specifier)
                        try:
                            client = OAuthClient('twitter', self)
                            client.token = oauth_access_token
                            results = client.get(
                                '/search',
                                q=search_keywords.keyword.encode('utf-8'))
                            for tweet_id in results['statuses']:
                                try:
                                    logging.info(
                                        'Add taskqueue. tweet_id: %s' %
                                        tweet_id)
                                    taskqueue.add(url='/feed/tweet',
                                                  method='GET',
                                                  params={
                                                      'tweet_id': tweet_id,
                                                      'keyword_id': keyword_id
                                                  })
                                except (taskqueue.Error,
                                        apiproxy_errors.Error):
                                    logging.exception(
                                        'Failed to add taskqueue.')

                        except Exception, error:
                            logging.error('Cache Failed: %s' % error)
    def callback(self, return_to='/config'):
        oauth_token = self.handler.request.get("oauth_token")
 
        if not oauth_token:
            return self.get_request_token()
 
        oauth_token = OAuthRequestToken.all().filter(
            'oauth_token =', oauth_token).filter(
            'service =', self.service).fetch(1)[0]
 
        token_info = self.get_data_from_signed_url(
            self.service_info['access_token_url'], oauth_token
            )
        
        """
        key_name = create_uuid()

        self.token = OAuthAccessToken(
            key_name=key_name, service=self.service,
            **dict(token.split('=') for token in token_info.split('&'))
            )
 
        if 'specifier_handler' in self.service_info:
            specifier = self.token.specifier = self.service_info['specifier_handler'](self)
            old = OAuthAccessToken.all().filter(
                'specifier =', specifier).filter(
                'service =', self.service)
            db.delete(old)
 
        self.token.put()
        """
        
        #logging.error("token_info="+token_info)
        token_info_list = dict(token.split('=') for token in token_info.split('&'))
        
        token_query = OAuthAccessToken.all()
        token_query.filter("oauth_token =", token_info_list["oauth_token"])
        token_query.filter("oauth_token_secret =", token_info_list["oauth_token_secret"])
        token_query.filter("service =", self.service)
       
        #logging.error("oauth_token="+token_info_list["oauth_token"])
        #logging.error("service="+self.service)
        
        #remember_key = create_key(token_info_list["screen_name"])
        
        if not token_query.count():
            key_name = create_uuid()
            self.token = OAuthAccessToken(key_name=key_name)
            #self.token.key_name = create_uuid()
            self.token.oauth_token = token_info_list["oauth_token"]
            self.token.oauth_token_secret = token_info_list["oauth_token_secret"]
            self.token.service = self.service
            self.token.specifier = token_info_list["screen_name"]
            #self.token.remember_key = remember_key
            self.token.put()
            
        else:
            self.token = token_query.get()
            if self.token.specifier != token_info_list["screen_name"]:
                self.token.specifier = token_info_list["screen_name"]
                
            self.token.updated_at = datetime.now()
            self.token.put()
            
        user = users.get_current_user()
        user_prefs_query = UserPrefs.all()
        user_prefs_query.filter("google_id =", user)
        #user_prefs_query.filter("oauth_access_token_key =", self.token.key())
        user_prefs = user_prefs_query.get()
        if user_prefs is None:
            user_prefs = UserPrefs()
            user_prefs.google_id = user
            user_prefs.oauth_access_token_key = self.token.key()
            user_prefs.put()
        else:
            #user_prefs.google_id = user
            user_prefs.oauth_access_token_key = self.token.key()
            user_prefs.put()
        
        #cookie_value = {}
        #cookie_value['key'] = remember_key
        #cookie_value['specifier'] = self.token.specifier
        
        #memcache.add("twitter_token_" + remember_key, self.token, 3600)
        
        #self.set_cookie(cookie_value)
        self.handler.redirect(return_to)
Exemplo n.º 3
0
    def get(self):
        tweet_id = self.request.get('tweet_id')
        keyword_id = self.request.get('keyword_id')

        search_keywords = SearchKeywords.get_by_id(int(keyword_id))
        if search_keywords is not None:
            logging.info('Feed: %s' % search_keywords.keyword)

            user_prefs = UserPrefs().get_by_id(
                search_keywords.user_id.key().id())
            if user_prefs is not None:
                logging.info('Keyword owner name: %s' %
                             user_prefs.google_id.nickname())
                if user_prefs.oauth_access_token_key is not None:
                    oauth_access_token = OAuthAccessToken.get_by_key_name(
                        user_prefs.oauth_access_token_key.key().name())
                    if oauth_access_token is not None:
                        logging.info(
                            'Twitter Account: %s' %
                            user_prefs.oauth_access_token_key.specifier)
                        try:
                            search_cache_query = SearchCache().all()
                            search_cache_query.filter('tweet_id =',
                                                      int(tweet_id))
                            search_cache_query.filter('keyword_key =',
                                                      search_keywords.key())
                            if search_cache_query.get() is None:
                                client = OAuthClient('twitter', self)
                                client.token = oauth_access_token
                                tweet = client.get('/statuses/show/%d' %
                                                   int(tweet_id))

                                logging.info(
                                    'Tweet: (%s) %s' %
                                    (tweet['user']['name'], tweet['text']))
                                logging.info(tweet['created_at'])

                                search_cache = SearchCache()
                                search_cache.tweet_id = int(tweet_id)
                                search_cache.keyword_key = search_keywords.key(
                                )
                                search_cache.name = tweet['user']['name']
                                search_cache.screen_name = tweet['user'][
                                    'screen_name']
                                search_cache.profile_image_url = tweet['user'][
                                    'profile_image_url']
                                search_cache.text = tweet['text']
                                search_cache.location = tweet['user'][
                                    'location']
                                search_cache.time_zone = tweet['user'][
                                    'time_zone']
                                search_cache.tweeted_at = datetime.datetime.strptime(
                                    tweet['created_at'],
                                    "%a %b %d %H:%M:%S +0000 %Y")
                                search_cache.put()
                            else:
                                logging.info('Skip. tweet_id: %d' %
                                             int(tweet_id))

                        except Exception, error:
                            logging.error('Cache Failed: %s' % error)