def newretweeted(): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) twitter_user = TwitterUser.all().fetch(limit=10) random.shuffle(twitter_user) queue_cnt = 0 for usr in twitter_user: auth.set_access_token(usr.twitter_access_token, usr.twitter_access_token_secret) api = tweepy.API(auth) rts = api.user_timeline(user_id=usr.twitter_id, since_id=usr.last_retweeted_id if usr.last_retweeted_id != 0 else None, include_rts=1, exclude_replies=1, count=30) rts = [t for t in rts if hasattr(t, "retweeted_status")] if len(rts) > 0: usr.last_retweeted_id = rts[0].id usr.put() for t in reversed(rts): tweet_text = u"RT @{0}: {1}".format( t.retweeted_status.user.screen_name, get_tweet_urls_text(t.retweeted_status)) msg_text = htmlentitydecode(u"via {0}: {1}".format( t.user.screen_name, tweet_text)) dt = SavedTweets(tweet_id=t.id, user=usr.user, pushed_flag=False, tweet_text=msg_text) dt.put() taskqueue.add(url='/tasks/push_retweet', name='push_{0}'.format(t.id), countdown=queue_cnt * 120, params=dict(dbkey=dt.key())) queue_cnt += 1 region_invalidate(retrive_tweet_data, "short_term", usr.user, *default_range()) info = "schedualed {0} tweet(s) from {1}".format( queue_cnt, usr.twitter_id) else: info = "no new retweet from {0}".format(usr.twitter_id) logging.info(info)
def retrive_tweet_data(user, since_time, to_time): items_limit = 20 week_tweets_query = SavedTweets.all().filter("user ="******"retweet_time >=", since_time).\ filter("retweet_time <", to_time).\ order("-retweet_time") tweets = week_tweets_query.fetch(limit=items_limit) return tweets
def push_retweet(): dbkey = request.POST["dbkey"] t = SavedTweets.get(keys=dbkey) if t is not None: t.tweet_text map(lambda ct: xmpp.send_message(ct.addr, t.tweet_text), SubscribeContacts.all().fetch(limit=10)) t.pushed_flag = True t.put()