Skip to content

Instantly share code, notes, and snippets.

@brandonrobertz
Created January 11, 2022 00:16
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 brandonrobertz/44ed6581c64f388cc1ea8d291e3b38d5 to your computer and use it in GitHub Desktop.
Save brandonrobertz/44ed6581c64f388cc1ea8d291e3b38d5 to your computer and use it in GitHub Desktop.
Python Package Index (PyPI) command line search utility
#!/bin/bash
#
# pypi-search.sh
# Author: Brandon Roberts (bxroberts.org)
#
# This is a simple Python package search script. It uses
# Hext (https://hext.thomastrapp.com/) to extract results
# from a HTTP search request.
#
# Install Hext for this to work: pip install hext
# More information: https://hext.thomastrapp.com/download
#
# NOTE: This only gets the first page of results!
# Subsequent pages could be fetched by adding '&page=2'
# to the URL, if the user wants more pages and stopping when there's
# no more results
#
set -e
HEXT_FILE=/tmp/.pypi-search.hext-template.hext
HTML_FILE=/tmp/.pypi-search.search.html
echo '
<a class*="package-snippet" href:prepend("https://pypi.org"):href >
<h3 class*="package-snippet__title">
<span class*="package-snippet__name" @text:name />
<span class*="package-snippet__version" @text:version />
<span class*="package-snippet__released"><time @text:released></time></span>
</h3>
<p class*="package-snippet__description" @text:description />
</a>
' > ${HEXT_FILE}
curl --silent -G --data-urlencode "q=${@}" https://pypi.org/search/ > ${HTML_FILE}
htmlext -x ${HEXT_FILE} -i ${HTML_FILE}
rm -f ${HEXT_FILE} ${HTML_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment