Created
March 30, 2009 09:33
-
-
Save cosmin/87711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a basic example for taking a title and url, shortening it with bit.ly and posting it to twitter | |
# trimming the title down if necessary. | |
import os | |
import urllib2 | |
import urllib | |
TWITTER_USERNAME = 'test' | |
TWITTER_PASSWORD = 'test' | |
BITLY_USERNAME = 'test' | |
BITLY_API_KEY = 'test' | |
MESSAGE_PREFIX = '' | |
def post_to_twitter(message): | |
os.system('curl -u %s:%s -d status="%s" http://twitter.com/statuses/update.xml' % ( | |
TWITTER_USERNAME, | |
TWITTER_PASSWORD, | |
message)) | |
def shorten(url): | |
q = urllib.urlencode(dict(version='2.0.1',login=BITLY_USERNAME, apiKey=BITLY_API_KEY, longUrl=url)) | |
url = 'http://api.bit.ly/shorten?' + q | |
data = eval(urllib2.urlopen(url).read()) | |
return data['results'].values()[0]['shortUrl'] | |
def submit(title, url): | |
short_url = shorten(url) | |
max_len = 140 - len(' from @' + TWITTER_USERNAME) - len(MESSAGE_PREFIX) - len(short_url) | |
if len(title) > max_len: | |
title = title[:max_len - 3] + '...' | |
message = '%s %s %s' % (MESSAGE_PREFIX, title, short_url) | |
post_to_twitter(message) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment