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