Debian Codegraph @

download-pmc

raw
1 #!/bin/sh
2
3 # Public domain notice for all NCBI EDirect scripts is located at:
4 # https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 useFtp=true
7 useHttps=false
8
9 while [ $# -gt 0 ]
10 do
11 case "$1" in
12 -ftp )
13 useFtp=true
14 useHttps=false
15 shift
16 ;;
17 -http | -https )
18 useFtp=false
19 useHttps=true
20 shift
21 ;;
22 * )
23 break
24 ;;
25 esac
26 done
27
28 DownloadOneByFTP() {
29
30 dir="$1"
31 fl="$2"
32
33 url="ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml"
34
35 echo "$fl" |
36 nquire -asp "${url}"
37
38 # delete if file is present but empty
39 if [ -f "$fl" ] && [ ! -s "$fl" ]
40 then
41 rm -f "$fl"
42 fi
43
44 # retry if no file
45 if [ ! -f "$fl" ]
46 then
47 sleep 10
48 echo "First Failed Download Retry" >&2
49 echo "$fl" |
50 nquire -asp "${url}"
51 fi
52
53 # retry again if still no file
54 if [ ! -f "$fl" ]
55 then
56 sleep 10
57 echo "Second Failed Download Retry" >&2
58 echo "$fl" |
59 nquire -asp "${url}"
60 fi
61
62 # verify contents
63 if [ -s "$fl" ]
64 then
65 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
66 if [ -n "$errs" ]
67 then
68 # delete and retry one more time
69 rm -f "$fl"
70 sleep 10
71 echo "Invalid Contents Retry" >&2
72 echo "$fl" |
73 nquire -asp "${url}"
74 if [ -s "$fl" ]
75 then
76 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
77 if [ -n "$errs" ]
78 then
79 rm -f "$fl"
80 frst=$( echo "$errs" | head -n 1 )
81 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
82 fi
83 else
84 echo "Download Attempts Failed" >&2
85 fi
86 fi
87 else
88 rm -f "$fl"
89 echo "Download of '$fl' Failed" >&2
90 fi
91 }
92
93 DownloadOneByHTTPS() {
94
95 dir="$1"
96 fl="$2"
97
98 url="https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml"
99
100 nquire -bulk -get "${url}" "$fl" > "$fl"
101
102 # delete if file is present but empty
103 if [ -f "$fl" ] && [ ! -s "$fl" ]
104 then
105 rm -f "$fl"
106 fi
107
108 # retry if no file
109 if [ ! -f "$fl" ]
110 then
111 sleep 10
112 echo "First Failed Download Retry" >&2
113 nquire -bulk -get "${url}" "$fl" > "$fl"
114 fi
115
116 # retry again if still no file
117 if [ ! -f "$fl" ]
118 then
119 sleep 10
120 echo "Second Failed Download Retry" >&2
121 nquire -bulk -get "${url}" "$fl" > "$fl"
122 fi
123
124 # verify contents
125 if [ -s "$fl" ]
126 then
127 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
128 if [ -n "$errs" ]
129 then
130 # delete and retry one more time
131 rm -f "$fl"
132 sleep 10
133 echo "Invalid Contents Retry" >&2
134 nquire -bulk -get "${url}" "$fl" > "$fl"
135 if [ -s "$fl" ]
136 then
137 errs=$( (tar -xOzf "$fl" --to-stdout | xtract -mixed -verify -max 180) 2>&1 )
138 if [ -n "$errs" ]
139 then
140 rm -f "$fl"
141 frst=$( echo "$errs" | head -n 1 )
142 echo "ERROR invalid file '$fl' deleted, errors start with '$frst'" >&2
143 fi
144 fi
145 fi
146 else
147 rm -f "$fl"
148 echo "Download of '$fl' Failed" >&2
149 fi
150 }
151
152 DownloadSection() {
153
154 dir="$1"
155 flt="$2"
156
157 if [ "$useFtp" = true ]
158 then
159 nquire -lst "ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
160 grep ".tar.gz" | grep "$flt" |
161 skip-if-file-exists |
162 while read fl
163 do
164 sleep 1
165 echo "$fl"
166 DownloadOneByFTP "$dir" "$fl"
167 done
168 elif [ "$useHttps" = true ]
169 then
170 nquire -get "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/${dir}/xml" |
171 xtract -pattern a -if a -starts-with "oa_" -and a -ends-with ".tar.gz" -and a -contains "$flt" -element a |
172 skip-if-file-exists |
173 while read fl
174 do
175 sleep 1
176 echo "$fl"
177 DownloadOneByHTTPS "$dir" "$fl"
178 done
179 fi
180 }
181
182 for flt in baseline incr
183 do
184 for dir in oa_comm oa_noncomm oa_other
185 do
186 DownloadSection "$dir" "$flt"
187 if [ $? -ne 0 ]
188 then
189 DownloadSection "$dir" "$flt"
190 fi
191 done
192 done