Skip to content

Commit

Permalink
Support YouTube channels as podcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
z------------- committed Mar 11, 2018
1 parent 73ea44e commit fbe9fe1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"snackbar_error-media-unsupported": "The media is missing or in an unsupported format.",
"snackbar_error-media-unknown": "An unknown media error occurred.",

"snackbar_error-podcast-detail": "Couldn't get podcast information.",

"snackbar_setting-save-success": "Setting saved.",
"snackbar_setting-save-success-restart": "Setting saved. Please restart CPod for changes to take effect.",
"snackbar_setting-save-fail": "Could not save setting.",
Expand Down
4 changes: 0 additions & 4 deletions public/app/js/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ var cbus = {};

APP_NAME = "CPod";

const REQUEST_HEADERS = {
"User-Agent": "CPod (github.com/z-------------)"
};

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
window.AudioContext = window.AudioContext || window.webkitAudioContext;

Expand Down
14 changes: 12 additions & 2 deletions public/app/js/cbus-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ cbus.const.podcastSort = function(a, b) {
return 0;
};

cbus.const.REQUEST_HEADERS = {
"User-Agent": "CPod (github.com/z-------------)"
};
cbus.const.REQUEST_TIMEOUT = 15 * 1000;

cbus.const.USERDATA_PATH = remote.app.getPath("userData");
cbus.const.OFFLINE_STORAGE_DIR = path.join(cbus.const.USERDATA_PATH, "offline_episodes");
cbus.const.PODCAST_IMAGES_DIR = path.join(cbus.const.USERDATA_PATH, "podcast_images");
Expand All @@ -20,5 +25,10 @@ cbus.const.IMAGE_ON_DISK_PLACEHOLDER = "__cbus_image_on_disk__";
cbus.const.PODCAST_ART_SIZE = 200;
cbus.const.STREAM_PAGE_LENGTH = 50;

cbus.const.urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/; // https://stackoverflow.com/a/3809435/
cbus.const.videoMimeRegex = /video\/\w+/gi;
cbus.const.urlRegex = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/; // https://stackoverflow.com/a/3809435/
cbus.const.videoMimeRegex = /video\/\w+/i;
cbus.const.youtubeChannelRegexLoose = /^https:\/\/www.youtube.com\/(channel\/UC(\w|-)+|user\/\w+)$/i;

cbus.const.creds = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "creds.json"), {
encoding: "utf8"
}));
13 changes: 9 additions & 4 deletions public/app/js/cbus-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ cbus.data.update = function(specificFeedData) {
description: episode.description,
date: (episodeDate.getTime() ? episodeDate : null), // check if date is valid
feedURL: feedUrl,
art: episode.episodeArt,
art: episode.art,
length: episode.length,
chapters: episode.chapters,
isVideo: episode.isVideo
Expand Down Expand Up @@ -514,9 +514,14 @@ cbus.broadcast.listen("showPodcastDetail", function(e) {
}

cbus.server.getPodcastInfo(e.data.url, function(data) {
data.url = e.data.url;
console.log("getPodcastInfo", data)
cbus.broadcast.send("gotPodcastData", data);
if (data) {
data.url = e.data.url;
console.log("getPodcastInfo", data);
cbus.data.feedsCache.push(data);
cbus.broadcast.send("gotPodcastData", data);
} else {
cbus.ui.showSnackbar(i18n.__("snackbar_error-podcast-detail"), "error");
}
});

cbus.data.state.podcastDetailCurrentData = {
Expand Down
2 changes: 1 addition & 1 deletion public/app/js/cbus-server-get-podcast-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!cbus.hasOwnProperty("server")) { cbus.server = {} }

request({
url: podcastUrl,
headers: REQUEST_HEADERS
headers: cbus.const.REQUEST_HEADERS
}, function(err, result, body) {
x2j.parseString(body, function(err, result) {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion public/app/js/cbus-server-search-podcasts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if (!cbus.hasOwnProperty("server")) { cbus.server = {} }
cbus.server.searchPodcasts = function(searchTerm, callback) {
request({
url: "https://itunes.apple.com/search?media=podcast&term=" + encodeURIComponent(searchTerm),
headers: REQUEST_HEADERS
headers: cbus.const.REQUEST_HEADERS
}, function(err, result, body) {
if (!err) {
var json = JSON.parse(body)
Expand Down
6 changes: 3 additions & 3 deletions public/app/js/cbus-server-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ if (!cbus.hasOwnProperty("server")) { cbus.server = {} }
console.log("starting update of feed '" + feed.title + "'");
request({
url: feed.url,
headers: REQUEST_HEADERS,
timeout: 15 * 1000
headers: cbus.const.REQUEST_HEADERS,
timeout: cbus.const.REQUEST_TIMEOUT
}, function(err, result, body) {
if (!err && result.statusCode.toString()[0] === "2") {
x2j.parseString(body, function(err, result) {
Expand Down Expand Up @@ -95,7 +95,7 @@ if (!cbus.hasOwnProperty("server")) { cbus.server = {} }
item["media:content"][0].$.type && item["media:content"][0].$.type.indexOf("image/") === 0) {
episodeArt = item["media:content"][0].$.url;
}
episodeInfo.episodeArt = episodeArt;
episodeInfo.art = episodeArt;

/* episode chapters (podlove.org/simple-chapters) */
var episodeChapters = [];
Expand Down
23 changes: 21 additions & 2 deletions public/app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,31 @@ $(document).ready(function() {
if (query.length > 0) {
searchResultsElem.innerHTML = "";

let regexMatchResult = query.match(cbus.const.urlRegex);
if (regexMatchResult && regexMatchResult[0] === query) {
if (cbus.const.youtubeChannelRegexLoose.test(query)) {
// YT channel in channel/<ID> or user/<username> format
request.post("https://podsync.net/api/create", {
json: {
url: query,
format: "video",
quality: "high",
page_size: 50
}
}, (err, res, body) => {
if (err || res.statusCode < 200 && res.statusCode >= 300) {
cbus.ui.showSnackbar(i18n.__("snackbar_error-podcast-detail"), "error");
} else {
cbus.broadcast.send("showPodcastDetail", {
url: "https://podsync.net/" + body.id
});
}
});
} else if (cbus.const.urlRegex.test(query)) {
// URL
cbus.broadcast.send("showPodcastDetail", {
url: query
});
} else {
// search query
cbus.server.searchPodcasts(query, function(data) {
if (data) {
cbus.broadcast.send("got-search-results")
Expand Down

0 comments on commit fbe9fe1

Please sign in to comment.