Last active
August 29, 2015 14:06
-
-
Save mvcisback/5463e6cb3f7715449442 to your computer and use it in GitHub Desktop.
Shorten urls using bitly and python
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
function task() { | |
shorten-urls task "$@" | |
} |
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
#!/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() |
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
#!/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