-
Notifications
You must be signed in to change notification settings - Fork 0
/
getref
executable file
·393 lines (361 loc) · 12.7 KB
/
getref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
# Usage: getref BIB_ENTRY_ID|REFERENCE_TITLE [-b|--bib BIB_FILE_NAME or BIB_ENTRY_ID] [-f|--from REFERENCE_FILE] [-l|--livre]
# getref tries downloading a reference according to the given bib entry id or title and name it as
# $BIB_ENTRY_ID-$REFERENCE_TITLE or $REFERENCE_TITLE according to the availability of $BIB_ENTRY_ID.
# According to $BIB_FILE_NAME, we decide $REFERENCE_TITLE by $BIB_ENTRY_ID or vice versa.
BIBENTRYID=""
REFTITLE=""
BIBFILE=""
INPUTREF=""
BIBTYPE=""
SUCCESS=1
TIME=$(date +%Y.%m.%d-%H.%M.%S)
# The directory where this scrip resides
TDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Some configuration, which should define REFDIR and DEFAULT_BIBFILE
# shellcheck source=./tconfig
source "$TDIR/tconfig"
# Directory to save the references
if ! [[ -d "$REFDIR" ]]; then
REFDIR="$PWD"
fi
# Default bib file
if ! [[ -f "$DEFAULT_BIBFILE" ]]; then
DEFAULT_BIBFILE=""
fi
# Command to read the reference
READER="evince"
# Libgen
LIBGEN="http://libgen.rs/search.php?req="
# Libgen for articles
LIBGENART="http://libgen.rs/scimag/?q="
# Temporary html file
TMPDIR="$(mktemp -d)"
TMPHTML="$TMPDIR/$TIME-getref.html"
########################################### FUNCTIONS ##############################################
clean_title () {
if [[ $# -ne 1 ]]; then
printf "\n Error: $0 needs one argument.\n"
exit 1
fi
echo "$1" | sed 's/{//g' | sed 's/}//g' | sed 's/(//g' | sed 's/)//g' | sed 's/\[//g' | sed 's/\]//g' \
| sed "s/'//" | sed 's/"//g' | sed 's/`//g' | sed 's/^//g' | sed 's/~//g' \
| sed 's/\\\\L/L/g' | sed 's/\\\\l/l/g' | sed 's/\\//g' | sed 's/^\s*//' | sed 's/\s*$//'
}
underscore_title () {
if [[ $# -ne 1 ]]; then
printf "\n Error: $0 needs one argument.\n"
exit 1
fi
echo "$1" | sed -r 's/[^a-zA-Z0-9]+/_/g' | sed 's/_$//' | sed 's/^_//'
}
fix_url() {
if [[ $# -ne 1 ]]; then
printf "\n Error: $0 needs one argument.\n"
exit 1
fi
if echo $1 | grep -q '^\/\/' ; then
echo "http:$1"
else
echo "$1"
fi
}
####################################################################################################
# Parse the arguments
if [[ $# -gt 6 ]]; then
printf "Usage: getref BIB_ENTRY_ID|REFERENCE_TITLE [-b|--bib BIB_FILE_NAME] [-f|--from REFERENCE_FILE] [-l|--livre]\n"
exit 1
fi
while [[ -n "$1" ]]; do
case "$1" in
-b|--bib)
if [[ $# -gt 1 ]]; then
BIBFILE="$2"
shift
fi
;;
-f|--from)
if [[ $# -gt 1 ]]; then
INPUTREF="$2"
shift
fi
;;
-l|--livre)
BIBTYPE="book"
;;
*.bib)
if [[ -z $BIBFILE ]]; then
$BIBFILE="$1"
fi
;;
*.pdf|*.djvu|*.ps|*.dvi|http://*|https://*|ftp://*)
if [[ -z $INPUTREF ]]; then
INPUTREF="$1"
fi
;;
*)
BIBENTRYID="$1"
;;
esac
shift
done
if ! [[ -z "$BIBFILE" ]] && ! echo "$BIBFILE" | grep -q '\.bib$' ; then
# If $BIBFILE is nonempty but it does not have the .bib extension, then it should be BIBENTRYID.
REFTITLE="$BIBENTRYID"
BIBENTRYID="$BIBFILE"
BIBFILE=""
elif echo "$BIBENTRYID" | grep -q '[^a-zA-Z0-9_]' || ! echo "$BIBENTRYID" | grep -q '[a-z]' \
|| ! echo "$BIBENTRYID" | grep -q '[A-Z]' || ! echo "$BIBENTRYID" | grep -q '[0-9]'; then
# If BIBENTRYID satisfies the above criteria, then it is indeed $REFTITLE.
REFTITLE="$BIBENTRYID"
BIBENTRYID=""
fi
if ! [[ -z "$REFTITLE" ]]; then
REFTITLE=$(clean_title "$REFTITLE")
if [[ $? != 0 ]]; then
exit $?
fi
UNDERSCORED_REFTITLE=$(underscore_title "$REFTITLE")
fi
# Decide the bib files to check
if ! [[ -z "$BIBFILE" ]]; then
BIBFILES="$BIBFILE"
else
BIBFILES="$DEFAULT_BIBFILE"
fi
#BIBFILES=$(ls -t *.bib 2>/dev/null)
if [[ -z "$BIBFILES" ]]; then
printf "\nNo bib file found.\n"
fi
# Decide $REFTITLE by $BIBENTRYID or vice versa.
LINEBREAKER="LINEBREAKERSTARTING$(date +%s)LINEBREAKERENDING"
if ! [[ -z "$BIBENTRYID" ]]; then
REFTITLE_IN_BIBFILE=""
for FILE in $BIBFILES; do
REFTITLE_IN_BIBFILE=$(sed '/^\s*%/d' "$FILE" \
| sed 's/%.*$//' \
| awk -v ORS="$LINEBREAKER" 1 \
| sed "s/$LINEBREAKER\s*@/$LINEBREAKER\n@/g" \
| grep "^@.*{\s*$BIBENTRYID\s*," | head -1 \
| sed "s/^.*$LINEBREAKER\s*title\s*=\s*{//" \
| sed -r "s/},\s*$LINEBREAKER.*}\s*[$LINEBREAKER]+\s*$//" \
| sed 's/\$//g' \
| sed 's/\\//g' \
| sed 's/{//g' \
| sed 's/}//g' \
| sed 's/^\s*//' \
| sed 's/\s*$//' \
)
if ! [[ -z "$REFTITLE_IN_BIBFILE" ]] && [[ -z "$BIBTYPE" ]]; then
BIBTYPE=$(sed '/^\s*%/d' "$FILE" \
| sed 's/%.*$//' \
| awk -v ORS="$LINEBREAKER" 1 \
| sed "s/$LINEBREAKER\s*@/$LINEBREAKER\n@/g" \
| grep "$BIBENTRYID" \
| sed "s/^.*@//" \
| sed "s/{.*$//" \
| sed 's/^\s*//' \
| sed 's/\s*$//' \
)
break
fi
done
if [[ -z "$REFTITLE_IN_BIBFILE" ]] && ! [[ -z "$BIBFILES" ]]; then
printf "\nThe bib entry id does not exist in the bib files(s).\n\n"
else
REFTITLE_IN_BIBFILE=$(clean_title "$REFTITLE_IN_BIBFILE")
UNDERSCORED_REFTITLE_IN_BIBFILE=$(underscore_title "$REFTITLE_IN_BIBFILE")
fi
if ! [[ -z "$REFTITLE_IN_BIBFILE" ]] && ! [[ -z "$REFTITLE" ]]; then
if [[ $(echo "$UNDERSCORED_REFTITLE_IN_BIBFILE" | tr '[:upper:]' '[:lower:]') != $(echo "$UNDERSCORED_REFTITLE" | tr '[:upper:]' '[:lower:]') ]]; then
read -n1 -s -r -p \
$"\nThe reference title and bib entry id contradicts each other:\
\nIn "$FILE", the title of "$BIBENTRYID" is:\
\n"$REFTITLE_IN_BIBFILE"\
\nContinue to get the reference? [Y/n]\
\nIf yes, the title in the bib file will be used.
" KEY
if [[ "$KEY" == "Y" || "$KEY" == "y" || "$KEY" == "" ]]; then
REFTITLE="$REFTITLE_IN_BIBFILE"
UNDERSCORED_REFTITLE="$UNDERSCORED_REFTITLE_IN_BIBFILE"
else
exit 1
fi
else
REFTITLE="$REFTITLE_IN_BIBFILE"
UNDERSCORED_REFTITLE="$UNDERSCORED_REFTITLE_IN_BIBFILE"
fi
elif ! [[ -z "$REFTITLE_IN_BIBFILE" ]] && [[ -z "$REFTITLE" ]]; then
REFTITLE="$REFTITLE_IN_BIBFILE"
UNDERSCORED_REFTITLE="$UNDERSCORED_REFTITLE_IN_BIBFILE"
elif [[ -z "$REFTITLE_IN_BIBFILE" ]] && [[ -z "$REFTITLE" ]]; then
printf "\nThe title of the reference cannot be decided.\n\n"
exit 2
fi
elif ! [[ -z "$REFTITLE" ]]; then
for FILE in $BIBFILES; do
CLEANED_FILE=$(sed '/^\s*%/d' "$FILE" \
| sed 's/%.*$//' \
| awk -v ORS="$LINEBREAKER" 1 \
| sed "s/$LINEBREAKER\s*@/$LINEBREAKER\n@/g" \
| sed -r "s/^@article|^@book|^@booklet|^@.*report|^@.*thesis|^@incollection\
|^@inproceedings|^@inbook|^@proceedings|^@misc|^@unpublished/@@/I" \
| sed "s/{\s*/{/g" \
| sed "s/\s*}/}/g" \
| sed "s/{//g" \
| sed "s/}//g" \
| sed "s/\s*=\s*/=/g" \
)
CLEANED_FILE=$(clean_title "$CLEANED_FILE")
CLEANED_FILE=$(echo "$CLEANED_FILE" | sed -r 's/[^a-zA-Z0-9@=]+/_/g')
BIBENTRYID=$(echo "$CLEANED_FILE" \
| grep -i "_title=${UNDERSCORED_REFTITLE}_$LINEBREAKER\|_title=$UNDERSCORED_REFTITLE$LINEBREAKER" | head -1 \
| sed "s/^.*@@\s*//" \
| sed "s/_$LINEBREAKER.*$//" \
| sed 's/^\s*//' \
| sed 's/\s*$//' \
)
if ! [[ -z "$BIBENTRYID" ]] && [[ -z "$BIBTYPE" ]]; then
BIBTYPE=$(sed '/^\s*%/d' "$FILE" \
| sed 's/%.*$//' \
| awk -v ORS="$LINEBREAKER" 1 \
| sed "s/$LINEBREAKER\s*@/$LINEBREAKER\n@/g" \
| grep "$BIBENTRYID" \
| sed "s/^.*@//" \
| sed "s/{.*$//" \
| sed 's/^\s*//' \
| sed 's/\s*$//' \
)
break
fi
done
if [[ -z "$BIBENTRYID" ]] && ! [[ -z "$BIBFILES" ]]; then
printf "\nThe title does not exist in the bib files(s).\n"
fi
else
printf "\nNo bib entry id or reference title received.\n\n"
exit 3
fi
if ! [[ -z "$BIBENTRYID" ]]; then
EXISTINGREF=$(ls "$REFDIR"/"$BIBENTRYID-"* 2>/dev/null | grep -i 'pdf\|djvu\|ps\|dvi' | head -1)
if ! [[ -z "$EXISTINGREF" ]]; then
printf "\nThe reference exists:\n"
printf "$EXISTINGREF\n\n"
$READER "$EXISTINGREF"
exit 0
fi
REFFILE="$BIBENTRYID"-"$UNDERSCORED_REFTITLE"
else
REFFILE="$UNDERSCORED_REFTITLE"
REFDIR="$PWD"
fi
if ! [[ -z "$INPUTREF" ]]; then
if [[ -z "$BIBENTRYID" ]] || [[ -z "$REFTITLE" ]]; then
printf "\nNo bib entry id received or the reference does not exist in the bib file(s).\n\n"
exit 3
fi
if echo "$INPUTREF" | grep -q "^http://\|^https://\|^ftp://" ; then
TMPREF="$TIME-ref"
wget "$INPUTREF" -O "$TMPREF"
INPUTREF=$TMPREF
fi
INPUTREF=$(readlink -f "$INPUTREF")
FILETYPE=$(file "$INPUTREF" | tr '[:upper:]' '[:lower:]')
if echo "$FILETYPE" | grep -q "pdf"; then
FILETYPE="pdf"
elif echo "$FILETYPE" | grep -q "djvu"; then
FILETYPE="djvu"
elif echo "$FILETYPE" | grep -q "postscript"; then
FILETYPE="ps"
elif echo "$FILETYPE" | grep -q "dvi"; then
FILETYPE="dvi"
else
printf "\nUnkown file type:\n"
printf "$(file "$INPUTREF")\n\n"
FILETYPE="unknown"
fi
mv "$INPUTREF" "$REFDIR"/"$REFFILE"."$FILETYPE"
REFFILE="$REFDIR"/"$REFFILE"."$FILETYPE"
$READER "$REFFILE"
exit 0
fi
BIBTYPE=$(echo "$BIBTYPE" | tr '[:upper:]' '[:lower:]')
if [[ "$BIBTYPE" == "book" ]] || [[ "$BIBTYPE" == "booklet" ]] || [[ "$BIBTYPE" == "proceedings" ]] \
|| [[ "$BIBTYPE" = "masterthesis" ]] || [[ "$BIBTYPE" = "phdthesis" ]]; then
TMPURL="$LIBGEN$REFTITLE"
echo "Checking $TMPURL ..."
wget -q "$TMPURL" -O "$TMPHTML"
TMPURL=$(cat "$TMPHTML" | grep " title='Gen.lib.rus.ec'" | head -1 \
| sed "s/ title='Gen.lib.rus.ec'.*$//" | sed "s/^.*href=//" | sed 's/"//g' | sed "s/'//g")
if [[ -z "$TMPURL" ]]; then
SUCCESS=0
else
TMPURL=$(fix_url "$TMPURL")
echo "Checking "$TMPURL" ..."
wget -q "$TMPURL" -O "$TMPHTML"
TMPURL=$(cat "$TMPHTML" | grep ">GET<" | head -1 \
| sed "s/>GET<.*$//" | sed "s/^.*href=//" | sed 's/"//g' | sed "s/'//g")
if [[ -z "$TMPURL" ]]; then
SUCCESS=0
else
TMPURL=$(fix_url "$TMPURL")
echo "Checking "$TMPURL" ..."
wget "$TMPURL" -O "$REFFILE"
fi
fi
else
TMPURL="$LIBGENART$REFTITLE"
echo "Checking $TMPURL ..."
wget -q "$TMPURL" -O "$TMPHTML"
TMPURL=$(cat "$TMPHTML" | grep ">Sci-Hub<" | head -1 \
| sed 's/>Sci-Hub<.*$//' | sed 's/^.*href=//' | sed 's/"//g' | sed "s/'//g")
if [[ -z "$TMPURL" ]]; then
SUCCESS=0
else
TMPURL=$(fix_url "$TMPURL")
echo "Checking $TMPURL ..."
wget -q "$TMPURL" -O "$TMPHTML"
TMPURL=$(cat "$TMPHTML" | grep "save<" | head -1 \
| sed "s/^.*href=//" | sed "s/\".*$//" | sed 's/"//g' | sed "s/'//g")
if [[ -z "$TMPURL" ]]; then
SUCCESS=0
else
TMPURL=$(fix_url "$TMPURL")
echo "Checking "$TMPURL" ..."
wget "$TMPURL" -O "$REFFILE"
fi
fi
fi
rm "$TMPHTML"
if [[ $SUCCESS -eq 0 ]]; then
printf "Failed to get \n\n $REFTITLE\n\n"
printf "Download it manually and name it to\n\n $REFFILE\n\n"
exit 4
fi
FILETYPE=$(file "$REFFILE" | tr '[:upper:]' '[:lower:]')
if echo "$FILETYPE" | grep -q "pdf"; then
FILETYPE="pdf"
elif echo "$FILETYPE" | grep -q "djvu"; then
FILETYPE="djvu"
elif echo "$FILETYPE" | grep -q "postscript"; then
FILETYPE="ps"
elif echo "$FILETYPE" | grep -q "dvi"; then
FILETYPE="dvi"
elif echo "$FILETYPE" | grep -q "empty"; then
printf "Failed to get \n\n $REFTITLE\n\n"
printf "Download it manually and name it to\n\n $REFFILE\n\n"
exit 4
else
printf "\nUnkown file type:\n"
printf "$(file "$REFFILE")\n\n"
FILETYPE="unknown"
fi
if [[ "$FILETYPE" == "unknown" ]]; then
printf "The reference has an unknown file type. Check it manually and then rename\n\n"
printf "$REFFILE\n\n"
else
mv "$REFFILE" "$REFDIR"/"$REFFILE"."$FILETYPE"
REFFILE="$REFDIR"/"$REFFILE"."$FILETYPE"
fi
$READER "$REFFILE"
exit 0