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.

371 lines
8.6 KiB

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