Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for radio programs #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 88 additions & 13 deletions qml/js/yleApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ var api = (function(appId, appKey) {
return foundSubtitles && foundSubtitles[0] && foundSubtitles[0].uri;
}

function getMediaUrl(programId, mediaId) {
var url = apiUrl + mediaUrl + "&program_id=" + programId + "&media_id=" + mediaId + "&protocol=HLS";
function getMediaUrl(programId, mediaId, protocol) {
var url = [apiUrl, mediaUrl, "&program_id=", programId, "&media_id=", mediaId, "&protocol=", protocol || "HLS"].join("");

return HTTP.get(url)
.then(function(res) {
return {
subtitlesUrl: findSubtitlesUrlByLanguage("fi", res.data[0].subtitles),
subtitlesUrl: res.data && res.data[0] && res.data[0].subtitles && findSubtitlesUrlByLanguage("fi", res.data[0].subtitles) || '',
url: decryptUrl(res.data[0].url),
};
});
Expand Down Expand Up @@ -224,6 +225,78 @@ var api = (function(appId, appKey) {
});
}

function getRadioServices() {
var radioChannelsUrl = [apiUrl, servicesUrl, "&type=radiochannel"].join("");

return HTTP.get(radioChannelsUrl)
.then(function(response) { return response.data; })
.then(function(services) { return services.map(function(service) { return service.id; }) })
.catch(handleError("getRadioServices", function() { return []; }));
}

function getCurrentRadioBroadcasts() {
return getRadioServices()
.then(function(services) {
var url = apiUrl + currentBroadcastUrl + "&service=" + services.join(',') + "&start=0&end=0";
return HTTP.get(url)
})
.then(function(response) {
return response.data.map(function(res) {
return res.content;
})
})
.then(filterAvailablePrograms)
.then(mapPrograms)
.catch(handleError("getCurrentRadioBroadcasts", function() { return []; }));
}

function getNowPlayingRadioPrograms() {
function getNowPlayingRadioProgramUrl(id) {
return [apiUrl, "/programs/nowplaying/", id, ".json", "?", credentials].join("");
}

function resolvePromise(promise) {
return promise
.then(function(result) { return result })
.catch(function(error) { return {} });
}

return getRadioServices()
.then(function(services) {
return Promise.all(services.map(function(service) {
var url = getNowPlayingRadioProgramUrl(service);
return resolvePromise(HTTP.get(url));
}));
})
.then(function(results) {
return results.reduce(function(allServices, result) {
return allServices.concat(result.data || []);
}, []);
})
.then(function(allServices) {
return allServices.filter(function(service) {
return service.delta === "1";
});
})
.then(function(programs) {
return Promise.all(programs.map(function(program) {
var programId = program && program.service && program.service.id;
var mediaId = program && program.service && program.service.outlet && program.service.outlet.length && program.service.outlet[0] && program.service.outlet[0].media && program.service.outlet[0].media.id;
console.log(programId, mediaId);

if (!programId || !mediaId) return Promise.resolve(program);

return getMediaUrl(programId, mediaId)
.then(function(result) {
program.mediaUrl = result;
return program;
})
.catch(function(error) { console.log(JSON.stringify(error, null, 2)); return program; });
}));
})
.catch(handleError("getNowPlayingRadioPrograms", function() { return []; }));
}

return {
formatTime: formatTime,
formatProgramDetails: formatProgramDetails,
Expand All @@ -235,16 +308,18 @@ var api = (function(appId, appKey) {
getProgramsByCategoryId: getProgramsByCategoryId,
getProgramsBySeriesId: getProgramsBySeriesId,
search: search,
getCurrentRadioBroadcasts: getCurrentRadioBroadcasts,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the getNowPlayingRadioPrograms function which is used in FirstPage.qml. Also the function is missing from below in function list. In addition to this there now seems to be two functions which are probably supposed to do the same thing getNowPlayingRadioPrograms and getCurrentRadioBroadcasts.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, renamed the getNowPlayingRadioPrograms into getCurrentRadioBroadcasts and forgot to rename it into FirstPage.qml.

The two methods are using different endpoints, the other (not used atm) returns the currently playing songs/programs in the given radiochannel and which I thought was needed for listening those songs. Not sure if we want to show information about next/current/previous songs somewhere in the app?

};
})(appId, appKey);

function formatTime() { return api.formatTime.apply(null, arguments) };
function formatProgramDetails() { return api.formatProgramDetails.apply(null, arguments) };
function getCategories() { return api.getCategories.apply(null, arguments) };
function getCurrentBroadcasts() { return api.getCurrentBroadcasts.apply(null, arguments) };
function getMediaUrl() { return api.getMediaUrl.apply(null, arguments) };
function reportUsage() { return api.reportUsage.apply(null, arguments) };
function getProgramById() { return api.getProgramById.apply(null, arguments) };
function getProgramsByCategoryId() { return api.getProgramsByCategoryId.apply(null, arguments) };
function getProgramsBySeriesId() { return api.getProgramsBySeriesId.apply(null, arguments) };
function search() { return api.search.apply(null, arguments) };
function formatTime() { return api.formatTime.apply(null, arguments); }
function formatProgramDetails() { return api.formatProgramDetails.apply(null, arguments); }
function getCategories() { return api.getCategories.apply(null, arguments); }
function getCurrentBroadcasts() { return api.getCurrentBroadcasts.apply(null, arguments); }
function getMediaUrl() { return api.getMediaUrl.apply(null, arguments); }
function reportUsage() { return api.reportUsage.apply(null, arguments); }
function getProgramById() { return api.getProgramById.apply(null, arguments); }
function getProgramsByCategoryId() { return api.getProgramsByCategoryId.apply(null, arguments); }
function getProgramsBySeriesId() { return api.getProgramsBySeriesId.apply(null, arguments); }
function search() { return api.search.apply(null, arguments); }
function getCurrentRadioBroadcasts() { return api.getCurrentRadioBroadcasts.apply(null, arguments); }
1 change: 1 addition & 0 deletions qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ApplicationWindow
property string coverMode: ""
property string coverTitle: ""
property string coverSubTitle: ""
property string playbackMode: "tv"

function updateCover(newCoverMode, newCoverTitle, newCoverSubtitle) {
coverMode = newCoverMode ? newCoverMode : ""
Expand Down
26 changes: 22 additions & 4 deletions qml/pages/FirstPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import "../js/yleApi.js" as YleApi
Page {
id: page

function updateContent() {
if (playbackMode === "tv") {
YleApi.getCurrentBroadcasts()
.then(function(broadcasts) {
listView.model = broadcasts
})
} else if (playbackMode === "radio") {
YleApi.getCurrentRadioBroadcasts()
.then(function(programs) {
listView.model = programs
})
}
}

Component.onCompleted: {
updateCover(qsTr("Current Broadcasts"), "", "")
YleApi.getCurrentBroadcasts()
.then(function(broadcasts) {
listView.model = broadcasts
})
updateContent()
}

onVisibleChanged: {
Expand Down Expand Up @@ -39,6 +50,13 @@ Page {
text: qsTr("Categories")
onClicked: pageStack.push(Qt.resolvedUrl("CategoriesPage.qml"))
}
MenuItem {
text: playbackMode === "tv" ? qsTr("Radio") : qsTr("Tv")
onClicked: {
playbackMode = playbackMode === "tv" ? "radio" : "tv"
updateContent()
}
}
}

SilicaListView {
Expand Down
4 changes: 3 additions & 1 deletion qml/pages/PlayerPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ Page {
}

function initialize() {
YleApi.getMediaUrl(program.id, program.mediaId)
var protocol = playbackMode === "radio" ? "PMD" : "HLS"
YleApi.getMediaUrl(program.id, program.mediaId, protocol)
.then(function(response) {
if (response.subtitlesUrl && subtitlesText) {
subtitlesUrl = response.subtitlesUrl
subtitlesText.getSubtitles(subtitlesUrl)
}
console.log("playbackMode", playbackMode, JSON.stringify(response))
mediaPlayer.source = response.url
mediaPlayer.play()
YleApi.reportUsage(program.id, program.mediaId)
Expand Down
28 changes: 18 additions & 10 deletions translations/harbour-nayttamo-fi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,39 @@
<name>FirstPage</name>
<message>
<source>Current Broadcasts</source>
<translation>Menossa olevat ohjelmat</translation>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<translation type="unfinished">Tietoa</translation>
</message>
<message>
<source>Search</source>
<translation>Haku</translation>
<translation type="unfinished">Haku</translation>
</message>
<message>
<source>Categories</source>
<translation>Kategoriat</translation>
<translation type="unfinished">Kategoriat</translation>
</message>
<message>
<source>Show program info</source>
<translation>Näytä ohjelman tiedot</translation>
<source>Radio</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<translation>Tietoa</translation>
<source>Tv</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No current broadcasts</source>
<translation>Ei tämänhetkisiä lähetyksiä</translation>
<source>Show program info</source>
<translation type="unfinished">Näytä ohjelman tiedot</translation>
</message>
<message>
<source>Show programs in series</source>
<translation>Näytä sarjan ohjelmat</translation>
<translation type="unfinished">Näytä sarjan ohjelmat</translation>
</message>
<message>
<source>No current broadcasts</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
Expand Down
28 changes: 18 additions & 10 deletions translations/harbour-nayttamo-sv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,39 @@
<name>FirstPage</name>
<message>
<source>Current Broadcasts</source>
<translation>Nu spelas</translation>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<translation type="unfinished">Info</translation>
</message>
<message>
<source>Search</source>
<translation>Sök</translation>
<translation type="unfinished">Sök</translation>
</message>
<message>
<source>Categories</source>
<translation>Kategorier</translation>
<translation type="unfinished">Kategorier</translation>
</message>
<message>
<source>Show program info</source>
<translation>Visa programinformation</translation>
<source>Radio</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<translation>Info</translation>
<source>Tv</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No current broadcasts</source>
<translation>Nu spelas ingenting</translation>
<source>Show program info</source>
<translation type="unfinished">Visa programinformation</translation>
</message>
<message>
<source>Show programs in series</source>
<translation>Visa program i serie</translation>
<translation type="unfinished">Visa program i serie</translation>
</message>
<message>
<source>No current broadcasts</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
Expand Down
14 changes: 11 additions & 3 deletions translations/harbour-nayttamo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<source>Current Broadcasts</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search</source>
<translation type="unfinished"></translation>
Expand All @@ -97,21 +101,25 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>Show program info</source>
<source>Radio</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>About</source>
<source>Tv</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No current broadcasts</source>
<source>Show program info</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Show programs in series</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No current broadcasts</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>PlayerPage</name>
Expand Down