Created
March 26, 2015 17:34
-
-
Save dsanson/285d52a077f7e01d44f1 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# | |
# Get A Phil Papers Item (gappi); version 0.3 | |
# by Kevin C. Klement ([email protected]) | |
# Feel free to email Kevin with suggestions or questions | |
# License: GPLv3 | |
# | |
# get the name of the script | |
SCRIPTNAME="$(basename "$0")" | |
# function to show usage info | |
usage_info() { | |
cat > /dev/stderr << EOF | |
USAGE: $SCRIPTNAME [-n NN] "search item 1" "search item 2" ... | |
For each search item, gappi will fetch the BibTeX record | |
for the first [or first NN] PhilPapers result for that search item. Each | |
item's individual search parameters should be enclosed in quotes. | |
You can use the additional search parameters listed at. | |
http://philpapers.org/help/search.html | |
E.g.: | |
$SCRIPTNAME "@authors Strawson @title On Referring" | |
Note that it is "@authors" with an s, not "@author", even | |
when there is only one. | |
Use an backslash escaped \" for a literal quotation mark. | |
EOF | |
} | |
MATCHES=1 # default number of matches to return | |
# if there are no search items given, then show usage info | |
if [[ "$#" == 0 ]] ; then | |
usage_info | |
fi | |
# loop for each search item | |
while [[ "$#" -gt 0 ]] ; do | |
# help users who ask for it | |
if [[ "$1" == "-help" ]] || | |
[[ "$1" == "--help" ]] || | |
[[ "$1" == "-h" ]] || | |
[[ "$1" == "-?" ]] ; then | |
usage_info | |
exit 0 | |
fi | |
# -n NN sets the number of matches to be returned | |
if [[ "$1" == "-n" ]] ; then | |
shift | |
if [[ "$1" -gt 0 ]] ; then | |
MATCHES="$1" | |
shift | |
else | |
echo "The option '-n' must be followed by a postive integer." | |
usage_info | |
exit 0 | |
fi | |
fi | |
# replace spaces with %20 for URLS | |
SEARCHSTRING="${1// /%20}" | |
# get BibTeX entry for that match, send to stdout | |
curl --silent --data-urlencode -G \ | |
-d "search_header=search_header.html&searchStr=$SEARCHSTRING&filterMode=keywords&limit=$MATCHES&newWindow=on&sort=relevance&showCategories=on&proOnly=on&format=bib" \ | |
"http://philpapers.org/asearch.pl" 2>/dev/null | |
# add a line of padding | |
echo "" | |
# move to next search item | |
shift | |
done | |
exit 0 | |
Thanks. I'll go ahead and incorporate these changes on my site, only fixing the typo "pos[i]tive", and having that go to /dev/stderr, since it's an error message, and also exiting with an error code "exit 1" instead of "exit 0" in that case.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simplifies Kevin Klement's
gappi
script---just one call to curl to fetch the bibtex entry for each search item---and adds a command line option,-n NN
, to specify the number of bibtex entries to return for each search term.