Package elisa :: Package plugins :: Package ugly :: Package youtube :: Module youtube_media
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.ugly.youtube.youtube_media

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  __maintainer__ = 'Philippe Normand <philippe@fluendo.com>' 
 18   
 19  from elisa.base_components.media_provider import MediaProvider, UriNotMonitorable 
 20  from elisa.base_components.media_provider import NotifyEvent 
 21  from elisa.core.component import InitializeFailure 
 22  from elisa.core.media_uri import MediaUri 
 23  from elisa.core import common 
 24  from elisa.core.bus import bus_message 
 25  from elisa.core.observers.dict import DictObservable 
 26  from twisted.internet import defer, threads 
 27   
 28  from elisa.extern.translation import gettexter, N_ 
 29  T_ = gettexter('elisa-youtube') 
 30   
 31   
 32  from youtube_client import YouTubeClient 
 33   
 34  import os, gst 
 35   
36 -class YoutubeMedia(MediaProvider):
37 """ 38 This class implements local filesystem support 39 """ 40 default_config = {'user': ''} 41 42 config_doc = {'user': 'Your Youtube username, optional' 43 } 44
45 - def initialize(self):
46 #FIXME : works only on Linux 47 registry = gst.registry_get_default() 48 registry.scan_path(self.plugin.directory) 49 try: 50 gst.element_factory_make('flvdemux') 51 except gst.PluginNotFoundError: 52 msg = "Can't initialize flvdemux CVS version of gst-plugins-bad" 53 raise InitializeFailure(self.name, msg) 54 55 self._client = YouTubeClient() 56 57 uri = "youtube:///" 58 action_type = bus_message.MediaLocation.ActionType.LOCATION_ADDED 59 msg = bus_message.InternetLocation(action_type, "YouTube", 'youtube', uri, 60 media_types=['video',], 61 theme_icon='youtube') 62 common.application.bus.send_message(msg) 63 self.debug("Initialize done")
64
66 return {}
67
69 return { 'youtube': 0 }
70
71 - def get_media_type(self, uri):
72 dfr = defer.Deferred() 73 if self._is_directory(uri): 74 dfr.callback({'file_type': 'directory', 'mime_type': ''}) 75 else: 76 dfr.callback({'file_type': 'video', 'mime_type': 'video/x-flv'}) 77 return dfr
78
79 - def is_directory(self, uri):
80 d = defer.Deferred() 81 d.callback(self._is_directory(uri)) 82 return d
83
84 - def _is_directory(self, uri):
85 return uri.scheme == 'youtube' and uri.host != 'video'
86
87 - def get_real_uri(self, uri):
88 real_uri = None 89 video_id = uri.path[1:] 90 video_url = "http://www.youtube.com/watch?v=%s" % video_id 91 real = self._client.get_flv_video_url(video_url) 92 if real: 93 real_uri = MediaUri(real) 94 return real_uri
95
96 - def has_children_with_types(self, uri, media_types):
97 dfr = defer.Deferred() 98 99 has_children = False 100 if self._is_directory(uri): 101 if 'video' in media_types: 102 has_children = True 103 104 dfr.callback(has_children) 105 return dfr
106
107 - def _build_main_menu(self, children):
108 self.debug("Building main menu") 109 child_uri = MediaUri('youtube://recently_featured/today') 110 child_uri.label = T_(N_("Featured today")) 111 children.append((child_uri, {})) 112 113 child_uri = MediaUri('youtube://most_viewed') 114 child_uri.label = T_(N_("Most viewed")) 115 children.append((child_uri, {})) 116 117 child_uri = MediaUri('youtube://top_rated') 118 child_uri.label = T_(N_("Top rated")) 119 children.append((child_uri, {})) 120 121 child_uri = MediaUri('youtube://tags') 122 child_uri.label = T_(N_("Tags")) 123 children.append((child_uri, {})) 124 125 user = self.config.get('user','') 126 if user: 127 child_uri = MediaUri('youtube://favorite_videos/%s' % user) 128 child_uri.label = T_(N_("My favorite videos")) 129 children.append((child_uri, {})) 130 child_uri = MediaUri('youtube://user/%s' % user) 131 child_uri.label = T_(N_("My videos")) 132 children.append((child_uri, {})) 133 child_uri = MediaUri('youtube://contacts/%s' % user) 134 child_uri.label = T_(N_("My contacts")) 135 children.append((child_uri, {})) 136 137 return children
138 139
140 - def _retrieve_children(self, uri, children):
141 videos = [] 142 users = [] 143 tags = {} 144 145 host = uri.host 146 if host == '': 147 children = self._build_main_menu(children) 148 elif host == 'contacts': 149 username = uri.path[1:] # strip / from path 150 users = self._client.get_contacts(username) 151 elif host == 'recently_featured': 152 videos = self._client.recently_featured() 153 elif host == 'top_rated': 154 videos = self._client.top_rated() 155 elif host == 'most_viewed': 156 videos = self._client.most_viewed() 157 elif host == 'tags': 158 tags = self._client.get_categories() 159 elif host == 'tag': 160 tagname = uri.path[1:] # strip / from path 161 videos = self._client.get_category(tagname) 162 163 elif host == 'favorite_videos': 164 username = uri.path[1:] # strip / from path 165 videos = self._client.favorite_videos(username) 166 elif host == 'user': 167 username = uri.path[1:] # strip / from path 168 videos = self._client.videos_upload_by(username) 169 170 for tag, label in tags.iteritems(): 171 child_uri = MediaUri('youtube://tag/%s' % tag) 172 child_uri.label = label 173 children.append((child_uri, {})) 174 175 for video in videos: 176 video_id = video.id.text.split("/")[-1] 177 child_uri = MediaUri('youtube://video/%s' % video_id) 178 child_uri.label = video.title.text 179 thumbnail_url = self._client.get_largest_thumbnail(video) 180 metadata = DictObservable() 181 metadata['default_image'] = MediaUri(thumbnail_url) 182 children.append((child_uri, metadata)) 183 184 if users is not None: 185 for user in users: 186 child_uri = MediaUri('youtube://user/%s' % user) 187 child_uri.label = user 188 children.append((child_uri, {})) 189 190 return children
191 192
193 - def get_direct_children(self, uri, l):
194 d = threads.deferToThread(self._retrieve_children, uri, l) 195 return d
196
197 - def _blocking_open(self, uri, mode=None):
198 return None
199
200 - def uri_is_monitorable(self, uri):
201 return False
202