Exemplo n.º 1
0
 def get(self):
     """Uses twitter api to search and respond for certain tweets
     Sample response:
     {u'favorited': False,
     u'in_reply_to_user_id': None,
     u'contributors': None,
     u'truncated': False,
     u'text': u'Achoo - CRON ! Excuse me.',
     u'created_at': u'Thu Jun 02 13:33:47 +0000 2011',
     u'retweeted': False,
     u'in_reply_to_status_id': None,
     u'coordinates': None,
     u'id': 76280352674549760,
     u'source': u'',
     u'in_reply_to_status_id_str': None, 
     u'in_reply_to_screen_name': None, 
     u'place': None, 
     u'retweet_count': 0, 
     u'geo': None, 
     u'in_reply_to_user_id_str': None, 
     u'id_str': u'76280352674549760'}
     """
     client = OAuthClient('twitter', self)
     # get the latest tweets from cronbot
     username = '******'
     # TODO: get last tweet
     extra_data = {'screen_name': username,
                   #'since_id': '76280352674549760'
     }
     tweet_list = client.get('/statuses/user_timeline', **extra_data)
     final_tweets = []
     try:
         tweet = tweet_list[0]
         final_tweets.append(tweet)
     except IndexError:
         tweet = None
     # mention data
     mention_data = {'count': 1}
     mention_list = client.get('/statuses/mentions', **mention_data)
     try:
         mention = mention_list[0]
         final_tweets.append(mention)
     except IndexError:
         mention = None
     for tweet in final_tweets:
         text = generate_tweet(tweet.get('text'), tweet['user']['screen_name'])
         tweet_id = tweet.get('id_str')
         # We havent answered to this one
         if not Tweet.get_by_key_name(tweet_id):
             # Store at the first try
             data = {'tweet_id': tweet_id,
                     'tweet': text,}
             stored_tweet = Tweet(key_name=tweet_id, **data)
             stored_tweet.put()
             do_tweet(self, text)
     self.response.out.write('ok')
Exemplo n.º 2
0
    def get(self):

        if os.environ["HTTP_HOST"].startswith("smstweet.appspot.com"):
            self.redirect("http://www.smstweet.in", permanent=True)
            return

        phoneno = self.request.get("phoneno")
        user_name = None
        message = None
        server_error = False
        client = OAuthClient("twitter", self)

        client_cookie = client.get_cookie()

        if client_cookie:
            try:
                info = client.get(
                    "/account/verify_credentials", expected_status=(200, 401)
                )  # TODO : this may fail, try three times
                if "error" in info:
                    client.expire_cookie()
                else:
                    user_name = info["screen_name"]
            except (urlfetch.DownloadError, ValueError), e:
                server_error = True
                logging.warning("Home:Credentials could not be fetched. %s " % e)
            except Timeout, e:
                server_error = True
                logging.warning("Timedout(Home) : Never mind")
Exemplo n.º 3
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)
Exemplo n.º 4
0
  def fetch_status(self,type,key_name, url):
    phone = self.request.get('phone')    
    tuser = TwitterUser.get_by_phonenumber(phone)
    if tuser == None:
      logging.warning("Could not fetch tuser based on phone number %s",phone)
      return

    since_id = memcache.get("%s%s" % (key_name,tuser.user))
    if not since_id:
      since_id = -1
      memcache.add("%s%s" % (key_name,tuser.user), since_id)

    client = OAuthClient('twitter', self)
    try:
      info = client.get(url, 'GET', (200,401,403), tuser, since_id=since_id, count = 10)
      if 'error' in info:
        logging.warning("%s Fetch failed for %s because of %s" % (type,tuser.user, info['error']))
      elif len(info) > 0:
        logging.debug("fetched %d %s's for %s" % (len(info), type,tuser.user))
        memcache.replace("%s%s" % (key_name,tuser.user), info[0]['id'])
        if type == 'DM':
          for dm in info: TweetDM.create(dm['sender_screen_name'],tuser.user, dm['text'], dm['created_at'], dm['id'])
        else:
          for dm in info: TweetMention.create(dm['user']['screen_name'],tuser.user, dm['text'], dm['created_at'], dm['id'])
        #endif
      #endif
    except (urlfetch.DownloadError, ValueError), e:
      logging.warning("%s: could not be fetched. %s " % (type,e))
Exemplo n.º 5
0
  def updateStatuswithToken(self, tuser, status) :

    client = OAuthClient('twitter', self)

    if len(status) == 0:
      self.response.out.write('Dude, where is the status message to post? Pressed the send button to fast?')
      return

    taskqueue.add(url = '/tasks/post_message', params = { 'phone' : tuser.phonenumber, 'count' : 1, 'status' : status[:140] })

    lastError = tuser.get_last_error()
    if lastError:
      msg = lastError
    elif tuser.tweetCount == 0:
      msg = "Welcome to SMSTweet and Congrats on posting your first message. You can sms TWUP to get latest from your timeline. Details at http://smstweet.in/help"
    elif tuser.reminder and tuser.reminder == 1:
      # Get the mentions of the user
      try:
        info = client.get('/statuses/mentions', tuser = tuser,count=1)
        msg = "could not get any message with your mention"
        if info and len(info) > 0:
          if 'text' in info[0]:
            msg = "%s: %s" % (info[0]['user']['screen_name'], info[0]['text'])
      except (urlfetch.DownloadError, ValueError), e:
        logging.warning("Update:mentions could not be fetched. %s " % e)
        msg = "Twitter is having it's fail whale moment, but I guess I managed to post your status."
Exemplo n.º 6
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']:
                             search_cache_query = SearchCache().all()
                             search_cache_query.filter('tweet_id =', tweet_id)
                             search_cache_query.filter('keyword_key =', search_keywords.key())
                             if search_cache_query.get() is None:
                                 tweet = client.get('/statuses/show/%d' % tweet_id)
                                 logging.info('Tweet: (%s) %s' % (tweet['user']['name'], tweet['text']))
                                 logging.info(tweet['created_at'])
                                 search_cache = SearchCache()
                                 search_cache.tweet_id = 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' % tweet_id)
                             
                     except Exception, error:
                         logging.error('Cache Failed: %s' % error)
Exemplo n.º 7
0
  def get_twitter_profile(cls,tuser):
    client = OAuthClient('twitter', cls)
    info = client.get('/account/verify_credentials', 'GET', (200,401), tuser)
    tp = None
    if 'id' in info:
      tp = TwitterProfile.save_twitter_profile(info,client.token)
    elif 'error' in info:
      print "profile %s gave error %s. Deleting the record" % (tuser.user, info['error'])
      tuser.delete()

    return tp
Exemplo n.º 8
0
def get_current_user(obj):
    url = obj.request.url
    if ('appspot' in url) or (APP_NAME in url):
        from twitter_oauth_handler import OAuthClient,OAuthHandler
        client = OAuthClient('twitter', obj)
        if not client.get_cookie():
            user = None
        else:
            info = client.get('/account/verify_credentials')
            if info:
                user = info['screen_name']
            else:
                user = None
    else:
        from google.appengine.api import users as google_users
        user = google_users.get_current_user()
    return user
Exemplo n.º 9
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)
Exemplo n.º 10
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)