コード例 #1
0
ファイル: tasks.py プロジェクト: mayanks/smstweet
  def post(self):
    status = self.request.get('status')
    phone = self.request.get('phone')
    count = int(self.request.get('count'))

    tuser = TwitterUser.get_by_phonenumber(phone)
    if tuser == None:
      logging.warning("Could not fetch tuser based on phone number %s",phone)
      return

    client = OAuthClient('twitter', self)
    try:
      info = client.post('/statuses/update', 'POST', (200,401,403), tuser, status=status)
      if 'error' in info:
        logging.warning("Submiting failed as credentials were incorrect (user:%s) %s", tuser.user, info['error'])
        tuser.lastError = "Twitter returned '%s' for your last update. You may be over limit or may have to register with SMSTweet again" % info['error']
        tuser.put()
      else:
        logging.debug("updated the status for user %s", tuser.user)
        Tweet.save_tweet(info)

    except (urlfetch.DownloadError, ValueError), e:
      logging.warning("Update:update (%d) could not be fetched. %s " % (count,e))
      if count > 10:
        logging.error("Tried updating the message 10 times. Finally giving up.")
      else:
        # Try again
        taskqueue.add(url = '/tasks/post_message', params = { 'phone' : phone, 'count' : count + 1, 'status' : status })
コード例 #2
0
ファイル: main.py プロジェクト: mayanks/smstweet
    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")
コード例 #3
0
ファイル: tasks.py プロジェクト: mayanks/smstweet
  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 })
コード例 #4
0
ファイル: tasks.py プロジェクト: mayanks/smstweet
  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))
コード例 #5
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)
コード例 #6
0
ファイル: smscommands.py プロジェクト: mayanks/smstweet
  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."
コード例 #7
0
ファイル: handler.py プロジェクト: alfredo/evil_cronbot
def do_tweet(request, text, extra_data=None):
    """Performs the tweet"""
    client = OAuthClient('twitter', request)
    data = {'status': text}
    if extra_data:
        data.update(extra_data)
    status = client.post('/statuses/update', **data)
    return status
コード例 #8
0
ファイル: handler.py プロジェクト: alfredo/evil_cronbot
 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')
コード例 #9
0
ファイル: tuser.py プロジェクト: mayanks/smstweet
  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
コード例 #10
0
ファイル: smscommands.py プロジェクト: mayanks/smstweet
  def registerUser(self, phoneno, user_name, passwd):
    client = OAuthClient('twitter', self)
    token = client.get_xauth_token(user_name, passwd)
    if token:
      tuser = TwitterUser.create_by_phonenumber(phoneno, token.specifier)
      tuser.accessTokenid = '-1'
      tuser.put()
      dstat = DailyStat.get_by_date()
      dstat.new_user()

      self.response.out.write("Congratulations !! Your twitter username is registered. Go ahead and send a twitter message by SMSing \"twt <your twitter status\"")
    else:
      logging.warning("Failed to get token for user %s with passwd %s\n" % (user_name, passwd )) 
      self.response.out.write("Incorrect username/password. Note that both username and password are case sensitive. Better register online at http://www.smstweet.in") 
コード例 #11
0
ファイル: users.py プロジェクト: dudarev/twitter_login
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
コード例 #12
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)
コード例 #13
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)
コード例 #14
0
ファイル: main.py プロジェクト: Atrac613/mocomo.co
 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.')
コード例 #15
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)