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.

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