Skip to content

Commit

Permalink
feat: added possibility to receive videos by id
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyTheTank committed Feb 3, 2016
1 parent af7c30e commit 29e3483
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 13 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ Supported apiNG models
### II. Requests
Every **apiNG plugin** expects an array of **requests** as html attribute.

#### Requests by Video
| parameter | sample | default | description | optional |
|----------|---------|---------|---------|---------|
| **`videoId`** | `cBLulx9f9vc` | | Youtube `videoId` (comma seperated list) | no |

Sample requests:
* `[{'videoId':'cBLulx9f9vc'}, {'videoId':'tC76tIp0kBk'}]`
* `[{'videoId':'cBLulx9f9vc,tC76tIp0kBk'}]`

#### Requests by Channel
| parameter | sample | default | description | optional |
|----------|---------|---------|---------|---------|
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Jonathan Hornung <jonathan.hornung@gmail.com>"
],
"version": "0.7.9",
"version": "0.7.10",
"description": "Youtube plugin for apiNG",
"main": "dist/angular-plugin-youtube.min.js",
"moduleType": [],
Expand Down
8 changes: 6 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
<script src="../dist/aping-plugin-youtube.js"></script>
</head>
<body ng-app="app">

<h1>[{'videoId':'cBLulx9f9vc,tC76tIp0kBk'}]</h1>
<aping template-url="template.html" model="video" get-native-data="false" aping-youtube="[{'videoId':'cBLulx9f9vc,tC76tIp0kBk'}]"></aping>
<hr>
<h1>[{'channelId':'UCoxMr8CYaJY-om5eJ2RA_4g'}]</h1>
<aping template-url="template.html" model="video" get-native-data="false" aping-youtube="[{'channelId':'UCoxMr8CYaJY-om5eJ2RA_4g'}]"></aping>
<aping template-url="template.html" model="social" get-native-data="false" aping-youtube="[{'channelId':'UCoxMr8CYaJY-om5eJ2RA_4g'}]"></aping>
<hr>
<h1>[{'lat':'48.2187997', 'lng':'11.6247072', 'distance':'1000m'}]</h1>
<aping template-url="template.html" model="social" aping-youtube="[{'lat':'48.2187997', 'lng':'11.6247072', 'distance':'1000m'}]"></aping>
<aping template-url="template.html" model="video" aping-youtube="[{'lat':'48.2187997', 'lng':'11.6247072', 'distance':'1000m'}]"></aping>
<hr>
<h1>[{'search':'fcbayern'}]</h1>
<aping template-url="template.html" model="social" aping-youtube="[{'search':'fcbayern', 'order':'rating'}]"></aping>
Expand Down
77 changes: 73 additions & 4 deletions dist/aping-plugin-youtube.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
@name: aping-plugin-youtube
@version: 0.7.9 (03-02-2016)
@version: 0.7.10 (03-02-2016)
@author: Jonathan Hornung <jonathan.hornung@gmail.com>
@url: https://github.com/JohnnyTheTank/apiNG-plugin-youtube
@license: MIT
Expand Down Expand Up @@ -57,7 +57,18 @@ angular.module("jtt_aping_youtube", ['jtt_youtube'])
}


if (request.channelId) { //search for channelID (and optional searchterm)
if (request.videoId) {
requestObject.videoId = request.videoId;

youtubeFactory.getVideoById(requestObject)
.then(function (_videosData) {
if (_videosData) {
apingController.concatToResults(apingYoutubeHelper.getObjectByJsonData(_videosData, helperObject));
}
});

} else if (request.channelId) { //search for channelID (and optional searchterm)

requestObject.channelId = request.channelId;
if (request.search) {
requestObject.q = request.search;
Expand Down Expand Up @@ -118,6 +129,39 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
return "https://www.youtube.com/";
};

this.convertYoutubeDurationToSeconds = function (duration) {
var a = duration.match(/\d+/g);

if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
a = [0, a[0], 0];
}

if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
a = [a[0], 0, a[1]];
}
if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
a = [a[0], 0, 0];
}

duration = 0;

if (a.length == 3) {
duration = duration + parseInt(a[0]) * 3600;
duration = duration + parseInt(a[1]) * 60;
duration = duration + parseInt(a[2]);
}

if (a.length == 2) {
duration = duration + parseInt(a[0]) * 60;
duration = duration + parseInt(a[1]);
}

if (a.length == 1) {
duration = duration + parseInt(a[0]);
}
return duration
};

this.getYoutubeIdFromUrl = function (_url) {
var rx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
return _url.match(rx)[1] || false;
Expand Down Expand Up @@ -185,7 +229,7 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
blog_id: _item.snippet.channelId || undefined,
blog_link: this.getThisPlatformLink() + "channel/" + _item.snippet.channelId,
intern_type: _item.id.kind,
intern_id: _item.id.videoId || _item.snippet.resourceId.videoId,
intern_id: _item.id.videoId || ((_item.snippet.resourceId && _item.snippet.resourceId.videoId) ? _item.snippet.resourceId.videoId : _item.id),
timestamp: apingTimeHelper.getTimestampFromDateString(_item.snippet.publishedAt, 1000, 7200),
});
socialObject.date_time = new Date(socialObject.timestamp);
Expand All @@ -212,6 +256,17 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
socialObject.thumb_url = this.getYoutubeImageFromId(socialObject.intern_id, 'default');
socialObject.native_url = this.getYoutubeImageFromId(socialObject.intern_id);
socialObject.post_url = this.getThisPlatformLink() + "watch?v=" + socialObject.intern_id;

if (_item.statistics) {
if (_item.statistics.commentCount && _item.statistics.commentCount > 0) {
socialObject.comments = _item.statistics.commentCount;
}

if (_item.statistics.likeCount && _item.statistics.likeCount > 0) {
socialObject.likes = _item.statistics.likeCount;
}
}

return socialObject;
};

Expand All @@ -222,7 +277,7 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
blog_id: _item.snippet.channelId || undefined,
blog_link: this.getThisPlatformLink() + "channel/" + _item.snippet.channelId,
intern_type: _item.id.kind,
intern_id: _item.id.videoId || _item.snippet.resourceId.videoId,
intern_id: _item.id.videoId || ((_item.snippet.resourceId && _item.snippet.resourceId.videoId) ? _item.snippet.resourceId.videoId : _item.id),
timestamp: apingTimeHelper.getTimestampFromDateString(_item.snippet.publishedAt, 1000, 7200),
});
videoObject.date_time = new Date(videoObject.timestamp);
Expand All @@ -243,6 +298,20 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
videoObject.position = _item.snippet.position;
videoObject.markup = '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' + videoObject.intern_id + '?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>';

if (_item.statistics) {
if (_item.statistics.commentCount && _item.statistics.commentCount > 0) {
videoObject.comments = _item.statistics.commentCount;
}

if (_item.statistics.likeCount && _item.statistics.likeCount > 0) {
videoObject.likes = _item.statistics.likeCount;
}
}

if (_item.contentDetails && _item.contentDetails.duration) {
videoObject.duration = this.convertYoutubeDurationToSeconds(_item.contentDetails.duration);
}

return videoObject;
};
}]);;"use strict";
Expand Down
4 changes: 2 additions & 2 deletions dist/aping-plugin-youtube.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aping-plugin-youtube",
"version": "0.7.9",
"version": "0.7.10",
"description": "Youtube plugin for apiNG",
"main": "dist/angular-plugin-youtube.min.js",
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion src/aping-youtube-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ angular.module("jtt_aping_youtube", ['jtt_youtube'])
}


if (request.channelId) { //search for channelID (and optional searchterm)
if (request.videoId) {
requestObject.videoId = request.videoId;

youtubeFactory.getVideoById(requestObject)
.then(function (_videosData) {
if (_videosData) {
apingController.concatToResults(apingYoutubeHelper.getObjectByJsonData(_videosData, helperObject));
}
});

} else if (request.channelId) { //search for channelID (and optional searchterm)

requestObject.channelId = request.channelId;
if (request.search) {
requestObject.q = request.search;
Expand Down
62 changes: 60 additions & 2 deletions src/aping-youtube-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
return "https://www.youtube.com/";
};

this.convertYoutubeDurationToSeconds = function (duration) {
var a = duration.match(/\d+/g);

if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
a = [0, a[0], 0];
}

if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
a = [a[0], 0, a[1]];
}
if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
a = [a[0], 0, 0];
}

duration = 0;

if (a.length == 3) {
duration = duration + parseInt(a[0]) * 3600;
duration = duration + parseInt(a[1]) * 60;
duration = duration + parseInt(a[2]);
}

if (a.length == 2) {
duration = duration + parseInt(a[0]) * 60;
duration = duration + parseInt(a[1]);
}

if (a.length == 1) {
duration = duration + parseInt(a[0]);
}
return duration
};

this.getYoutubeIdFromUrl = function (_url) {
var rx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
return _url.match(rx)[1] || false;
Expand Down Expand Up @@ -76,7 +109,7 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
blog_id: _item.snippet.channelId || undefined,
blog_link: this.getThisPlatformLink() + "channel/" + _item.snippet.channelId,
intern_type: _item.id.kind,
intern_id: _item.id.videoId || _item.snippet.resourceId.videoId,
intern_id: _item.id.videoId || ((_item.snippet.resourceId && _item.snippet.resourceId.videoId) ? _item.snippet.resourceId.videoId : _item.id),
timestamp: apingTimeHelper.getTimestampFromDateString(_item.snippet.publishedAt, 1000, 7200),
});
socialObject.date_time = new Date(socialObject.timestamp);
Expand All @@ -103,6 +136,17 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
socialObject.thumb_url = this.getYoutubeImageFromId(socialObject.intern_id, 'default');
socialObject.native_url = this.getYoutubeImageFromId(socialObject.intern_id);
socialObject.post_url = this.getThisPlatformLink() + "watch?v=" + socialObject.intern_id;

if (_item.statistics) {
if (_item.statistics.commentCount && _item.statistics.commentCount > 0) {
socialObject.comments = _item.statistics.commentCount;
}

if (_item.statistics.likeCount && _item.statistics.likeCount > 0) {
socialObject.likes = _item.statistics.likeCount;
}
}

return socialObject;
};

Expand All @@ -113,7 +157,7 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
blog_id: _item.snippet.channelId || undefined,
blog_link: this.getThisPlatformLink() + "channel/" + _item.snippet.channelId,
intern_type: _item.id.kind,
intern_id: _item.id.videoId || _item.snippet.resourceId.videoId,
intern_id: _item.id.videoId || ((_item.snippet.resourceId && _item.snippet.resourceId.videoId) ? _item.snippet.resourceId.videoId : _item.id),
timestamp: apingTimeHelper.getTimestampFromDateString(_item.snippet.publishedAt, 1000, 7200),
});
videoObject.date_time = new Date(videoObject.timestamp);
Expand All @@ -134,6 +178,20 @@ angular.module("jtt_aping_youtube").service('apingYoutubeHelper', ['apingModels'
videoObject.position = _item.snippet.position;
videoObject.markup = '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' + videoObject.intern_id + '?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>';

if (_item.statistics) {
if (_item.statistics.commentCount && _item.statistics.commentCount > 0) {
videoObject.comments = _item.statistics.commentCount;
}

if (_item.statistics.likeCount && _item.statistics.likeCount > 0) {
videoObject.likes = _item.statistics.likeCount;
}
}

if (_item.contentDetails && _item.contentDetails.duration) {
videoObject.duration = this.convertYoutubeDurationToSeconds(_item.contentDetails.duration);
}

return videoObject;
};
}]);

0 comments on commit 29e3483

Please sign in to comment.