Debian Codegraph @

download-pubmed

raw symbols SCIP
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/pubmed/${dir}"
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=$( (gunzip -c "$fl" | xtract -mixed -verify) 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=$( (gunzip -c "$fl" | xtract -mixed -verify) 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/pubmed/${dir}"
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=$( (gunzip -c "$fl" | xtract -mixed -verify) 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=$( (gunzip -c "$fl" | xtract -mixed -verify) 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
156 if [ "$useFtp" = true ]
157 then
158 nquire -lst ftp.ncbi.nlm.nih.gov pubmed "$dir" |
159 grep -v ".md5" | grep "xml.gz" |
160 skip-if-file-exists |
161 while read fl
162 do
163 sleep 1
164 echo "$fl"
165 DownloadOneByFTP "$dir" "$fl"
166 done
167 elif [ "$useHttps" = true ]
168 then
169 nquire -get https://ftp.ncbi.nlm.nih.gov pubmed "$dir" |
170 xtract -pattern a -if a -starts-with pubmed -and a -ends-with ".xml.gz" -element a |
171 skip-if-file-exists |
172 while read fl
173 do
174 sleep 1
175 echo "$fl"
176 DownloadOneByHTTPS "$dir" "$fl"
177 done
178 fi
179 }
180
181 if [ "$#" -eq 0 ]
182 then
183 DownloadSection "baseline"
184 if [ $? -ne 0 ]
185 then
186 DownloadSection "baseline"
187 fi
188 DownloadSection "updatefiles"
189 if [ $? -ne 0 ]
190 then
191 DownloadSection "updatefiles"
192 fi
193 fi
194
195 while [ "$#" -gt 0 ]
196 do
197 sect="$1"
198 shift
199 DownloadSection "$sect"
200 if [ $? -ne 0 ]
201 then
202 DownloadSection "$sect"
203 fi
204 done