Mercurial > hg > orthanc
comparison Resources/Samples/Python/ArchiveStudiesInTimeRange.py @ 1348:cff17da28916
ArchiveStudiesInTimeRange.py sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 22 Apr 2015 15:12:04 +0200 |
parents | |
children | b1291df2f780 |
comparison
equal
deleted
inserted
replaced
1347:60cc0ee61edb | 1348:cff17da28916 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Orthanc - A Lightweight, RESTful DICOM Store | |
5 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
6 # Department, University Hospital of Liege, Belgium | |
7 # | |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 | |
22 | |
23 import os | |
24 import os.path | |
25 import sys | |
26 import RestToolbox | |
27 | |
28 def PrintHelp(): | |
29 print('Download ZIP archives for all the studies generated ' | |
30 'during a given time range (according to the StudyDate tag)\n') | |
31 print('Usage: %s <URL> <StartDate> <EndDate> <TargetFolder>\n' % sys.argv[0]) | |
32 print('Example: %s http://localhost:8042/ 20150101 20151231 /tmp/\n' % sys.argv[0]) | |
33 exit(-1) | |
34 | |
35 def CheckIsDate(date): | |
36 if len(date) != 8 or not date.isdigit(): | |
37 print '"%s" is not a valid date!\n' % date | |
38 exit(-1) | |
39 | |
40 | |
41 if len(sys.argv) != 5: | |
42 PrintHelp() | |
43 | |
44 URL = sys.argv[1] | |
45 START = sys.argv[2] | |
46 END = sys.argv[3] | |
47 TARGET = sys.argv[4] | |
48 | |
49 CheckIsDate(START) | |
50 CheckIsDate(END) | |
51 | |
52 # Loop over the studies | |
53 for studyId in RestToolbox.DoGet('%s/studies' % URL): | |
54 # Retrieve the DICOM tags of the current study | |
55 study = RestToolbox.DoGet('%s/studies/%s' % (URL, studyId))['MainDicomTags'] | |
56 | |
57 # Retrieve the DICOM tags of the parent patient of this study | |
58 patient = RestToolbox.DoGet('%s/studies/%s/patient' % (URL, studyId))['MainDicomTags'] | |
59 | |
60 # Check that the StudyDate tag lies within the given range | |
61 studyDate = study['StudyDate'][:8] | |
62 if studyDate >= START and studyDate <= END: | |
63 # Create a filename | |
64 filename = '%s - %s %s - %s.zip' % (study['StudyDate'], | |
65 patient['PatientID'], | |
66 patient['PatientName'], | |
67 study['StudyDescription']) | |
68 | |
69 # Remove any non-ASCII character in the filename | |
70 filename = filename.encode('ascii', errors = 'replace') | |
71 | |
72 # Download the ZIP archive of the study | |
73 print('Downloading %s' % filename) | |
74 zipContent = RestToolbox.DoGet('%s/studies/%s/archive' % (URL, studyId)) | |
75 | |
76 # Write the ZIP archive at the proper location | |
77 with open(os.path.join(TARGET, filename), 'wb') as f: | |
78 f.write(zipContent) |