From dd6c897b87ea369edd265f9e36e9deb4606cde48 Mon Sep 17 00:00:00 2001 From: Jonathan Hornung Date: Sun, 20 Dec 2015 17:10:05 +0100 Subject: [PATCH] impr: Implemented location based search (close #3) --- README.md | 6 ++++-- demo/js/app.js | 19 ++++++++++++++----- dist/angular-youtube-api-factory.min.js | 2 +- src/angular-youtube-api-factory.js | 16 +++++++++++++--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fdd2491..b31bc67 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,10 @@ youtubeFactory.getVideosFromChannelById({ ```js //docs: https://developers.google.com/youtube/v3/docs/search/list -youtubeFactory.getVideosFromSearchByString({ - q: "", //search string +youtubeFactory.getVideosFromSearchByParams({ + q: "", // (optional) search string + location: "", // (optional) The parameter value is a string that specifies latitude/longitude coordinates e.g. '37.42307,-122.08427'. + locationRadius: "", // (optional) valid values e.g. '1500m', '5km', '10000ft', and '0.75mi' | default: '5000m' order: "", // (optional) valid values: 'date', 'rating', 'relevance', 'title', 'videoCount', 'viewCount' | default: 'date' maxResults: "", // (optional) valid values: 0-50 | default: 5 pageToken: "", // (optional) diff --git a/demo/js/app.js b/demo/js/app.js index 504c9c8..c8f437b 100644 --- a/demo/js/app.js +++ b/demo/js/app.js @@ -5,23 +5,32 @@ app.controller('controller', ['$scope', 'youtubeFactory', function($scope, youtu youtubeFactory.getVideosFromChannelById({ channelId: "UCVkXCOYluJvD6OPjX9HXj-A", - maxResults: "50", // (optional) default: 5 + maxResults: "50", key: _apiKey, }).success(function (_data) { console.info("videos from channel by id", _data); }); - youtubeFactory.getVideosFromSearchByString({ + youtubeFactory.getVideosFromSearchByParams({ q: "fcbayern", - maxResults: "50", // (optional) default: 5 + maxResults: "50", key: _apiKey, }).success(function (_data) { - console.info("videos from search by string", _data); + console.info("videos from search by q", _data); + }); + + youtubeFactory.getVideosFromSearchByParams({ + location: "37.42307,-122.08427", + locationRadius: "1000m", + maxResults: "50", + key: _apiKey, + }).success(function (_data) { + console.info("videos from search by location", _data); }); youtubeFactory.getVideosFromPlaylistById({ playlistId: "PLNLa2lbKPczGCueOYxjrwYDuNTBtQveK0", - maxResults: "50", // (optional) default: 5 + maxResults: "50", key: _apiKey, }).success(function (_data) { console.info("videos from playlist by id", _data); diff --git a/dist/angular-youtube-api-factory.min.js b/dist/angular-youtube-api-factory.min.js index 90bc9c1..23f09b1 100644 --- a/dist/angular-youtube-api-factory.min.js +++ b/dist/angular-youtube-api-factory.min.js @@ -1,3 +1,3 @@ /*! angular-youtube-api-factory v0.3.6 (20-12-2015) by Jonathan Hornung */ -"use strict";angular.module("jtt_youtube",[]).factory("youtubeFactory",["$http","youtubeSearchDataService",function(a,b){var c={};return c.getVideosFromChannelById=function(c){var d=b.getNew("videosFromChannelById",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideosFromSearchByString=function(c){var d=b.getNew("videosFromSearchByString",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideosFromPlaylistById=function(c){var d=b.getNew("videosFromPlaylistById",c);return a({method:"GET",url:d.url,params:d.object})},c.getChannelById=function(c){var d=b.getNew("channelById",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideoById=function(c){var d=b.getNew("videoById",c);return a({method:"GET",url:d.url,params:d.object})},c}]).service("youtubeSearchDataService",function(){this.getApiBaseUrl=function(a){return"https://content.googleapis.com/youtube/v3/"},this.getNew=function(a,b){var c={object:{part:"id,snippet",key:b.key},url:""};switch(a){case"videosFromChannelById":c.object.type="video",c.object.channelId=b.channelId,"undefined"!=typeof b.order&&(c.object.order=b.order),"undefined"!=typeof b.q&&(c.object.q=b.q),"undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videosFromSearchByString":c.object.type="video","undefined"!=typeof b.order&&(c.object.order=b.order),"undefined"!=typeof b.q&&(c.object.q=b.q),"undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videosFromPlaylistById":c.object.playlistId=b.playlistId,c.object.type="video","undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"playlistItems?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videoById":c.object.id=b.videoId,c.url=this.getApiBaseUrl()+"videos?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"channelById":c.object.type="channel",c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&")}return c}}); \ No newline at end of file +"use strict";angular.module("jtt_youtube",[]).factory("youtubeFactory",["$http","youtubeSearchDataService",function(a,b){var c={};return c.getVideosFromChannelById=function(c){var d=b.getNew("videosFromChannelById",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideosFromSearchByParams=function(c){var d=b.getNew("videosFromSearchByParams",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideosFromPlaylistById=function(c){var d=b.getNew("videosFromPlaylistById",c);return a({method:"GET",url:d.url,params:d.object})},c.getChannelById=function(c){var d=b.getNew("channelById",c);return a({method:"GET",url:d.url,params:d.object})},c.getVideoById=function(c){var d=b.getNew("videoById",c);return a({method:"GET",url:d.url,params:d.object})},c}]).service("youtubeSearchDataService",function(){this.getApiBaseUrl=function(a){return"https://content.googleapis.com/youtube/v3/"},this.getNew=function(a,b){var c={object:{part:"id,snippet",key:b.key},url:""};switch(a){case"videosFromChannelById":c.object.type="video",c.object.channelId=b.channelId,"undefined"!=typeof b.order&&(c.object.order=b.order),"undefined"!=typeof b.q&&(c.object.q=b.q),"undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videosFromSearchByParams":c.object.type="video","undefined"!=typeof b.order&&(c.object.order=b.order),"undefined"!=typeof b.q&&(c.object.q=b.q),"undefined"!=typeof b.location&&(c.object.location=b.location),"undefined"!=typeof b.locationRadius?c.object.locationRadius=b.locationRadius:"undefined"!=typeof b.location&&(c.object.locationRadius="5000m"),"undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videosFromPlaylistById":c.object.playlistId=b.playlistId,c.object.type="video","undefined"!=typeof b.maxResults&&(c.object.maxResults=b.maxResults),c.url=this.getApiBaseUrl()+"playlistItems?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"videoById":c.object.id=b.videoId,c.url=this.getApiBaseUrl()+"videos?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&");break;case"channelById":c.object.type="channel",c.url=this.getApiBaseUrl()+"search?","undefined"!=typeof b.nextPageToken&&(c.url+="pageToken="+b.nextPageToken+"&")}return c}}); \ No newline at end of file diff --git a/src/angular-youtube-api-factory.js b/src/angular-youtube-api-factory.js index a0f83f5..b3304c7 100644 --- a/src/angular-youtube-api-factory.js +++ b/src/angular-youtube-api-factory.js @@ -22,9 +22,9 @@ angular.module("jtt_youtube", []) }); }; - youtubeFactory.getVideosFromSearchByString = function (_params) { + youtubeFactory.getVideosFromSearchByParams = function (_params) { - var youtubeSearchData = youtubeSearchDataService.getNew("videosFromSearchByString", _params); + var youtubeSearchData = youtubeSearchDataService.getNew("videosFromSearchByParams", _params); return $http({ method: 'GET', @@ -105,7 +105,7 @@ angular.module("jtt_youtube", []) } break; - case "videosFromSearchByString": + case "videosFromSearchByParams": youtubeSearchData.object.type = "video"; if (typeof _params.order !== "undefined") { youtubeSearchData.object.order = _params.order; @@ -113,6 +113,16 @@ angular.module("jtt_youtube", []) if (typeof _params.q !== "undefined") { youtubeSearchData.object.q = _params.q; } + if (typeof _params.location !== "undefined") { + youtubeSearchData.object.location = _params.location; + } + if (typeof _params.locationRadius !== "undefined") { + youtubeSearchData.object.locationRadius = _params.locationRadius; + } else { + if (typeof _params.location !== "undefined") { + youtubeSearchData.object.locationRadius = "5000m" + } + } if (typeof _params.maxResults !== "undefined") { youtubeSearchData.object.maxResults = _params.maxResults; }