Example #1
0
 def update(self, status=None, in_reply_to=None):
     count = Statuses.all().count()
     if not status:
         status = random.choice(Statuses.all().fetch(1000)).status
     url = "http://twitter.com/statuses/update.json"
     data = urllib.urlencode({"status": status.encode("utf-8"), "in_reply_to_status_id": in_reply_to})
     result = urlfetch.fetch(url=url, method=urlfetch.POST, payload=data, headers=self.auth_header)
     logging.debug(result.status_code)
     logging.debug(result.content)
Example #2
0
 def update(self, status = None, in_reply_to = None):
     count = Statuses.all().count()
     if not status:
         status = random.choice(Statuses.all().fetch(1000)).status
     url  = 'http://api.twitter.com/1/statuses/update.json'
     data = {
         'status' : status.encode('utf-8'),
         'in_reply_to_status_id' : in_reply_to,
         }
     result = self.client.make_request(
         url,
         token   = self.bot_config['access_token'],
         secret  = self.bot_config['access_token_secret'],
         additional_params = data,
         protected         = True,
         method            = urlfetch.POST)
     logging.debug(result.status_code)
     logging.debug(result.content)
Example #3
0
    def hanrize(self):
#        cache = memcache.decr(HANRIZE_COUNT)
#        if cache:
#            logging.debug('count: %d' % cache)
#            return

        url = 'http://api.twitter.com/1/statuses/home_timeline.json'
        result = self.client.make_request(
            url,
            token   = self.bot_config['access_token'],
            secret  = self.bot_config['access_token_secret'],
            additional_params = { 'count': 100 },
            protected         = True,
            method            = urlfetch.GET)
        logging.debug(result.status_code)
        if result.status_code == 200:
            statuses = simplejson.loads(result.content)

            # 次の実行時間を決定する
#            format = '%a %b %d %H:%M:%S +0000 %Y'
#            first = datetime.strptime(statuses[ 0]['created_at'], format)
#            last  = datetime.strptime(statuses[-1]['created_at'], format)
#            logging.debug('first : %s' % first)
#            logging.debug('last  : %s' % last)
#            logging.debug(first - last)
#            memcache.set(HANRIZE_COUNT, (first - last).seconds * 2 / 60)

            def judge(status):
                # 過去発言は繰り返さない
#                reply_ids = Statuses.all()
#                if status['id']:
#                    return False

                # 非公開の発言も除く
                if status['user']['protected']:
                    return False
                # 120文字以上の発言は除く
                if len(status['text']) > 120:
                    return False
                # RTっぽい発言も除く
                if re.search('RT[ :].*@\w+', status['text']):
                    return False
                # ハッシュタグっぽいものを含んでいる発言も除く
                if re.search(u'[##]\w+', status['text']):
                    return False
                # 既に「裸」が含まれている発言も除く
                if re.search(u'裸', status['text']):
                    return False
                # それ以外のものはOK
                return True

            # 残ったものからランダムに選択して半裸にする
            candidate = filter(judge, statuses)
            random.shuffle(candidate)
            hanra = Hanra()
            
            for status in candidate:
                text = hanra.hanrize(status['text']).decode('utf-8')
                
                # BadValueError: Property status is not multi-line 対策
                text = text.strip('\t\n\x0b\x0c\r')
                
                # うまく半裸にできたもの かつ 120文字まで
                if re.search(u'半裸で', text):    # and len(text) < 120:
                    logging.debug(text)

                    rep_id = status['id']
#                    query = Statuses.all().filter('in_reply_to =', rep_id)

#                    query = GqlQuery('SELECT * FROM Statuses WHERE in_reply_to = rep_id')
#                    exist = db.get(db.Key.from_path('Statuses', rep_id))
#                    if query == None:
#                    if not query:

                    logging.debug(rep_id)

                    tweeted = Statuses()
                    tweeted.status = text
                    tweeted.in_reply_to = status['id']
                    tweeted.put()
                
                    self.update(status = u'半裸RT @%s: %s' % (
                        status['user']['screen_name'],
                        text,
                        ), in_reply_to = status['id'])


                    break