def get(self):
        twitter = AppEngineTwitter()
        session = appengine_utilities.sessions.Session(writer='cookie')

        if not session.has_key('access_token'):
            twitter.set_oauth(OAUTH_KEY, OAUTH_SECRET)
            req_info = twitter.prepare_oauth_login()
            session['request_token']  = req_info['oauth_token']
            session['request_secret'] = req_info['oauth_token_secret']
            return self.redirect(req_info['url'])
            
        twitter.set_oauth(OAUTH_KEY,
                          OAUTH_SECRET,
                          session['access_token'],
                          session['access_secret'],
                          )

        twitter.friends_ids()
        friends = simplejson.loads(twitter.last_response.content)

        query = self.request.get('query')
        page  = self.request.get('page') or 1
        
        results = twitter.search(
            query.encode('utf8'),
            {'rpp': 50,
             'page': page,
             }
            )

        for result in results:
            if result['from_user_id'] in friends:
                result['is_following'] = 1
            result['unique_id'] = md5.new(result['from_user'] + result['created_at']).hexdigest()

        twitter.verify()
        my = simplejson.loads(twitter.last_response.content)

        template_values = {
            'query':   query,
            'encoded_query': urllib.quote(query.encode('utf8')),
            'results': results,
            'my':      my,
            'page':    page,
            'users':   results,
            }

        path = os.path.join(os.path.dirname(__file__), 'search.html')
        self.response.out.write(template.render(path, template_values))
コード例 #2
0
    def get(self):
        twitter = AppEngineTwitter()
        session = appengine_utilities.sessions.Session(writer='cookie')

        if not session.has_key('access_token'):
            twitter.set_oauth(OAUTH_KEY, OAUTH_SECRET)
            req_info = twitter.prepare_oauth_login()
            session['request_token'] = req_info['oauth_token']
            session['request_secret'] = req_info['oauth_token_secret']
            return self.redirect(req_info['url'])

        twitter.set_oauth(
            OAUTH_KEY,
            OAUTH_SECRET,
            session['access_token'],
            session['access_secret'],
        )

        twitter.friends_ids()
        friends = simplejson.loads(twitter.last_response.content)

        query = self.request.get('query')
        page = self.request.get('page') or 1

        results = twitter.search(query.encode('utf8'), {
            'rpp': 50,
            'page': page,
        })

        for result in results:
            if result['from_user_id'] in friends:
                result['is_following'] = 1
            result['unique_id'] = md5.new(result['from_user'] +
                                          result['created_at']).hexdigest()

        twitter.verify()
        my = simplejson.loads(twitter.last_response.content)

        template_values = {
            'query': query,
            'encoded_query': urllib.quote(query.encode('utf8')),
            'results': results,
            'my': my,
            'page': page,
            'users': results,
        }

        path = os.path.join(os.path.dirname(__file__), 'search.html')
        self.response.out.write(template.render(path, template_values))
コード例 #3
0
def run(search_term):
    acc_token = pickle.load(file(KEY_FILE_API))
    gae_twitter = AppEngineTwitter()
    gae_twitter.set_oauth(CONSUMER_KEY, CONSUMER_SECRET, acc_token.key,
                          acc_token.secret)

    results = gae_twitter.search(search_term.encode('utf8'), {'rpp': 20})
    api = oauth_twitter(
    )  #twitter.Api(username=bot_username, password=bot_password)
    escape_user_list = []
    escape_user_list.append(BOT_USERNAME)

    # Get most corrently tweeted tweet
    status = api.GetUserTimeline()

    if debug_flag:
        print "Debugging..."
        hoge = api.GetReplies()
        for h in hoge:
            print h

    for s in status:
        if s.text.startswith("RT"):
            recent_tweet = s.text
            break
        else:
            print "The following tweet would be posted by hand, so skipped it."
            print "Tweet: " + s.text.encode('utf8')
            print

    print "Recent Tweet: " + recent_tweet.encode('utf8')
    print

    # Search Most Recent Tweet
    results.reverse()
    flag_enable = 0
    for i, result in enumerate(results):
        rt = "RT [at]" + result['from_user'] + " " + result['text']
        rt_len = len(rt)
        if debug_flag:
            print "[Debug] rt[" + str(i) + "]: " + rt.encode('utf8')

        if flag_enable:
            print "I am going to tweet the tweet above."
            if rt_len > MAX_LEN:
                print "But, this tweet length is longer that 140 characters, so skipped it."
                continue
            if result['from_user'] in escape_user_list:
                print "But, this tweet above is tweeted by Escape User, so skipped it."
                continue
            if result['text'].startswith('@'):
                print "But, this tweet above starts with '@', so skipped it."
                continue
            """
         Retweet and exit
         """
            if debug_flag:
                print "Next Tweet: " + rt.encode('utf8')
            else:
                print "I have re-tweeted: " + rt.encode('utf8')
                print "Result of my re-tweeting: " + str(
                    gae_twitter.update(rt.encode('utf8')))
            exit()

        if recent_tweet.replace("@", "[at]") == rt.replace("@", "[at]"):
            if debug_flag:
                print "My Most Recent Tweet: " + recent_tweet.encode('utf8')
                print "-----------------------------------------------------"
            flag_enable = 1

    if flag_enable:
        print "There are no tweet found that I should tweet."
        exit()
    print
    print "There are no tweets recently tweeted, so tweet the oldest tweet."
    print
    #   print "results: ",
    #   print str(results)
    for i, result in enumerate(results):
        rt = "RT [at]" + result['from_user'] + " " + result['text']
        rt_len = len(rt)
        if debug_flag:
            print "[Debug] rt[" + str(i) + "]: " + rt.encode('utf8')

        print "I am going to tweet the tweet above."
        if rt_len > MAX_LEN:
            print "But, this tweet length is longer that 140 characters, so skipped it."
            continue
        if result['from_user'] in escape_user_list:
            print "But, this tweet above is tweeted by Escape User, so skipped it."
            continue
        if result['text'].startswith('@'):
            print "But, this tweet above starts with '@', so skipped it."
            continue
        """
      Retweet and exit
      """
        if debug_flag:
            print "Next Tweet: " + rt.encode('utf8')
        else:
            print "I have tweeted: " + rt.encode('utf8')
            print "Result of my re-tweeting: " + str(
                gae_twitter.update(rt.encode('utf8')))
        exit()
コード例 #4
0
ファイル: main.py プロジェクト: yasulab/CafeMiyamaBot
def run(name, pswd, search_term):
   acc_token = pickle.load(file(KEY_FILE_API))
   gae_twitter = AppEngineTwitter()   
   gae_twitter.set_oauth(OAUTH_KEY,
                         OAUTH_SECRET,
                         acc_token.key,
                         acc_token.secret)

   results = gae_twitter.search(search_term.encode('utf8'), {'rpp': 20})
   api = oauth_twitter() #twitter.Api(username=bot_username, password=bot_password)
   escape_user_list = []
   escape_user_list.append(name)
   escape_user_list.append("milkcocoa")
   escape_user_list.append("ikiben")
   
   # Get most corrently tweeted tweet
   status = api.GetUserTimeline()
   
   print "Debugging..."
   hoge = api.GetReplies()
   for h in hoge:
      print h
   
   for s in status:
      if s.text.startswith("RT"):
         recent_tweet = s.text
         break
      else:
         print "The following tweet would be posted by hand, so skipped it."
         print "Tweet: " + s.text.encode('utf8')
         print
      
   print "Recent Tweet: "+recent_tweet.encode('utf8')
   print

   # Search Most Recent Tweet
   results.reverse()
   flag_enable = 0
   for i,result in enumerate(results):
      rt = "RT [at]" + result['from_user']  + " " + result['text']
      rt_len = len(rt)
      if debug_flag:
         print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8') 
      
      if flag_enable:
         print "I am going to tweet the tweet above."
         if rt_len > MAX_LEN:
            print "But, this tweet length is longer that 140 characters, so skipped it."
            continue
         if result['from_user'] in escape_user_list:
            print "But, this tweet above is tweeted by Escape User, so skipped it."
            continue
         if result['text'].startswith('@'):
            print "But, this tweet above starts with '@', so skipped it."
            continue
         """
         Retweet and exit
         """
         if debug_flag:
            print "Next Tweet: "+rt.encode('utf8')
         else:
            print "I have re-tweeted: "+rt.encode('utf8')
            print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
         exit()
               
      if recent_tweet.replace("@", "[at]") == rt.replace("@", "[at]"):
         if debug_flag:
            print "My Most Recent Tweet: " + recent_tweet.encode('utf8')
            print "-----------------------------------------------------"
         flag_enable = 1

   if flag_enable:
      print "There are no tweet found that I should tweet."
      exit()
   print
   print "There are no tweets recently tweeted, so tweet the oldest tweet."
   print
#   print "results: ",
#   print str(results)
   for i,result in enumerate(results):  
      rt = "RT [at]" + result['from_user']  + " " + result['text']  
      rt_len = len(rt)
      if debug_flag:
         print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8') 

      print "I am going to tweet the tweet above."
      if rt_len > MAX_LEN:
         print "But, this tweet length is longer that 140 characters, so skipped it."
         continue
      if result['from_user'] in escape_user_list:
         print "But, this tweet above is tweeted by Escape User, so skipped it."
         continue
      if result['text'].startswith('@'):
         print "But, this tweet above starts with '@', so skipped it."
         continue
      """
      Retweet and exit
      """
      if debug_flag:
         print "Next Tweet: "+rt.encode('utf8')
      else:
         print "I have tweeted: "+rt.encode('utf8')
         print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
      exit()