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.

350 lines
9.2 KiB

  1. # Licensed under the MIT license
  2. # http://opensource.org/licenses/mit-license.php
  3. # Copyright 2005, Tim Potter <tpot@samba.org>
  4. from elementtree.ElementTree import Element, SubElement, tostring, _ElementInterface
  5. class Resource:
  6. """An object representing a resource."""
  7. def __init__(self, data, protocolInfo):
  8. self.data = data
  9. self.protocolInfo = protocolInfo
  10. self.bitrate = None
  11. self.size = None
  12. def toElement(self):
  13. root = Element('res')
  14. root.attrib['protocolInfo'] = self.protocolInfo
  15. root.text = self.data
  16. if self.bitrate is not None:
  17. root.attrib['bitrate'] = str(self.bitrate)
  18. if self.size is not None:
  19. root.attrib['size'] = str(self.size)
  20. return root
  21. class Object:
  22. """The root class of the entire content directory class heirachy."""
  23. klass = 'object'
  24. creator = None
  25. res = None
  26. writeStatus = None
  27. def __init__(self, id, parentID, title, restricted = False,
  28. creator = None):
  29. self.id = id
  30. self.parentID = parentID
  31. self.title = title
  32. self.creator = creator
  33. if restricted:
  34. self.restricted = '1'
  35. else:
  36. self.restricted = '0'
  37. def toElement(self):
  38. root = Element(self.elementName)
  39. root.attrib['id'] = self.id
  40. root.attrib['parentID'] = self.parentID
  41. SubElement(root, 'dc:title').text = self.title
  42. SubElement(root, 'upnp:class').text = self.klass
  43. root.attrib['restricted'] = self.restricted
  44. if self.creator is not None:
  45. SubElement(root, 'dc:creator').text = self.creator
  46. if self.res is not None:
  47. root.append(self.res.toElement())
  48. if self.writeStatus is not None:
  49. SubElement(root, 'upnp:writeStatus').text = self.writeStatus
  50. return root
  51. def toString(self):
  52. return tostring(self.toElement())
  53. class Item(Object):
  54. """A class used to represent atomic (non-container) content
  55. objects."""
  56. klass = Object.klass + '.item'
  57. elementName = 'item'
  58. refID = None
  59. def toElement(self):
  60. root = Object.toElement(self)
  61. if self.refID is not None:
  62. SubElement(root, 'refID').text = self.refID
  63. return root
  64. class ImageItem(Item):
  65. klass = Item.klass + '.imageItem'
  66. class Photo(ImageItem):
  67. klass = ImageItem.klass + '.photo'
  68. class AudioItem(Item):
  69. """A piece of content that when rendered generates some audio."""
  70. klass = Item.klass + '.audioItem'
  71. genre = None
  72. description = None
  73. longDescription = None
  74. publisher = None
  75. language = None
  76. relation = None
  77. rights = None
  78. def toElement(self):
  79. root = Item.toElement(self)
  80. if self.genre is not None:
  81. SubElement(root, 'upnp:genre').text = self.genre
  82. if self.description is not None:
  83. SubElement(root, 'dc:description').text = self.description
  84. if self.longDescription is not None:
  85. SubElement(root, 'upnp:longDescription').text = \
  86. self.longDescription
  87. if self.publisher is not None:
  88. SubElement(root, 'dc:publisher').text = self.publisher
  89. if self.language is not None:
  90. SubElement(root, 'dc:language').text = self.language
  91. if self.relation is not None:
  92. SubElement(root, 'dc:relation').text = self.relation
  93. if self.rights is not None:
  94. SubElement(root, 'dc:rights').text = self.rights
  95. return root
  96. class MusicTrack(AudioItem):
  97. """A discrete piece of audio that should be interpreted as music."""
  98. klass = AudioItem.klass + '.musicTrack'
  99. artist = None
  100. album = None
  101. originalTrackNumber = None
  102. playlist = None
  103. storageMedium = None
  104. contributor = None
  105. date = None
  106. def toElement(self):
  107. root = AudioItem.toElement(self)
  108. if self.artist is not None:
  109. SubElement(root, 'upnp:artist').text = self.artist
  110. if self.album is not None:
  111. SubElement(root, 'upnp:album').text = self.album
  112. if self.originalTrackNumber is not None:
  113. SubElement(root, 'upnp:originalTrackNumber').text = \
  114. self.originalTrackNumber
  115. if self.playlist is not None:
  116. SubElement(root, 'upnp:playlist').text = self.playlist
  117. if self.storageMedium is not None:
  118. SubElement(root, 'upnp:storageMedium').text = self.storageMedium
  119. if self.contributor is not None:
  120. SubElement(root, 'dc:contributor').text = self.contributor
  121. if self.date is not None:
  122. SubElement(root, 'dc:date').text = self.date
  123. return root
  124. class AudioBroadcast(AudioItem):
  125. klass = AudioItem.klass + '.audioBroadcast'
  126. class AudioBook(AudioItem):
  127. klass = AudioItem.klass + '.audioBook'
  128. class VideoItem(Item):
  129. klass = Item.klass + '.videoItem'
  130. class Movie(VideoItem):
  131. klass = VideoItem.klass + '.movie'
  132. class VideoBroadcast(VideoItem):
  133. klass = VideoItem.klass + '.videoBroadcast'
  134. class MusicVideoClip(VideoItem):
  135. klass = VideoItem.klass + '.musicVideoClip'
  136. class PlaylistItem(Item):
  137. klass = Item.klass + '.playlistItem'
  138. class TextItem(Item):
  139. klass = Item.klass + '.textItem'
  140. class Container(Object):
  141. """An object that can contain other objects."""
  142. klass = Object.klass + '.container'
  143. elementName = 'container'
  144. childCount = 0
  145. createClass = None
  146. searchClass = None
  147. searchable = None
  148. def __init__(self, id, parentID, title, restricted = 0, creator = None):
  149. Object.__init__(self, id, parentID, title, restricted, creator)
  150. def toElement(self):
  151. root = Object.toElement(self)
  152. root.attrib['childCount'] = str(self.childCount)
  153. if self.createClass is not None:
  154. SubElement(root, 'upnp:createclass').text = self.createClass
  155. if self.searchClass is not None:
  156. if not isinstance(self.searchClass, (list, tuple)):
  157. self.searchClass = ['searchClass']
  158. for i in searchClass:
  159. SubElement(root, 'upnp:searchclass').text = i
  160. if self.searchable is not None:
  161. root.attrib['searchable'] = str(self.searchable)
  162. return root
  163. class Person(Container):
  164. klass = Container.klass + '.person'
  165. class MusicArtist(Person):
  166. klass = Person.klass + '.musicArtist'
  167. class PlaylistContainer(Container):
  168. klass = Container.klass + '.playlistContainer'
  169. class Album(Container):
  170. klass = Container.klass + '.album'
  171. class MusicAlbum(Album):
  172. klass = Album.klass + '.musicAlbum'
  173. class PhotoAlbum(Album):
  174. klass = Album.klass + '.photoAlbum'
  175. class Genre(Container):
  176. klass = Container.klass + '.genre'
  177. class MusicGenre(Genre):
  178. klass = Genre.klass + '.musicGenre'
  179. class MovieGenre(Genre):
  180. klass = Genre.klass + '.movieGenre'
  181. class StorageSystem(Container):
  182. klass = Container.klass + '.storageSystem'
  183. total = -1
  184. used = -1
  185. free = -1
  186. maxpartition = -1
  187. medium = 'UNKNOWN'
  188. def toElement(self):
  189. root = Container.toElement(self)
  190. SubElement(root, 'upnp:storageTotal').text = str(self.total)
  191. SubElement(root, 'upnp:storageUsed').text = str(self.used)
  192. SubElement(root, 'upnp:storageFree').text = str(self.free)
  193. SubElement(root, 'upnp:storageMaxPartition').text = str(self.maxpartition)
  194. SubElement(root, 'upnp:storageMedium').text = self.medium
  195. return root
  196. class StorageVolume(Container):
  197. klass = Container.klass + '.storageVolume'
  198. total = -1
  199. used = -1
  200. free = -1
  201. medium = 'UNKNOWN'
  202. def toElement(self):
  203. root = Container.toElement(self)
  204. SubElement(root, 'upnp:storageTotal').text = str(self.total)
  205. SubElement(root, 'upnp:storageUsed').text = str(self.used)
  206. SubElement(root, 'upnp:storageFree').text = str(self.free)
  207. SubElement(root, 'upnp:storageMedium').text = self.medium
  208. return root
  209. class StorageFolder(Container):
  210. klass = Container.klass + '.storageFolder'
  211. used = -1
  212. def toElement(self):
  213. root = Container.toElement(self)
  214. if self.used is not None:
  215. SubElement(root, 'upnp:storageUsed').text = str(self.used)
  216. return root
  217. class DIDLElement(_ElementInterface):
  218. def __init__(self):
  219. _ElementInterface.__init__(self, 'DIDL-Lite', {})
  220. self.attrib['xmlns'] = 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite'
  221. self.attrib['xmlns:dc'] = 'http://purl.org/dc/elements/1.1/'
  222. self.attrib['xmlns:upnp'] = 'urn:schemas-upnp-org:metadata-1-0/upnp'
  223. def addContainer(self, id, parentID, title, restricted = False):
  224. e = Container(id, parentID, title, restricted, creator = '')
  225. self.append(e.toElement())
  226. def addItem(self, item):
  227. self.append(item.toElement())
  228. def numItems(self):
  229. return len(self)
  230. def toString(self):
  231. return tostring(self)
  232. if __name__ == '__main__':
  233. root = DIDLElement()
  234. root.addContainer('0\Movie\\', '0\\', 'Movie')
  235. root.addContainer('0\Music\\', '0\\', 'Music')
  236. root.addContainer('0\Photo\\', '0\\', 'Photo')
  237. root.addContainer('0\OnlineMedia\\', '0\\', 'OnlineMedia')
  238. print tostring(root)