def post(self): # New user has joined in. Follow him and post a welcome message try: sms_client = OAuthClient('twitter', self) sms_client.token = OAuthAccessToken.all().filter( 'specifier =', 'smstweetin').filter( 'service =', 'twitter').fetch(1)[0] user = self.request.get('screen_name') count = int(self.request.get('count')) info = sms_client.post('/friendships/create', 'POST', (200,401,403), screen_name=user) # TODO : this may fail, try three times # Stop sending the follow status #status = "@%s has started using SMSTweet. Welcome %s to the group and tell about us to your friends" % (user, user) #info = sms_client.post('/statuses/update', 'POST', (200,401), status=status) # TODO : this may fail, try three times sms_client.token = OAuthAccessToken.all().filter( 'specifier =', user).filter( 'service =', 'twitter').fetch(1)[0] info = sms_client.post('/friendships/create', 'POST', (200,401,403), screen_name='smstweetin') # TODO : this may fail, try three times except (urlfetch.DownloadError, ValueError, Timeout), e: logging.warning("SmsTweetin:Friendship/create failed (%d) %s" % (count,e)) if count > 10: logging.error("SmsTweetin:Friendship/create Finally giving up") else: # Try again taskqueue.add(url = '/tasks/follow_new_user', params = { 'screen_name' : user, 'count' : count + 1 })
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 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 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)
def get(self): #from google.appengine.api import memcache import feedparser import google_url_shortner_api logging.debug('Starting feed taskqueue.') session_id = self.request.get('session_id') data = memcache.get('feed_%s' % session_id) if data is not None: logging.debug('Memcache receive success.') logging.debug(data) user_prefs_query = UserPrefs.all() user_prefs_query.filter('callback_key =', data['callback_key']) user_prefs = user_prefs_query.get() if user_prefs is not None: logging.info('Google Account: %s' % user_prefs.google_id.nickname()) feed = feedparser.parse(data['feed']) logging.debug(feed) entry_count = 0 for entry in feed['entries']: if entry_count > 3: logging.info('Entry count over capacity. count: %s' % len(feed['entries'])) break; tweet = entry['summary'] logging.info('Raw Tweet: %s' % tweet) bitly_link = '' logging.debug(entry) logging.debug(entry['link']) checked_link = [] for link in entry['links']: if link['type'] == 'image/jpeg': if not link['href'] in checked_link: bitly_link = bitly_link + ' ' + google_url_shortner_api.short(link['href']) checked_link.append(link['href']) if entry.get('media_player'): if not entry['media_player']['url'] in checked_link: bitly_link = bitly_link + ' ' + google_url_shortner_api.short(entry['media_player']['url']) checked_link.append(entry['media_player']['url']) #tweet = tweet.encode('utf-8') tweet = tweet.replace('\r\n', '\n') tweet = tweet.replace('\r', '\n') tweet = tweet.replace('\n', '') #tweet = unicode(tweet, 'utf-8') short_link_length = 21 twitter_max_length = 140 link_length = short_link_length * len(checked_link) tweet_length = len(tweet) logging.debug('Link length: %s' % link_length) logging.debug('Tweet length: %s' % tweet_length) if (tweet_length + link_length) > twitter_max_length: limit_tweet_length = (tweet_length - twitter_max_length) + link_length limit_tweet_length = tweet_length - limit_tweet_length - 3 tweet = tweet[0:limit_tweet_length] tweet = tweet + '...' + bitly_link logging.info('Limited Tweet: %s' % tweet) logging.info('Limited Tweet Length: %s' % len(tweet)) else: tweet = tweet + bitly_link tweet = tweet.encode('utf-8') logging.info('Tweet: %s' % tweet) 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 client.post('/statuses/update', status=tweet) except Exception, error: logging.error('Tweet Failed: %s' % error) entry_count = entry_count + 1 else: logging.error('Callback_key not found.')
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)