Skip to content

Commit cdecb72

Browse files
authored
Merge pull request #167 from BuildFire/fix-channel-urls
support new channel urls format
2 parents 0500794 + 8fdce99 commit cdecb72

3 files changed

Lines changed: 48 additions & 13 deletions

File tree

control/content/app.services.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242
};
4343
}])
44-
.factory("Utils", [function () {
44+
.factory("Utils", ["$http", "YOUTUBE_KEYS", function ($http, YOUTUBE_KEYS) {
4545
return {
4646
extractSingleVideoId: function (url) {
4747
var regExp = /^https?:\/\/(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/;
@@ -52,6 +52,29 @@
5252
return null;
5353
}
5454
},
55+
fixChannelIdURL: function (url, callback){
56+
let isChannel = this.extractChannelId(url);
57+
let isSingle = this.extractSingleVideoId(url);
58+
let isPlaylist = this.extractPlaylistId(url);
59+
if(isChannel || isSingle || isPlaylist) return callback(null, url);
60+
61+
let regex = /((http|https):\/\/|)(www\.)?youtube\.com\/([a-zA-Z0-9_\-@]{1,})/;
62+
let res = url.match(regex);
63+
if (res && res.length > 2 && res[4]) {
64+
$http.get("https://youtube.googleapis.com/youtube/v3/search?part=snippet&q="+res[4]+"&type=channel&key=" + YOUTUBE_KEYS.API_KEY, { cache: true })
65+
.success(function (response) {
66+
if(response.items[0] && response.items[0].id && response.items[0].id.channelId){
67+
callback(null, "https://www.youtube.com/channel/"+response.items[0].id.channelId);
68+
}else {
69+
callback(null, url);
70+
}
71+
}).error(function (error) {
72+
callback(error);
73+
});
74+
}else{
75+
callback(null, url);
76+
}
77+
},
5578
extractChannelId: function (url) {
5679
var regex = /((http|https):\/\/|)(www\.)?youtube\.com\/(channel|user|c)\/([a-zA-Z0-9_\-]{1,})/;
5780
var res = url.match(regex);

control/content/controllers/content.home.controller.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,28 @@
222222
$scope.updatedWithDelay = () => {
223223
$timeout.cancel(validateTimeOut);
224224
validateTimeOut = $timeout(() => {
225-
ContentHome.validateRssLink();
225+
ContentHome.fixChannelIdURL();
226226
}, 700);
227227
};
228228

229+
ContentHome.fixChannelIdURL = function(){
230+
if(ContentHome.rssLink){
231+
Utils.fixChannelIdURL(ContentHome.rssLink, (err,res)=>{
232+
if(err) console.error(err);
233+
if(res) return ContentHome.validateRssLink(res);
234+
235+
return ContentHome.validateRssLink(ContentHome.rssLink);
236+
});
237+
};
238+
};
239+
229240
// Function to validate youtube rss feed link entered by user.
230241

231-
ContentHome.validateRssLink = function() {
232-
let isChannel = Utils.extractChannelId(ContentHome.rssLink);
233-
let isVideo = Utils.extractSingleVideoId(ContentHome.rssLink);
234-
let isPlaylist = Utils.extractPlaylistId(ContentHome.rssLink);
242+
ContentHome.validateRssLink = function(youtubeUrl){
243+
if(!youtubeUrl) return ContentHome.fixChannelIdURL();
244+
let isChannel = Utils.extractChannelId(youtubeUrl);
245+
let isVideo = Utils.extractSingleVideoId(youtubeUrl);
246+
let isPlaylist = Utils.extractPlaylistId(youtubeUrl);
235247

236248
if (isChannel) {
237249
ContentHome.contentType = CONTENT_TYPE.CHANNEL_FEED;
@@ -257,7 +269,7 @@
257269

258270
switch (ContentHome.contentType) {
259271
case CONTENT_TYPE.SINGLE_VIDEO:
260-
var videoID = Utils.extractSingleVideoId(ContentHome.rssLink);
272+
var videoID = Utils.extractSingleVideoId(youtubeUrl);
261273
if (videoID) {
262274
$http
263275
.get(
@@ -302,7 +314,7 @@
302314
if (!$scope.$$phase) $scope.$apply();
303315
});
304316
} else {
305-
if (Utils.extractChannelId(ContentHome.rssLink)) {
317+
if (Utils.extractChannelId(youtubeUrl)) {
306318
ContentHome.failureMessage =
307319
"Seems like you have entered feed url. Please choose correct option to validate url.";
308320
}
@@ -318,7 +330,7 @@
318330
}
319331
break;
320332
case CONTENT_TYPE.CHANNEL_FEED:
321-
var feedIdAndType = Utils.extractChannelId(ContentHome.rssLink);
333+
var feedIdAndType = Utils.extractChannelId(youtubeUrl);
322334
var feedApiUrl = null;
323335
if (feedIdAndType) {
324336
if (feedIdAndType.channel)
@@ -377,7 +389,7 @@
377389
if (!$scope.$$phase) $scope.$apply();
378390
});
379391
} else {
380-
if (Utils.extractSingleVideoId(ContentHome.rssLink)) {
392+
if (Utils.extractSingleVideoId(youtubeUrl)) {
381393
ContentHome.failureMessage =
382394
"Seems like you have entered single video url. Please choose correct option to validate url.";
383395
}
@@ -393,7 +405,7 @@
393405
}
394406
break;
395407
case CONTENT_TYPE.PLAYLIST_FEED:
396-
var playlistId = Utils.extractPlaylistId(ContentHome.rssLink);
408+
var playlistId = Utils.extractPlaylistId(youtubeUrl);
397409
if (playlistId) {
398410
$http
399411
.post(PROXY_SERVER.serverUrl + "/videos", {
@@ -437,7 +449,7 @@
437449
if (!$scope.$$phase) $scope.$apply();
438450
});
439451
} else {
440-
if (Utils.extractSingleVideoId(ContentHome.rssLink)) {
452+
if (Utils.extractSingleVideoId(youtubeUrl)) {
441453
ContentHome.failureMessage =
442454
"Seems like you have entered single video url. Please choose correct option to validate url.";
443455
}

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
"widget": {},
2121
"features": [{"name" : "sharing"}],
2222
"languages": ["en"]
23-
}
23+
}

0 commit comments

Comments
 (0)