YouTube plugin using Data API V3
Posted: Sat Apr 25, 2015 5:02 pm
Plugin type Webresource
One simple url:
Note: The url covers channel uploads since everything is playlists.
A Google YouTube API key is needed in code below.
One simple url:
https://www.googleapis.com/youtube/v3/playlistItems?playlistId=YOUR_PLAYLIST_ID_HERE
Note: The url covers channel uploads since everything is playlists.
A Google YouTube API key is needed in code below.
- Code:
import org.serviio.library.metadata.*
import org.serviio.library.online.*
import groovy.json.*
/**
* YouTube.com content URL extractor plugin.
*
* API call playlistItems.list is implemented
* Using youtube_dl with best (default) quality
*
* @author Stefan Andersson
*
**/
class YouTube extends WebResourceUrlExtractor {
final VALID_RESOURCE_URL = '^https?://www.googleapis.com/youtube/.*$'
String getExtractorName() {
return getClass().getName()
}
boolean extractorMatches(URL feedUrl)
{
return feedUrl ==~ VALID_RESOURCE_URL
}
WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve)
{
final user_agent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"
final api_key = "YOUR_API_KEY_HERE"
if (maxItemsToRetrieve == -1) maxItemsToRetrieve = 50
def apiUrl = new URL(resourceUrl.toString() + "&part=snippet" + "&maxResults=" + maxItemsToRetrieve.toString() + "&key=" + api_key)
def json = new JsonSlurper().parseText(openURL(apiUrl, user_agent))
def items = []
json.items.each()
{
if (it.snippet.title != "Deleted video")
{
items.add(new WebResourceItem(title : it.snippet.title,
additionalInfo : ['videoId' : it.snippet.resourceId.videoId,
'thumb' : it.snippet.thumbnails.high.url]))
log("title: ${it.snippet.title}")
log("videoId: ${it.snippet.resourceId.videoId}")
log("thumb: ${it.snippet.thumbnails.standard.url}")
}
}
return new WebResourceContainer(title : "", items: items)
}
ContentURLContainer extractUrl(WebResourceItem item, PreferredQuality requestedQuality)
{
def linkUrl = "http://www.youtube.com/watch?v=${item.additionalInfo['videoId']}"
def thumbnailUrl = item.additionalInfo['thumb']
.
.
.
.
}