annotate Resources/Samples/Python/DownloadAnonymized.py @ 2517:d3d306bc4b99

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Mar 2018 14:35:48 +0200
parents 878b59270859
children 4e43e67f8ecf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
353
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3
747
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
4 # Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1184
diff changeset
6 # Department, University Hospital of Liege, Belgium
2447
878b59270859 upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
7 # Copyright (C) 2017-2018 Osimis S.A., Belgium
747
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
8 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
9 # This program is free software: you can redistribute it and/or
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
10 # modify it under the terms of the GNU General Public License as
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
11 # published by the Free Software Foundation, either version 3 of the
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
12 # License, or (at your option) any later version.
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
13 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
14 # This program is distributed in the hope that it will be useful, but
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
17 # General Public License for more details.
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
18 #
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
19 # You should have received a copy of the GNU General Public License
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
21
44382c8bcd15 added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 353
diff changeset
22
353
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
2032
65b1ce7cb84f Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
24 URL = 'http://127.0.0.1:8042'
353
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 # This sample code will download a ZIP file for each patient that has
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 # been anonymized in Orthanc.
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 import os
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 import os.path
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 import sys
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 import RestToolbox
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 # Loop over the patients
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 for patient in RestToolbox.DoGet('%s/patients' % URL):
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 # Ignore patients whose name starts with "Anonymized", as it is
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 # the result of a previous anonymization
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 infos = RestToolbox.DoGet('%s/patients/%s' % (URL, patient))
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 name = infos['MainDicomTags']['PatientName'].lower()
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if name.startswith('anonymized'):
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 # Trigger the download
1184
4e9d517503ae port to Python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 747
diff changeset
46 print('Downloading %s' % name)
353
61e4c62b8c35 samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 zipContent = RestToolbox.DoGet('%s/patients/%s/archive' % (URL, patient))
2070
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
48 with open(os.path.join('/tmp', name + '.zip'), 'wb') as f:
7e6afa0beaf6 samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
49 f.write(zipContent)