A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.3 KiB

  1. # Licensed under the MIT license
  2. # http://opensource.org/licenses/mit-license.php
  3. # Copyright 2005, Tim Potter <tpot@samba.org>
  4. #
  5. # This module implements the Content Directory Service (CDS) service
  6. # type as documented in the ContentDirectory:1 Service Template
  7. # Version 1.01
  8. #
  9. #
  10. # TODO: Figure out a nicer pattern for debugging soap server calls as
  11. # twisted swallows the tracebacks. At the moment I'm going:
  12. #
  13. # try:
  14. # ....
  15. # except:
  16. # traceback.print_exc(file = log.logfile)
  17. #
  18. from twisted.python import log
  19. from twisted.web import resource, static
  20. from elementtree.ElementTree import Element, SubElement, tostring
  21. from upnp import UPnPPublisher
  22. from DIDLLite import DIDLElement, Container, Movie, Resource, MusicTrack
  23. import traceback
  24. from urllib import quote
  25. class ContentDirectoryControl(UPnPPublisher, dict):
  26. """This class implements the CDS actions over SOAP."""
  27. def getnextID(self):
  28. ret = str(self.nextID)
  29. self.nextID += 1
  30. return ret
  31. def addContainer(self, parent, title, **kwargs):
  32. ret = self.addItem(parent, Container, title, **kwargs)
  33. self.children[ret] = []
  34. return ret
  35. def addItem(self, parent, klass, *args, **kwargs):
  36. assert isinstance(self[parent], Container)
  37. nid = self.getnextID()
  38. i = klass(nid, parent, *args, **kwargs)
  39. self.children[parent].append(i)
  40. self[i.id] = i
  41. return i.id
  42. def getchildren(self, item):
  43. assert isinstance(self[item], Container)
  44. return self.children[item][:]
  45. def __init__(self, title, *args):
  46. super(ContentDirectoryControl, self).__init__(*args)
  47. fakeparent = '-1'
  48. self.nextID = 0
  49. self.children = { fakeparent: []}
  50. self[fakeparent] = Container(None, '-1', 'fake')
  51. root = self.addContainer(fakeparent, title)
  52. assert root == '0'
  53. del self[fakeparent]
  54. del self.children[fakeparent]
  55. # Required actions
  56. def soap_GetSearchCapabilities(self, *args, **kwargs):
  57. """Required: Return the searching capabilities supported by the device."""
  58. log.msg('GetSearchCapabilities()')
  59. return { 'SearchCapabilitiesResponse': { 'SearchCaps': '' }}
  60. def soap_GetSortCapabilities(self, *args, **kwargs):
  61. """Required: Return the CSV list of meta-data tags that can be used in
  62. sortCriteria."""
  63. log.msg('GetSortCapabilities()')
  64. return { 'SortCapabilitiesResponse': { 'SortCaps': '' }}
  65. def soap_GetSystemUpdateID(self, *args, **kwargs):
  66. """Required: Return the current value of state variable SystemUpdateID."""
  67. log.msg('GetSystemUpdateID()')
  68. return { 'SystemUpdateIdResponse': { 'Id': 5 }}
  69. BrowseFlags = ('BrowseMetaData', 'BrowseDirectChildren')
  70. def soap_Browse(self, *args):
  71. """Required: Incrementally browse the native heirachy of the Content
  72. Directory objects exposed by the Content Directory Service."""
  73. (ObjectID, BrowseFlag, Filter, StartingIndex, RequestedCount,
  74. SortCriteria) = args
  75. StartingIndex = int(StartingIndex)
  76. RequestedCount = int(RequestedCount)
  77. log.msg('Browse(ObjectID=%s, BrowseFlags=%s, Filter=%s, '
  78. 'StartingIndex=%s RequestedCount=%s SortCriteria=%s)' %
  79. (`ObjectID`, `BrowseFlag`, `Filter`, `StartingIndex`,
  80. `RequestedCount`, `SortCriteria`))
  81. didl = DIDLElement()
  82. result = {}
  83. try:
  84. if BrowseFlag == 'BrowseDirectChildren':
  85. ch = self.getchildren(ObjectID)[StartingIndex: StartingIndex + RequestedCount]
  86. log.msg(ch)
  87. log.msg(dict.__repr__(self))
  88. filter(lambda x, s = self, d = didl: d.addItem(s[x.id]) and None, ch)
  89. else:
  90. didl.addItem(self[ObjectID])
  91. result = {'BrowseResponse': {'Result': didl.toString() ,
  92. 'NumberReturned': didl.numItems(),
  93. 'TotalMatches': didl.numItems(),
  94. 'UpdateID': 0}}
  95. except:
  96. traceback.print_exc(file=log.logfile)
  97. log.msg('Returning: %s' % result)
  98. return result
  99. # Optional actions
  100. def soap_Search(self, *args, **kwargs):
  101. """Search for objects that match some search criteria."""
  102. (ContainerID, SearchCriteria, Filter, StartingIndex,
  103. RequestedCount, SortCriteria) = args
  104. log.msg('Search(ContainerID=%s, SearchCriteria=%s, Filter=%s, ' \
  105. 'StartingIndex=%s, RequestedCount=%s, SortCriteria=%s)' %
  106. (`ContainerID`, `SearchCriteria`, `Filter`,
  107. `StartingIndex`, `RequestedCount`, `SortCriteria`))
  108. def soap_CreateObject(self, *args, **kwargs):
  109. """Create a new object."""
  110. (ContainerID, Elements) = args
  111. log.msg('CreateObject(ContainerID=%s, Elements=%s)' %
  112. (`ContainerID`, `Elements`))
  113. def soap_DestroyObject(self, *args, **kwargs):
  114. """Destroy the specified object."""
  115. (ObjectID) = args
  116. log.msg('DestroyObject(ObjectID=%s)' % `ObjectID`)
  117. def soap_UpdateObject(self, *args, **kwargs):
  118. """Modify, delete or insert object metadata."""
  119. (ObjectID, CurrentTagValue, NewTagValue) = args
  120. log.msg('UpdateObject(ObjectID=%s, CurrentTagValue=%s, ' \
  121. 'NewTagValue=%s)' % (`ObjectID`, `CurrentTagValue`,
  122. `NewTagValue`))
  123. def soap_ImportResource(self, *args, **kwargs):
  124. """Transfer a file from a remote source to a local
  125. destination in the Content Directory Service."""
  126. (SourceURI, DestinationURI) = args
  127. log.msg('ImportResource(SourceURI=%s, DestinationURI=%s)' %
  128. (`SourceURI`, `DestinationURI`))
  129. def soap_ExportResource(self, *args, **kwargs):
  130. """Transfer a file from a local source to a remote
  131. destination."""
  132. (SourceURI, DestinationURI) = args
  133. log.msg('ExportResource(SourceURI=%s, DestinationURI=%s)' %
  134. (`SourceURI`, `DestinationURI`))
  135. def soap_StopTransferResource(self, *args, **kwargs):
  136. """Stop a file transfer initiated by ImportResource or
  137. ExportResource."""
  138. (TransferID) = args
  139. log.msg('StopTransferResource(TransferID=%s)' % TransferID)
  140. def soap_GetTransferProgress(self, *args, **kwargs):
  141. """Query the progress of a file transfer initiated by
  142. an ImportResource or ExportResource action."""
  143. (TransferID, TransferStatus, TransferLength, TransferTotal) = args
  144. log.msg('GetTransferProgress(TransferID=%s, TransferStatus=%s, ' \
  145. 'TransferLength=%s, TransferTotal=%s)' %
  146. (`TransferId`, `TransferStatus`, `TransferLength`,
  147. `TransferTotal`))
  148. def soap_DeleteResource(self, *args, **kwargs):
  149. """Delete a specified resource."""
  150. (ResourceURI) = args
  151. log.msg('DeleteResource(ResourceURI=%s)' % `ResourceURI`)
  152. def soap_CreateReference(self, *args, **kwargs):
  153. """Create a reference to an existing object."""
  154. (ContainerID, ObjectID) = args
  155. log.msg('CreateReference(ContainerID=%s, ObjectID=%s)' %
  156. (`ContainerID`, `ObjectID`))
  157. class ContentDirectoryServer(resource.Resource):
  158. def __init__(self, title):
  159. resource.Resource.__init__(self)
  160. self.putChild('scpd.xml', static.File('content-directory-scpd.xml'))
  161. self.control = ContentDirectoryControl(title)
  162. self.putChild('control', self.control)