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.

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