Skip to content

Instantly share code, notes, and snippets.

@mvcisback
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvcisback/5463e6cb3f7715449442 to your computer and use it in GitHub Desktop.
Save mvcisback/5463e6cb3f7715449442 to your computer and use it in GitHub Desktop.
Shorten urls using bitly and python
function task() {
shorten-urls task "$@"
}
#!/usr/bin/env python
import requests
import click
ENDPOINT = 'https://api-ssl.bitly.com/v3/shorten'
@click.command()
@click.option('--api-key', envvar='BITLY_API_KEY')
@click.argument('uri')
def main(api_key, uri):
payload = {
'access_token': api_key,
'uri': uri,
'format': 'json',
}
response = requests.get(ENDPOINT, params=payload)
result = response.json()
print(result['data']['url'])
if __name__ == '__main__':
main()
#!/usr/bin/env python2
import re
import sys
from subprocess import check_call, check_output
def shorten(url):
if not re.match(r'(https?://\S+)', url):
return url
print("Shortening url")
return check_output(['shorten', url])
def main():
argv = list(map(shorten, sys.argv[1:]))
exit(check_call(argv, shell=False))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment