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.

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