Skip to content

Instantly share code, notes, and snippets.

@adi-g15
Created May 20, 2021 07:33
Show Gist options
  • Save adi-g15/1e283bbe2c51878665fbf4372894e5f3 to your computer and use it in GitHub Desktop.
Save adi-g15/1e283bbe2c51878665fbf4372894e5f3 to your computer and use it in GitHub Desktop.
pip search alternative
#!/bin/bash
# pypi-search.sh 
# This script fetch data from https://pypi.org/simple/ 
# process the output for simple package name output with perl
# and then apply a regex pattern to the result

pypiurl=https://pypi.org/simple/
currentdate=$(date +%y%m%d)

cachedir=~/.cache/simple-pypi
[[ -d $cachedir ]] || mkdir -p $cachedir

cachefile=$(ls -1 $cachedir/*_simple-pypi.html 2>/dev/null | sort | head -n1)
[[ $cachefile = "" ]] && cachefile=$cachedir/"${currentdate}_simple-pypi.html"

searchpattern="$1"
cmd="$2"

if [[ -f $cachefile ]] ; then
    dbdate=$(echo $cachefile | grep -Po "[0-9]{6,6}")
    # if db is older than 3 days or second parameter is 'update'
    ( (( ($currentdate - $dbdate) > 3 )) || [[ "x$cmd"  = 'xupdate' ]] ) && {
        echo "last update was on : $dbdate"
        cachefile=$cachedir/"${currentdate}_simple-pypi.html"
        wget -q --show-progress -O - $pypiurl > $cachefile
    }
else
    wget -q --show-progress -O - $pypiurl > $cachefile
fi

[[ x$searchpattern = "x" ]] && read -p "Enter pypi name pattern : " searchpattern
perl -pe 's/.*([\/"]{1,1}\>){1,1}([^>]+(.*)[^<])+\<\/a\>/\2/g' $cachefile | grep -P "$searchpattern"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment