Improve && Minimize

This commit is contained in:
Emre AKYÜZ 2023-12-08 20:56:30 +03:00 committed by GitHub
parent 156747aa77
commit c25ce80916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@
BIB_FILE="$HOME/latex/uni.bib" BIB_FILE="$HOME/latex/uni.bib"
correction_method() { correction_method() {
sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/doi:\6/p; T; q' sed -n -E 's/.*((DOI|doi)((\.(org))?\/?|:? *))([^: ]+[^ .]).*/\6/p; T; q'
} }
get_doi_from_pdf() { get_doi_from_pdf() {
@ -15,62 +15,49 @@ get_doi_from_pdf() {
} }
correct_names() { correct_names() {
sed '/^@[a-z]\+{[^[:space:]]\+[0-9]\{4\},/{ sed 's/\}, /\},\n /g
s/\([A-Z]\)/\L\1/g s/, /,\n /
s/_//g s/ }/\n}/
s/[0-9]*\([0-9]\{2\}\)/\1/g s/,\s*pages=/,\n\tpages=/' |
}' sed '1s/^ *//
} 1s/[0-9]*\([0-9]\{2\}\)/\1/
1s/_//
normalize_doi() { 1s/.*/\L&/
doi="$1" s/.*=/\L&/
doi=$(echo "$doi" | sed 's@%@\\x@g' | xargs -I {} printf "%b" "{}") s/=/ = /'
printf "%s" "$doi" | tr 'A-Z' 'a-z'
} }
process_doi() { process_doi() {
doi="$1" doi="$1"
bibtex_entry=$(curl -s "https://api.crossref.org/works/$doi/transform/application/x-bibtex" -w "\\n" | correct_names) bibtex_entry=$(curl -s "https://api.crossref.org/works/"$doi"/transform/application/x-bibtex" | correct_names)
red_color='\033[0;31m' red_color='\033[0;31m'
reset_color='\033[0m' reset_color='\033[0m'
printf "${red_color}%s${reset_color}\n" "$bibtex_entry" printf "${red_color}%s${reset_color}\n" "$bibtex_entry"
[ -z "$bibtex_entry" ] && [ "$(echo "$bibtex_entry" | cut -c2)" != "@" ] && echo "Failed to fetch bibtex entry for DOI: $doi" && return 1 [ -z "$bibtex_entry" ] && [ "$(echo "$bibtex_entry" | cut -c2)" != "@" ] && echo "Failed to fetch bibtex entry for DOI: $doi" && return 1
normalized_doi="${doi#doi:}" grep -Fq "doi = {${doi}}" "$BIB_FILE" || {
grep -q -E "doi\s*=\s*\{$(echo "$normalized_doi" | sed 's/(/\\(/g')\}" "$BIB_FILE" || {
[ -s "$BIB_FILE" ] && echo "" >> "$BIB_FILE" [ -s "$BIB_FILE" ] && echo "" >> "$BIB_FILE"
echo "$bibtex_entry" >> "$BIB_FILE" echo "$bibtex_entry" >> "$BIB_FILE"
echo "Added bibtex entry for DOI: $doi" echo "Added bibtex entry for DOI: $doi"
return 1 return 0
} }
echo "Bibtex entry for DOI: $doi already exists in the file." echo "Bibtex entry for DOI: $doi already exists in the file."
} }
[ -z "$1" ] && echo "Give either a pdf file or a DOI or a directory path that has PDFs as an argument." && exit 1 [ -z "$1" ] && echo "Give either a pdf file or a DOI or a directory path that has PDFs as an argument." && exit 0
[ -d "$1" ] && { [ -d "$1" ] && {
for pdf in "$1"/*.pdf; do for pdf in "$1"/*.pdf; do
doi=$(get_doi_from_pdf "$pdf") doi=$(get_doi_from_pdf "$pdf")
[ -n "$doi" ] && { [ -n "$doi" ] && process_doi "$doi"; done
doi=$(normalize_doi "$doi") exit 0
process_doi "$doi"
}
done
exit 1
} }
[ -f "$1" ] && [ "$(echo "$1" | grep -c "\.pdf$")" -ne 0 ] && { [ -f "$1" ] && [ "$(echo "$1" | grep -c "\.pdf$")" -ne 0 ] && {
doi=$(get_doi_from_pdf "$1") doi=$(get_doi_from_pdf "$1")
[ -n "$doi" ] && { [ -n "$doi" ] && { process_doi "$doi"; exit 0; }
doi=$(normalize_doi "$doi")
process_doi "$doi"
}
exit 1
} }
doi=$(echo "$1" | correction_method) doi=$(echo "$1" | correction_method)
[ -n "$doi" ] && { [ -n "$doi" ] && process_doi "$doi"
doi=$(normalize_doi "$doi")
process_doi "$doi"
}