PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/files/aping.plugin-bandsintown/0.7.7/aping-plugin-bandsintown.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 390 lines | 311 code | 69 blank | 10 comment | 85 complexity | fa7d03d8549a4e7954ea049f28a82060 MD5 | raw file
  1. /**
  2. @name: aping-plugin-bandsintown
  3. @version: 0.7.7 (11-01-2016)
  4. @author: Jonathan Hornung <jonathan.hornung@gmail.com>
  5. @url: https://github.com/JohnnyTheTank/apiNG-plugin-bandsintown
  6. @license: MIT
  7. */
  8. "use strict";
  9. var jjtApingBandsintown = angular.module("jtt_aping_bandsintown", ['jtt_bandsintown'])
  10. .directive('apingBandsintown', ['apingBandsintownHelper', 'apingUtilityHelper', 'bandsintownFactory', function (apingBandsintownHelper, apingUtilityHelper, bandsintownFactory) {
  11. return {
  12. require: '?aping',
  13. restrict: 'A',
  14. replace: 'false',
  15. link: function (scope, element, attrs, apingController) {
  16. var appSettings = apingController.getAppSettings();
  17. var requests = apingUtilityHelper.parseJsonFromAttributes(attrs.apingBandsintown, apingBandsintownHelper.getThisPlattformString(), appSettings);
  18. requests.forEach(function (request) {
  19. //create helperObject for helper function call
  20. var helperObject = {
  21. model: appSettings.model,
  22. showAvatar : request.showAvatar || false,
  23. artist: request.artist || undefined
  24. };
  25. if(typeof request.items !== "undefined") {
  26. helperObject.items = request.items;
  27. } else {
  28. helperObject.items = appSettings.items;
  29. }
  30. if(typeof appSettings.getNativeData !== "undefined") {
  31. helperObject.getNativeData = appSettings.getNativeData;
  32. } else {
  33. helperObject.getNativeData = false;
  34. }
  35. //create requestObject for api request call
  36. var requestObject = {
  37. app_id: apingUtilityHelper.getApiCredentials(apingBandsintownHelper.getThisPlattformString(), "app_id") || 'apiNG',
  38. };
  39. if(typeof request.items !== "undefined") {
  40. requestObject.count = request.items;
  41. } else {
  42. requestObject.count = appSettings.items;
  43. }
  44. if(requestObject.count === 0 || requestObject.count === '0') {
  45. return false;
  46. }
  47. // -1 is "no explicit limit". same for NaN value
  48. if(requestObject.count < 0 || isNaN(requestObject.count)) {
  49. requestObject.count = undefined;
  50. }
  51. if(request.artist) {
  52. requestObject.artist = request.artist;
  53. if(typeof request.artist_id !== "undefined") {
  54. requestObject.artist_id = request.artist_id;
  55. }
  56. if(typeof request.date !== "undefined") {
  57. requestObject.date = request.date;
  58. }
  59. if(typeof request.start_date !== "undefined" && typeof request.end_date !== "undefined") {
  60. requestObject.date = request.start_date+","+request.end_date;
  61. }
  62. if( typeof request.location !== "undefined"
  63. || (typeof request.lat !== "undefined" && typeof request.lng !== "undefined" )
  64. || typeof request.ip_address !== "undefined"
  65. ) {
  66. if(typeof request.location !== "undefined") {
  67. requestObject.location = request.location;
  68. } else if(typeof request.lat !== "undefined" && typeof request.lng !== "undefined") {
  69. requestObject.location = request.lat + ","+request.lng;
  70. } else {
  71. requestObject.location = request.ip_address;
  72. }
  73. if(typeof request.distance !== "undefined") {
  74. requestObject.radius = request.distance;
  75. }
  76. if(request.recommended === true || request.recommended === "true") {
  77. if(typeof request.exclude !== "undefined") {
  78. requestObject.only_recs = request.exclude;
  79. }
  80. bandsintownFactory.getRecommendedEventsFromArtistByLocation(requestObject)
  81. .then(function (_data) {
  82. if (_data) {
  83. apingController.concatToResults(apingBandsintownHelper.getObjectByJsonData(_data, helperObject));
  84. return;
  85. }
  86. });
  87. } else {
  88. bandsintownFactory.getEventsFromArtistByLocation(requestObject)
  89. .then(function (_data) {
  90. if (_data) {
  91. apingController.concatToResults(apingBandsintownHelper.getObjectByJsonData(_data, helperObject));
  92. return;
  93. }
  94. });
  95. }
  96. } else {
  97. bandsintownFactory.getEventsFromArtist(requestObject)
  98. .then(function (_data) {
  99. if (_data) {
  100. apingController.concatToResults(apingBandsintownHelper.getObjectByJsonData(_data, helperObject));
  101. }
  102. });
  103. }
  104. }
  105. });
  106. }
  107. }
  108. }]);;"use strict";
  109. jjtApingBandsintown.service('apingBandsintownHelper', ['apingModels', 'apingTimeHelper', 'apingUtilityHelper', function (apingModels, apingTimeHelper, apingUtilityHelper) {
  110. this.getThisPlattformString = function () {
  111. return "bandsintown";
  112. };
  113. this.getThisPlatformLink = function () {
  114. return "http://bandsintown.com/";
  115. };
  116. this.compareStrings = function (_string1, _string2) {
  117. return _string1.toLowerCase().trim().replace(/ /g, "") === _string2.toLowerCase().trim().replace(/ /g, "");
  118. };
  119. this.getArtistFromArray = function (_array, _artistName) {
  120. var returnObject = {
  121. artist_name: undefined,
  122. artist_id: undefined,
  123. artist_link: undefined,
  124. img_url: undefined,
  125. };
  126. var found = false;
  127. if (_array.length > 0) {
  128. var _this = this;
  129. angular.forEach(_array, function (value, key) {
  130. if (typeof value.name !== "undefined" && !found) {
  131. if (_this.compareStrings(value.name, _artistName)) {
  132. returnObject = {
  133. artist_name: value.name,
  134. artist_id: value.mbid || undefined,
  135. artist_link: value.website || value.facebook_tour_dates_url || undefined,
  136. img_url: value.image_url || value.thumb_url || undefined,
  137. };
  138. found = true;
  139. }
  140. }
  141. });
  142. if(!found) {
  143. returnObject = {
  144. artist_name: _array[0].name,
  145. artist_id: _array[0].mbid || undefined,
  146. artist_link: _array[0].website || _array[0].facebook_tour_dates_url || undefined,
  147. img_url: _array[0].image_url || _array[0].thumb_url || undefined,
  148. };
  149. }
  150. }
  151. return returnObject;
  152. };
  153. this.getObjectByJsonData = function (_data, _helperObject) {
  154. var requestResults = [];
  155. if (_data) {
  156. var _this = this;
  157. angular.forEach(_data.data, function (value, key) {
  158. if (typeof _helperObject.items === "undefined" || (_helperObject.items > 0 && requestResults.length < _helperObject.items)) {
  159. var tempResult;
  160. if (_helperObject.getNativeData === true || _helperObject.getNativeData === "true") {
  161. tempResult = value;
  162. } else {
  163. tempResult = _this.getItemByJsonData(value, _helperObject);
  164. }
  165. if (tempResult) {
  166. requestResults.push(tempResult);
  167. }
  168. }
  169. });
  170. }
  171. return requestResults;
  172. };
  173. this.getItemByJsonData = function (_item, _helperObject) {
  174. var returnObject = {};
  175. if (_item && _helperObject.model) {
  176. switch (_helperObject.model) {
  177. case "event":
  178. returnObject = this.getEventItemByJsonData(_item, _helperObject);
  179. break;
  180. default:
  181. return false;
  182. }
  183. }
  184. return returnObject;
  185. };
  186. this.getEventItemByJsonData = function (_item, _helperObject) {
  187. var eventObject = apingModels.getNew("event", this.getThisPlattformString());
  188. $.extend(true, eventObject, {
  189. start_timestamp: _item.datetime ? new Date(_item.datetime).getTime() : undefined,
  190. start_date_time: _item.datetime ? new Date(_item.datetime) : undefined,
  191. event_url: _item.facebook_rsvp_url || undefined, //URL to the event
  192. ticket_url: _item.ticket_url || undefined, //URL to the ticket
  193. intern_id: _item.id || undefined, // INTERN ID of event
  194. caption: _item.title || undefined,
  195. source: _item.artists || undefined,
  196. sold_out: _item.ticket_status ? _item.ticket_status == "unavailable" : undefined,
  197. });
  198. if(_item.artists && _item.artists.length > 0) {
  199. var artistTempObject = this.getArtistFromArray(_item.artists, _helperObject.artist);
  200. if(typeof artistTempObject.artist_name !== "undefined") {
  201. eventObject.artist_name = artistTempObject.artist_name;
  202. }
  203. if(typeof artistTempObject.artist_id !== "undefined") {
  204. eventObject.artist_id = artistTempObject.artist_id;
  205. }
  206. if(typeof artistTempObject.artist_link !== "undefined") {
  207. eventObject.artist_link = artistTempObject.artist_link;
  208. }
  209. if(typeof artistTempObject.img_url !== "undefined" && _helperObject.showAvatar) {
  210. eventObject.img_url = artistTempObject.img_url;
  211. }
  212. }
  213. if(typeof _item.venue !== "undefined") {
  214. eventObject.place_name = _item.venue.name || undefined;
  215. eventObject.city = _item.venue.city || undefined;
  216. eventObject.country = _item.venue.country || undefined;
  217. eventObject.latitude = _item.venue.latitude || undefined;
  218. eventObject.longitude = _item.venue.longitude || undefined;
  219. }
  220. return eventObject;
  221. };
  222. }]);;"use strict";
  223. angular.module("jtt_bandsintown", [])
  224. .factory('bandsintownFactory', ['$http', 'bandsintownSearchDataService', function ($http, bandsintownSearchDataService) {
  225. var bandsintownFactory = {};
  226. bandsintownFactory.getArtist = function (_params) {
  227. var bandsintownSearchData = bandsintownSearchDataService.getNew("artist", _params);
  228. return $http.jsonp(
  229. bandsintownSearchData.url,
  230. {
  231. method: 'GET',
  232. params: bandsintownSearchData.object,
  233. }
  234. );
  235. };
  236. bandsintownFactory.getEventsFromArtist = function (_params) {
  237. var bandsintownSearchData = bandsintownSearchDataService.getNew("eventsFromArtist", _params);
  238. return $http.jsonp(
  239. bandsintownSearchData.url,
  240. {
  241. method: 'GET',
  242. params: bandsintownSearchData.object,
  243. }
  244. );
  245. };
  246. bandsintownFactory.getEventsFromArtistByLocation = function (_params) {
  247. var bandsintownSearchData = bandsintownSearchDataService.getNew("eventsFromArtistByLocation", _params);
  248. return $http.jsonp(
  249. bandsintownSearchData.url,
  250. {
  251. method: 'GET',
  252. params: bandsintownSearchData.object,
  253. }
  254. );
  255. };
  256. bandsintownFactory.getRecommendedEventsFromArtistByLocation = function (_params) {
  257. var bandsintownSearchData = bandsintownSearchDataService.getNew("recommendedEventsFromArtistByLocation", _params);
  258. return $http.jsonp(
  259. bandsintownSearchData.url,
  260. {
  261. method: 'GET',
  262. params: bandsintownSearchData.object,
  263. }
  264. );
  265. };
  266. return bandsintownFactory;
  267. }])
  268. .service('bandsintownSearchDataService', function () {
  269. this.getApiBaseUrl = function (_params) {
  270. return 'http://api.bandsintown.com/';
  271. };
  272. this.fillDataInObjectByList = function(_object, _params, _list) {
  273. angular.forEach(_list, function (value, key) {
  274. if(typeof _params[value] !== "undefined") {
  275. _object.object[value] = _params[value];
  276. }
  277. });
  278. return _object;
  279. };
  280. this.getNew = function (_type, _params) {
  281. var bandsintownSearchData = {
  282. object: {
  283. format:"json",
  284. api_version: "2.0",
  285. app_id:_params.app_id || "angular-bandsintown-api-factory",
  286. callback: "JSON_CALLBACK",
  287. },
  288. url: "",
  289. };
  290. if (typeof _params.limit !== "undefined") {
  291. bandsintownSearchData.object.limit = _params.limit;
  292. }
  293. switch (_type) {
  294. case "artist":
  295. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  296. 'artist_id'
  297. ]);
  298. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+".json";
  299. break;
  300. case "eventsFromArtist":
  301. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  302. 'date', 'artist_id'
  303. ]);
  304. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events.json";
  305. break;
  306. case "eventsFromArtistByLocation":
  307. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  308. 'date', 'artist_id', 'location', 'radius'
  309. ]);
  310. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events/search.json";
  311. break;
  312. case "recommendedEventsFromArtistByLocation":
  313. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  314. 'date', 'artist_id', 'location', 'radius', 'only_recs'
  315. ]);
  316. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events/recommended";
  317. break;
  318. }
  319. return bandsintownSearchData;
  320. };
  321. });