Example #1
0
def _send_tweet(request, tweet_message=[], sr=None):
    """Utility function to tweet and save the tweet to the db."""
    
    api = bitlyapi.BitLy(settings.BITLY_USER, settings.BITLY_KEY) 
    url = request.build_absolute_uri().replace("/edit", "")
    res = api.shorten(longUrl=url)
    tweet_message.append("%s" % res['url'])
    send_msg = ' '.join(tweet_message)
    
    # TODO Check here to see if len(send_msg) > 140, then trim it somehow

    try:
        api = get_tweepy_api()
        api.update_status(send_msg)
        last_msg = api.user_timeline(count=1)[0]
        SeriesTweet.objects.create(
            series = sr,
            tweet = send_msg,
            bitly_url = res['url'],
            twitter_status_id = last_msg.id
        )

    except tweepy.TweepError as terror:
        if settings.DEBUG:
            print "tweep error: %s" % terror
    except IOError, ex:
        if settings.DEBUG:
            print "IOError when calling _send_tweet: %s" % ex
def get_latest_tweets():
    user = '******'
    messages_to_display = 5
    api = get_tweepy_api()
    statuses = api.user_timeline(count=messages_to_display)
    messages = []

    for status in statuses:
        # Replaces the @username mentions with a URL    
        replaced_mentions = re.sub(r'\b(@\w+)\b', r'<a href="http://twitter.com/\1">\1</a>',status.text);
        # Replaces the #tag's with a URL
        replaced_hashtags = re.sub(r'\b(#\w+)\b', r'<a href="http://twitter.com/#!/search?q=%23\1">\1</a>',replaced_mentions);
        # Replaces the published times with a URL
        replaced_times = (replaced_hashtags + " "+
            "<span class='tiny-font'><a href='http://twitter.com/#!/"+
            user+"/status/"+str(status.id)+"'>"+str(status.created_at)+
            "</a></span>")
        messages.append(replaced_times)
        
    return messages