Play youtube video from plugin
#1
Hi,
I am scraping a website with youtube videos and want to play those videos within XBMC. How do I go about playing these videos in XBMC can anyone give me the code required for this or point me to a plugin which does that so I can have a look.

I have done several searches and not able to find anything that works for me. Really appreciate your help with this.

Thanks
Reply
#2
Have a look at the MusicVideoJukebox add-on. I believe it uses the youtube add-on.
Reply
#3
or rbk.no

basically; return urls of the form plugin://plugin.video.youtube?action=play_video&videoid=<id>

don't forget to set IsPlayable!
Reply
#4
spiff Wrote:or rbk.no

basically; return urls of the form plugin://plugin.video.youtube?action=play_video&videoid=<id>

don't forget to set IsPlayable!

Thanks for this. I had a look at rbk.no but couldnt get the source. I tried adding the URL as plugin://plugin.video.youtube?action=play_video&videoid=<id> that didn't work. How do I set IsPlayable? I am very very new to python and xbmc hence any help will be much appreciated.
Reply
#5
Hmm install rbk using the addon installer. and done you have the source.
Reply
#6
spiff Wrote:Hmm install rbk using the addon installer. and done you have the source.

Did that got the code and it seems i am doing everything as per rbk.no. This is frustrating I want to give it one last shot before i give up. The code I am using below its able to scrape the site and return the url list only when i click it to play i am getting script error. i am using the same script provided in the vionage tutorial with changes required for this site.. see script...

My issue i think is in the VIDEOLINKS(url,name) section. Can anyone have a quick look please.....


Code:
import urllib,urllib2,re,xbmcplugin,xbmcgui

#TV DASH - by You 2008.

def CATEGORIES():
        addDir("Vijay TV Shows","http://www.rajtamil.com/search/label/Vijay%20TV%20shows?max-results=25",1,"http://reggiebibbs.files.wordpress.com/2007/12/the-movies360-crop.jpg")

        addDir( 'SUN TV Shows','http://www.rajtamil.com/search/label/Sun%20TV%20Shows?max-results=25',2,'http://www.homefrontcalgary.com/assets/images/news_images/tv(2).jpg')
                      
def INDEX(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
        match=re.compile('<a href=''(.+?)'' title=''(.+?)''>(.+?)</a>').findall(link)
        for url,name,thumbnail in match:
                addDir(name,url,4,thumbnail)
def INDEX2(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
        match=re.compile('<a title="Watch (.+?) Online" href="(.+?)"><img src="(.+?)"').findall(link)
        for name,url,thumb in match:
                addDir(name,'http://www.tvdash.com/'+url,3,thumb)
                
def EPISODES(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
        match=re.compile('<a title="Watch (.+?) Online" href="(.+?)"><img src="(.+?)"').findall(link)
        for name,url,thumb in match:
                if not thumb.find('http://')>0:
                        thumb='http://www.tvdash.com/'+thumb
                        addDir(name,'http://www.tvdash.com/'+url,4,thumb)

def VIDEOLINKS(url,name):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
    match=re.compile('<embed src="http://www.youtube.com/.+?/(.+?)?version=.+?" type=".+?"').findall(link)
    url = 'plugin://plugin.video.youtube/?action=play_video&videoid='+ + match[0].replace('?','')
    addLink(name,url,'')

def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]
                                
        return param




def addLink(name,url,iconimage):
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
    liz.setProperty("IsPlayable","true")
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz,isFolder=False)
        return ok


def addDir(name,url,mode,iconimage):
        u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
        return ok
        
              
params=get_params()
url=None
name=None
mode=None

try:
        url=urllib.unquote_plus(params["url"])
except:
        pass
try:
        name=urllib.unquote_plus(params["name"])
except:
        pass
try:
        mode=int(params["mode"])
except:
        pass

print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)

if mode==None or url==None or len(url)<1:
        print ""
        CATEGORIES()
      
elif mode==1:
        print ""+url
        INDEX(url)
        
elif mode==2:
        print ""+url
        INDEX2(url)

elif mode==3:
        print ""+url
        EPISODES(url)

elif mode==4:
        print ""+url
        VIDEOLINKS(url,name)



xbmcplugin.endOfDirectory(int(sys.argv[1]))
Reply
#7
Hi,

replace
Code:
url = 'plugin://plugin.video.youtube/?action=play_video&videoid='+ + match[0].replace('?','')
with
Code:
url = 'plugin://plugin.video.youtube/?action=play_video&videoid=' + match[0].replace('?','')
You have a double plus-sign.

didn't test the code but this one is definitely wrong Smile

regards,
sphere
My GitHub. My Add-ons:
Image
Reply
#8
Another (harder to find) error in your code is your wrong usage of quotes.
- this is not SQL where you can quote with double-usage
- you can use double-quotes in single-quote (print 'Foo "master" bar')
- you can use single-quotes in double-quote (print "Foo 'master' bar")
- you also can escape a quote-sign with a backslash (print "Foo \"master\" bar")

replace (in INDEX function):
Code:
match=re.compile('<a href=''(.+?)'' title=''(.+?)''>(.+?)</a>').findall(link)
with:
Code:
match=re.compile("<a href='(.+?)' title='(.+?)'>(.+?)</a>").findall(link)
This is the reason why your labels AND URLs were containing quotes. urllib which opens the url for you doesn't like url starting with a quote.

regards,
sphere
My GitHub. My Add-ons:
Image
Reply
#9
Great thanks sphere appreciate this help. I am at work now so will try this soon as I get home Smile

Is there a way to write to the xbmc log from a plugin? I wish I can post some comments to the log so I can see where its failing. For example I didn't realise the url had a quote.
Reply
#10
gankens Wrote:Is there a way to write to the xbmc log from a plugin? I wish I can post some comments to the log so I can see where its failing. For example I didn't realise the url had a quote.

Hi,

of course you can, the dirty way is just add "print what_you_like" to your code - it will print into the xbmc log with loglevel=notice.
A better way would be using the log method from the xbmc module.

regards
My GitHub. My Add-ons:
Image
Reply
#11
Almost there looks like the youtube plugin can't play playlists. getting this error


19:33:35 T:2953850880 M:1382260736 ERROR: Traceback (most recent call last):
File "/Users/ganken/Library/Application Support/XBMC/addons/plugin.video.youtube/default.py", line 48, in ?
navigator.executeAction(params)
File "/Users/ganken/Library/Application Support/XBMC/addons/plugin.video.youtube/YouTubeNavigation.py", line 203, in executeAction
player.playVideo(params)
File "/Users/ganken/Library/Application Support/XBMC/addons/plugin.video.youtube/YouTubePlayer.py", line 52, in playVideo
(video, status) = self.getVideoObject(params);
File "/Users/ganken/Library/Application Support/XBMC/addons/plugin.video.youtube/YouTubePlayer.py", line 251, in getVideoObject
(links, video) = self._getVideoLinks(video, params)
File "/Users/ganken/Library/Application Support/XBMC/addons/plugin.video.youtube/YouTubePlayer.py", line 322, in _getVideoLinks
video["apierror"] = re.compile('reason=(.*)%3Cbr').findall(content)[0]
NameError: global name 'content' is not defined
19:33:35 T:2691908928 M:1382289408 ERROR: Playlist Player: skipping unplayable item: 0, path [plugin://plugin.video.youtube/?action=play_video&videoid=F88FE41863FCB8AC]
Reply
#12
What is the videoid you call the youtube plugin with?
My GitHub. My Add-ons:
Image
Reply
#13
sphere Wrote:What is the videoid you call the youtube plugin with?

Its F88FE41863FCB8AC

Link: http://www.youtube.com/p/F88FE41863FCB8AC

Seems to be saying its not playable
19:33:35 T:2691908928 M:1382289408 ERROR: Playlist Player: skipping unplayable item: 0, path [plugin://plugin.video.youtube/?action=play_video&videoid=F88FE41863FCB8AC]
Reply
#14
Anyway I have to find a different site where the videos are not in a playlist. Seems the xbmc youtube plugin can't play playlist (or I don't know how to make it happen).

Managed to scrape the new site and able to play the videos as well. Couldn't have got this far without everyones help here specially sphere. Thanks everyone. Smile
Reply
#15
Good luck with your new plugin Wink

Btw. I would suggest using a code hosting service/webiste like github - there others can help you quicker and easier...

regards,
sphere
My GitHub. My Add-ons:
Image
Reply
 
Thread Rating:
  • 0 Vote(s) - 0 Average



Logout Mark Read Team Forum Stats Members Help
Play youtube video from plugin00
This forum uses Lukasz Tkacz MyBB addons.