How to get videos from youtube playlist?
#1
Hi to all.

I am new to plugin development. Recently I wrote two simple plugins for my personal use, but I also published the code on GitHub (I can share links here if that is not in conflict with some forum policy Smile ).
Now, I would like to improve one of these plugins.
The plugin shows the live program of some regional TV station. I would like to include sections (folders) in plugin that will contains some tvShows that are emmited on that tv station. All episodes of tv shows are published as youtube videos and playlists (every show has its own youtube playlist).
I already know that I can use kodi youtube plugin to play specific youtube video, but the question is how can I get all youtube videos if I know the ID of the playlist?

I was searching on the web and saw that some of plugins used youtube api v2 which is deprecated.
I didn't saw that someone is using youtube api v3 in kodi plugins ? (I guess that that has something related with developer_id which is required for v3, and nobody wants to expose its own id Huh).

Is there any workaround or any other way to get youtube videos inforation from yt playlist?
Reply
#2
You should request an API key at the Google developers page. As long as the playlists are publicly available, you don't need athentication (other then your API key).

Then you can use the python googleapiclient to get the videos belonging to this playlist. You can get 50 videos at a time max. So when there are more videos you have to paginate the results, so you can grab 50 at a time. Examples of this are available at the Google Developers pages. Here you can also test the response from the youtube api. Quite handy.

Here is a function I made to get videos by playlistid in youtube api v3 (dont forget to import the googleapiclient, and set the vars.variables as your own):
Code:
#Grabs the videos from a playlist by playlistId
# Params:
    # id: The id of the playlist which videos you want to retrieve
    #nextpage: The nextpage token. Default: false. If set it will retrieve the page. That allows all videos to be parsed, instead of the 50 limit
def vids_by_playlist(id, nextpage = False):
    youtube = build(
      vars.YOUTUBE_API_SERVICE_NAME,
      vars.YOUTUBE_API_VERSION,
      developerKey=vars.API_KEY
    )
    
    if nextpage == False:
        search_response = youtube.playlistItems().list(
          part="snippet,contentDetails",
          maxResults=50,
          playlistId=id
        ).execute()
    else:
        
        search_response = youtube.playlistItems().list(
          part="snippet,contentDetails",
          maxResults=50,
          playlistId=id,
          pageToken=nextpage
        ).execute()
    
    return search_response
Reply
#3
Thanks, Sleuteltje.

I'll try that definitely!

I implemented some solution by using BeautifulSoup4. I parsed the TV-station's web-page that contains episodes of the shows. It worked but the performances are poor. It requires a lot of time to parse the page (find thumb-image, caption, link to the specific episode page, and then on that page find the youtube url).

Question related to API key:
Can there be some consequences if someone look at my code and use my API for it's own apps? Can someone abuse my API key somehow?
Reply
#4
(2015-10-29, 09:53)zchira Wrote: ....
Can there be some consequences if someone look at my code and use my API for it's own apps? Can someone abuse my API key somehow?
You're welcome. If someone would grab your API key from your code, they can use it themselves. They could write some script that uses up all your quota. You have a (free) quota of 50,000,000 each day, so you have plenty room. I don't know why they would though, you can request such API keys for free. Plus there are more then 1 open source app you can download which contains such a API key in its source code, they don't seem to be in trouble of exposing their API key?.

But I had the same thought. It doesn't feel good to expose your API key. I would not know of a way to have your script open source and your API key hidden, besides requiring users to input their own API key (not user friendly). If you find one, I'm interested.

Another thought I had, if you reach the 50,000,000 quota limit (I'm not even close now tho). What stops a developer of requesting more API keys and alternating the API keys in the code?
Reply
#5
Great. I tried it and it works!
Just need some time to implement all the stuff into my plugin.
Reply
#6
I implemented it. It works Smile
I am collecting info from youtube (title, videoId, thumbnail url, description) from all videos from playlist.
I am creating ListItems:

Code:
url = youtube_url(item.yt_id)
    li = xbmcgui.ListItem(item.title, iconImage=item.thumb)
    li.setProperty('isplayable', 'true')
    li.setProperty('fanart_image', fanart)
    #infoLabels = { 'title': item.title, 'plot' : item.description}
    #li.setInfo( type="video", infoLabels=infoLabels)
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li, isFolder=False)

Now I am trying to set video description to list item by uncommenting lines
Code:
infoLabels = { 'title': item.title, 'plot' : item.description }                                                                                                                
    li.setInfo( type="video", infoLabels=infoLabels)

after that the description is set (and I can see it when switch view to mediaInfo), but log file is filled with warnings and errors:

Code:
18:06:46 T:140231709697792  NOTICE: Thread BackgroundLoader start, auto delete: false
18:06:46 T:140232471934720 WARNING: CreateLoader - unsupported protocol(plugin) in plugin://plugin.video.youtube/play/?video_id=bG30_nEl1Wc
18:06:46 T:140232471934720   ERROR: InputStream: Error opening, plugin://plugin.video.youtube/play/?video_id=bG30_nEl1Wc
18:06:46 T:140232480327424 WARNING: CreateLoader - unsupported protocol(plugin) in plugin://plugin.video.youtube/play/?video_id=NgeldQSW4Ng
18:06:46 T:140232480327424   ERROR: InputStream: Error opening, plugin://plugin.video.youtube/play/?video_id=NgeldQSW4Ng
18:06:46 T:140232480327424 WARNING: CreateLoader - unsupported protocol(plugin) in plugin://plugin.video.youtube/play/?video_id=p4J_TPRexqI
18:06:46 T:140232480327424   ERROR: InputStream: Error opening, plugin://plugin.video.youtube/play/?video_id=p4J_TPRexqI
18:06:46 T:140232471934720 WARNING: CreateLoader - unsupported protocol(plugin) in plugin://plugin.video.youtube/play/?video_id=HNaXdswmgMw
18:06:46 T:140232471934720   ERROR: InputStream: Error opening, plugin://plugin.video.youtube/play/?video_id=HNaXdswmgMw


what should I do?
are this warnings/errors dangerous or harmles?
any idea?
Reply

Logout Mark Read Team Forum Stats Members Help
How to get videos from youtube playlist?0