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.

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