Example #1
0
def show_mentions():
    token = get_twitter_token()
    if not token:
        return render_template('prompt.html')

    if 'max_id' in request.args:
        resp = twitter.get('statuses/mentions_timeline.json',
                           data={'max_id': request.args['max_id']})
    else:
        resp = twitter.get('statuses/mentions_timeline.json')

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    since_id = max_id = None
    if resp.status == 200:
        tweets = resp.data

        for tweet in tweets:
            if since_id is None or since_id < tweet['id']:
                since_id = tweet['id']
            if max_id is None or max_id > tweet['id']:
                max_id = tweet['id']
        max_id -= 1
    else:
        tweets = []

        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('timeline.html', tweets=tweets, max_id=max_id,
                           since_id=since_id, endpoint="show_mentions",
                           endpoint_args={})
Example #2
0
def thread(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status != 200 or not resp.data['in_reply_to_status_id']:
        tweet = None
    else:
        tweet = resp.data
        resp = twitter.get('statuses/show.json',
                           data={'id': tweet['in_reply_to_status_id']})
        if resp.status == 200:
            replied_tweet = resp.data
        else:
            replied_tweet = None

    if tweet is None or replied_tweet is None:
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('reply.html',
                           id=id,
                           tweet=tweet,
                           replied_tweet=replied_tweet)
Example #3
0
def thread(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status != 200 or not resp.data['in_reply_to_status_id']:
        tweet = None
    else:
        tweet = resp.data
        resp = twitter.get('statuses/show.json',
                           data={'id': tweet['in_reply_to_status_id']})
        if resp.status == 200:
            replied_tweet = resp.data
        else:
            replied_tweet = None

    if tweet is None or replied_tweet is None:
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('reply.html', id=id, tweet=tweet,
                           replied_tweet=replied_tweet)
Example #4
0
def show_user(name):
    if not get_twitter_token():
        return redirect(url_for('show_index'))

    resp = twitter.get('users/show.json', data={'screen_name': name})

    if resp.status == 401:
        flash('Unauthorized account access, or blocked account.')
        return redirect(url_for('show_index'))

    profile = resp.data

    if 'max_id' in request.args:
        resp = twitter.get('statuses/user_timeline.json',
                           data={
                               'max_id': request.args['max_id'],
                               'screen_name': name
                           })
    else:
        resp = twitter.get('statuses/user_timeline.json',
                           data={'screen_name': name})

    if request.is_xhr:
        if resp.status == 401:
            return jsonify(success=False, data='Unauthorized account access.')

        since_id, max_id, tweets = timeline_pagination(resp)

        return jsonify(success=True,
                       data=render_template('_timeline.html',
                                            tweets=tweets,
                                            max_id=max_id,
                                            since_id=since_id,
                                            endpoint='show_user',
                                            endpoint_args={'name': name}))
    else:
        if resp.status == 401:
            flash('The user is protected, or you are blocked.')

        since_id, max_id, tweets = timeline_pagination(resp)

        return render_template('profile.html',
                               tweets=tweets,
                               max_id=max_id,
                               since_id=since_id,
                               endpoint="show_user",
                               endpoint_args={'name': name},
                               profile=profile)
Example #5
0
def reply(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        tweet = resp.data
    else:
        tweet = None
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    names = re.findall(r'(@\w+)', tweet['text'])
    author = '@' + tweet['user']['screen_name']
    user = '******' + session['twitter_user']
    if author in names:
        names.remove(author)
    if user in names:
        names.remove(user)
    tweet_prefix = ' '.join([author] + names) + ' '

    return render_template('reply.html',
                           id=id,
                           tweet=tweet,
                           tweet_prefix=tweet_prefix)
Example #6
0
def reply(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        tweet = resp.data
    else:
        tweet = None
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    names = re.findall(r'(@\w+)', tweet['text'])
    author = '@'+tweet['user']['screen_name']
    user = '******'+session['twitter_user']
    if author in names:
        names.remove(author)
    if user in names:
        names.remove(user)
    tweet_prefix = ' '.join([author] + names) + ' '

    return render_template('reply.html', id=id, tweet=tweet,
                           tweet_prefix=tweet_prefix)
Example #7
0
def show_mentions():
    if not get_twitter_token():
        return redirect(url_for('show_index'))

    if 'max_id' in request.args:
        resp = twitter.get('statuses/mentions_timeline.json',
                           data={'max_id': request.args['max_id']})
    else:
        resp = twitter.get('statuses/mentions_timeline.json')

    if request.is_xhr:
        if resp.status == 401:
            return jsonify(success=False, data='Unauthorized account access.')

        since_id, max_id, tweets = timeline_pagination(resp)

        return jsonify(success=True,
                       data=render_template('_timeline.html',
                                            tweets=tweets,
                                            max_id=max_id,
                                            since_id=since_id,
                                            endpoint='show_mentions',
                                            endpoint_args={}))
    else:
        if resp.status == 401:
            session.pop('twitter_token')
            flash('Unauthorized account access.')
            return redirect(url_for('show_index'))

        since_id, max_id, tweets = timeline_pagination(resp)

        return render_template('timeline.html',
                               tweets=tweets,
                               max_id=max_id,
                               since_id=since_id,
                               endpoint="show_mentions",
                               endpoint_args={})
Example #8
0
def quote(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        tweet = resp.data
    else:
        tweet = None
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('quote.html', id=id, tweet=tweet)
Example #9
0
def quote(id):
    resp = twitter.get('statuses/show.json', data={'id': id})

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        tweet = resp.data
    else:
        tweet = None
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('quote.html', id=id, tweet=tweet)
Example #10
0
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('show_index')
    if resp is None:
        flash(u'You have been denied the request to sign in.')
        return redirect(next_url)

    session['twitter_token'] = (resp['oauth_token'],
                                resp['oauth_token_secret'])
    session['twitter_user'] = resp['screen_name']
    session['twitter_id'] = resp['user_id']

    icon = twitter.get('users/show.json?screen_name=%s' % resp['screen_name'])
    if icon.status == 200:
        session['twitter_name'] = icon.data['name']
        session['twitter_icon'] = icon.data['profile_image_url']

    return redirect(next_url)
Example #11
0
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('show_index')
    if resp is None:
        flash(u'You have been denied the request to sign in.')
        return redirect(next_url)

    session['twitter_token'] = (resp['oauth_token'],
                                resp['oauth_token_secret'])
    session['twitter_user'] = resp['screen_name']
    session['twitter_id'] = resp['user_id']

    icon = twitter.get('users/show.json?screen_name=%s' % resp['screen_name'])
    if icon.status == 200:
        session['twitter_name'] = icon.data['name']
        session['twitter_icon'] = icon.data['profile_image_url']

    return redirect(next_url)
Example #12
0
def show_messages():
    token = get_twitter_token()
    if not token:
        return redirect(url_for('show_index'))

    resp = twitter.get('direct_messages.json')
    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        messages = resp.data
    else:
        messages = []
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('messages.html', messages=messages)
Example #13
0
def show_messages():
    token = get_twitter_token()
    if not token:
        return render_template('prompt.html')

    resp = twitter.get('direct_messages.json')
    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status == 200:
        messages = resp.data
    else:
        messages = []
        flash('Unable to load tweets from Twitter. Maybe out of '
              'API calls or Twitter is overloaded.')

    return render_template('messages.html', messages=messages)
Example #14
0
def unretweet(id):
    resp = twitter.get('statuses/show/%d.json?include_my_retweet=1' % id)

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status != 200:
        for error in resp.data['errors']:
            flash(error['message'])
    else:
        retweet_id = resp.data['current_user_retweet']['id']
        resp = twitter.post('statuses/destroy/%d.json' % retweet_id)
        if resp.status == 200:
            flash('Successfully unretweeted.')
        else:
            flash('Unretweet failed.')

    return redirect(request.referrer or url_for('show_index'))
Example #15
0
def unretweet(id):
    resp = twitter.get('statuses/show/%d.json?include_my_retweet=1' % id)

    if resp.status == 401:
        session.pop('twitter_token')
        flash('Unauthorized account access.')
        return redirect(url_for('show_index'))

    if resp.status != 200:
        for error in resp.data['errors']:
            flash(error['message'])
    else:
        retweet_id = resp.data['current_user_retweet']['id']
        resp = twitter.post('statuses/destroy/%d.json' % retweet_id)
        if resp.status == 200:
            flash('Successfully unretweeted.')
        else:
            flash('Unretweet failed.')

    return redirect(request.referrer or url_for('show_index'))