Last active
September 22, 2019 17:06
-
-
Save alexwlchan/e28ec1ee5ac16de450cf06664e9e8471 to your computer and use it in GitHub Desktop.
Script for an Alfred shortcut to open PyPI docs
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 | |
# -*- encoding: utf-8 | |
""" | |
Try to open the documentation for a Python package, or the PyPI page if | |
it can't be found. | |
""" | |
import json | |
import sys | |
import webbrowser | |
try: | |
from urllib.request import urlopen | |
except ImportError: # Python 2 | |
from urllib2 import urlopen | |
package_name = sys.argv[1] | |
resp = urlopen("https://pypi.org/pypi/%s/json" % package_name) | |
data = json.load(resp) | |
try: | |
docs_url = data["info"]["project_urls"]["Documentation"] | |
except KeyError: | |
docs_url = "https://pypi.org/project/%s/" % package_name | |
webbrowser.open(docs_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment