Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Created May 16, 2018 07:05
Show Gist options
  • Save charlesreid1/a98665c5e7b504835189304f0ed90fd9 to your computer and use it in GitHub Desktop.
Save charlesreid1/a98665c5e7b504835189304f0ed90fd9 to your computer and use it in GitHub Desktop.
twitter-python with a URL shortener
# https://github.com/bear/python-twitter/issues/298#issuecomment-185193763
import re
from twitter import Api
from twitter.twitter_utils import URL_REGEXP
from shorten_url import ShortenURL
CONSUMER_KEY = 'WWWWWWW'
CONSUMER_SECRET = 'XXXXXXXXXXX'
ACCESS_TOKEN = 'YYYYYYYYYYY'
ACCESS_TOKEN_SECRET = 'ZZZZZZZZZ'
def PostStatusWithShortenedURL(status):
shortener = ShortenURL()
api = Api(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET)
urls = re.findall(URL_REGEXP, status)
for url in urls:
status = status.replace(url, shortener.Shorten(url), 1)
api.PostUpdate(status)
if __name__ == '__main__':
PostStatusWithShortenedURL("this is a test: http://www.example.com/tests")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment