Skip to content

Instantly share code, notes, and snippets.

@mathieu-aubin
Last active June 22, 2022 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieu-aubin/c40fd13f1c6bb5a0ce66342b46b87591 to your computer and use it in GitHub Desktop.
Save mathieu-aubin/c40fd13f1c6bb5a0ce66342b46b87591 to your computer and use it in GitHub Desktop.
Download PDF patent files from https://patents.google.com using download url and description
#!/bin/bash
#
# Downloads patent files from patents.google.com as pdf
# Must provide 2 arguments which are.. (quotes encapsulated)
#
# File URL (pdf download link)
# Description or name of patent
#
# File will be saved as PATENT#_DESCRIPTION.pdf
#
# Spaces in description will be converted to dots
#
if [ "$#" -eq 2 ]; then
URL="${1}";
DESC="${2//\ /\.}";
# File name from the url, patent pdf file
FNAME="${URL##*/}";
# Complete filename to save pdf document to
NAME="${FNAME%*.pdf}"_"${DESC}".pdf;
# Check if patent file already exist based on patent number, download or skip
if [ ! -e *"${FNAME%*.pdf}"* ]; then
# Get pdf document and save to proper name and list downloaded file
curl -#Lko "${NAME}" "${URL}" && ls -lah "${NAME}" || echo "ERROR: Could not download file from provided URL.";
else
# Skip because file exist...
>&2 echo "File with patent number ${FNAME%*.pdf} already exist, skipping";
exit 1;
fi
else
>&2 echo -e "$(basename $0):\n\tMust provide 2 arguments as in URL and DESCRIPTION, quotes encapsulated";
>&2 echo -e "\t\tdlpatent http://blahblah/patent.number.pdf 'Description Of Patent'";
>&2 echo -e "\t\t--> patent.number_Description.Of.Patent.pdf";
exit 1;
fi
# Ensure definite exit code
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment