Пример #1
0
def add_twitter():
    user = users.get_current_user()
    if user is None:
        redirect("/login?{0}".format(
            urllib.urlencode(dict(continue_url=request.url))))
    else:
        session = request.environ.get('beaker.session')
        auth = session.get("twitter_auth")
        verifier = request.GET.get('oauth_verifier')

        if auth is not None and auth.request_token is None:
            auth = None

        twi = TwitterUser.get_by_key_name(user.email())

        auth_url = ''
        if twi is None:

            #first
            if auth is None or verifier is None:
                auth = tweepy.OAuthHandler(consumer_key, consumer_secret,
                                           request.url)
                auth_url = auth.get_authorization_url()
                session["twitter_auth"] = auth
                session.save()
                redirect(auth_url)
            else:
                try:
                    auth.get_access_token(verifier)
                except tweepy.TweepError:
                    error('Error! Failed to get access token.')

                twi = TwitterUser.get_or_insert(
                    user.email(),
                    user=user,
                    last_retweeted_id=0,
                    twitter_access_token=auth.access_token.key,
                    twitter_access_token_secret=auth.access_token.secret,
                    twitter_id=auth.get_username())

                session["twitter_auth"] = None
                session.save()

        redirect("/twitter")
Пример #2
0
def twitter():
    user = users.get_current_user()
    if user is None:
        redirect("/login?{0}".format(
            urllib.urlencode(dict(continue_url=request.url))))

    twi = TwitterUser.get_by_key_name(user.email())

    return dict(is_twitter_added=twi is not None,
                twitter_id='' if twi is None else twi.twitter_id)
Пример #3
0
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)
Пример #4
0
def remove_twitter():
    user = users.get_current_user()
    if user is None:
        redirect("/login")
    else:
        twi = TwitterUser.get_by_key_name(user.email())
        if twi is not None:
            twi.delete()
            msg = "deleted."
        else:
            msg = "not auth."

        return dict(message=msg)
Пример #5
0
def review_tweets():
    user = users.get_current_user()
    if user is None:
        redirect("/login?{0}".format(
            urllib.urlencode(dict(continue_url=request.url))))

    twi = TwitterUser.get_by_key_name(user.email())
    if twi is None:
        return dict(is_twitter_missing=True,
                    message="You haven't link your twitter account.")

    to_time = request.GET.get('to_time')
    since_time = request.GET.get('since_time')

    def_since, def_to = default_range()
    to_time = def_to if to_time is None else datetime.strptime(
        to_time, DATETIME_FORMAT)
    since_time = def_since if since_time is None else datetime.strptime(
        since_time, DATETIME_FORMAT)

    tweets = retrive_tweet_data(user, since_time, to_time)

    if request.is_ajax:
        return json.dumps([
            dict(retweet_time=t.retweet_time.strftime(DATETIME_FORMAT),
                 local_time=(t.retweet_time +
                             CSTTZ).strftime(DATETIME_FORMAT_SHORT),
                 tweet_text=t.tweet_text) for t in tweets
        ])

    return dict(since_time=since_time,
                twitter_id=twi.twitter_id,
                tweets=tweets,
                datefmt=DATETIME_FORMAT,
                datefmt_short=DATETIME_FORMAT_SHORT,
                tzdelta=CSTTZ)