orthancDatabaseBrowser.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import orthancInterface
  2. import json
  3. import chardet
  4. def extractJSON(data):
  5. encoding=chardet.detect(data)["encoding"]
  6. return json.loads(data.decode(encoding))
  7. class orthancDB:
  8. def __init__(self,net):
  9. self.net=net
  10. def getList(self,category):
  11. url=self.net.getCoreURL()
  12. url+='/'+category
  13. response=self.net.get(url)
  14. return extractJSON(response.data)
  15. def getPatients(self):
  16. return self.getList('patients')
  17. def getStudies(self):
  18. return self.getList('studies')
  19. def getPatientData(self, orthancId):
  20. return self.getData('patients',orthancId)
  21. def getStudyData(self, studyOrthancId):
  22. return self.getData('studies',studyOrthancId)
  23. def getSeriesData(self, seriesOrthancId):
  24. return self.getData('series',seriesOrthancId)
  25. def getData(self, level, orthancId):
  26. url=self.net.getCoreURL()
  27. url+='/'+level+'/'+orthancId
  28. response=self.net.get(url)
  29. return extractJSON(response.data)
  30. def getDicomField(self,orthancInstanceId,dicomField):
  31. url=self.net.getCoreURL()
  32. url+='/instances/'+orthancInstanceId+'/content/'+dicomField
  33. response=self.net.get(url)
  34. try:
  35. encoding=chardet.detect(response.data)["encoding"]
  36. return response.data.decode(encoding)
  37. except TypeError:
  38. return ''
  39. def selectRows(self, qfilter):
  40. obj={}
  41. obj["Level"]="Instance"
  42. obj["Query"]=qfilter
  43. jsonData=json.dumps(obj)
  44. url=self.net.getCoreURL()
  45. url+='/tools/find'
  46. response=self.net.post(url,'json',jsonData)
  47. encoding=chardet.detect(response.data)["encoding"]
  48. return json.loads(response.data.decode(encoding))
  49. def upload(self, f) :
  50. url=self.net.getCoreURL()
  51. url+='/instances'
  52. fobj=open (f, 'rb')
  53. response=self.net.post(url,'octet-stream',fobj.read())
  54. try:
  55. encoding=chardet.detect(response.data)["encoding"]
  56. except AttributeError:
  57. print(response)
  58. return json.loads('{"status":"BAD"}')
  59. return json.loads(response.data.decode(encoding))
  60. def remove(self,level,orthancId):
  61. url=self.net.getCoreURL()
  62. url+='/'+level+'/'+orthancId
  63. response=self.net.delete(url)
  64. print(response.data)