YouTube Video Player App
import QtQuick 2.0
import Felgo 4.0
Item {
id: api
property string apiKey: "<your-api-key>"
property bool requestsPending: HttpNetworkActivityIndicator.enabled
Component.onCompleted: {
HttpNetworkActivityIndicator.setActivationDelay(0)
}
function getChannelForUsername(name, success, error) {
var reqUrl = _.getChannelForUsernameRequest.arg(name).arg(apiKey);
_.sendGetRequest(reqUrl, function(data) {
if(data && data.items[0])
success(data.items[0])
else
success(undefined)
}, error)
}
function getChannelPlaylists(channelId, success, error) {
var reqUrl = _.getChannelPlaylistsRequest.arg(channelId).arg(apiKey);
_.sendGetRequest(reqUrl, function(data) {
if(data && data.items)
success(data.items)
else
success([])
}, error)
}
function getPlaylistById(playlistId, success, error) {
var reqUrl = _.getPlaylistByIdRequest.arg(playlistId).arg(apiKey);
_.sendGetRequest(reqUrl, function(data) {
if(data && data.items[0])
success(data.items[0])
else
success(undefined)
}, error)
}
function getPlaylistItems(playlistId, success, error) {
var reqUrl = _.getPlaylistItemsRequest.arg(playlistId).arg(apiKey);
_.sendGetRequest(reqUrl, function(data) {
if(data && data.items)
success(data.items)
else
success([])
}, error)
}
function getVideos(videoIds, success, error) {
var idParam = ""
if(videoIds.length)
idParam = videoIds.join()
else
idParam = videoIds
var reqUrl = _.getVideosRequest.arg(idParam).arg(apiKey);
_.sendGetRequest(reqUrl, function(data) {
if(data && data.items)
success(data.items)
else
success([])
}, error)
}
QtObject {
id: _
property string getChannelForUsernameRequest: "https://www.googleapis.com/youtube/v3/channels?part=id,snippet,brandingSettings,contentDetails&forUsername=%1&key=%2"
property string getChannelPlaylistsRequest: "https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=%1&maxResults=50&key=%2"
property string getPlaylistByIdRequest: "https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&id=%1&key=%2"
property string getPlaylistItemsRequest: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails,status&playlistId=%1&maxResults=50&key=%2"
property string getVideosRequest: "https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,status,statistics&id=%1&maxResults=50&key=%2"
function sendGetRequest(url, successHandler, errorHandler) {
HttpRequest.get(url)
.then(function(response) {
successHandler(response.body)
})
.catch(function(error) {
console.error("Error: Failed to load data from "+url+", status = "+xmlHttpReq.status+", response = "+XMLHttpRequest.responseText)
if(errorHandler !== undefined)
errorHandler(error.response.status, error.message)
})