PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Mirros/jsdelivr
JavaScript | 388 lines | 312 code | 66 blank | 10 comment | 84 complexity | a456ec997dd63392130c685fe0d85d6f MD5 | raw file
  1. /**
  2. @name: aping-plugin-bandsintown
  3. @version: 0.7.8 (28-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. 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. angular.module("jtt_aping_bandsintown")
  110. .service('apingBandsintownHelper', ['apingModels', 'apingTimeHelper', 'apingUtilityHelper', function (apingModels, apingTimeHelper, apingUtilityHelper) {
  111. this.getThisPlattformString = function () {
  112. return "bandsintown";
  113. };
  114. this.getThisPlatformLink = function () {
  115. return "http://bandsintown.com/";
  116. };
  117. this.compareStrings = function (_string1, _string2) {
  118. return _string1.toLowerCase().trim().replace(/ /g, "") === _string2.toLowerCase().trim().replace(/ /g, "");
  119. };
  120. this.getArtistFromArray = function (_array, _artistName) {
  121. var returnObject = {
  122. artist_name: undefined,
  123. artist_id: undefined,
  124. artist_link: undefined,
  125. img_url: undefined,
  126. };
  127. var found = false;
  128. if (_array.length > 0) {
  129. var _this = this;
  130. angular.forEach(_array, function (value, key) {
  131. if (typeof value.name !== "undefined" && !found) {
  132. if (_this.compareStrings(value.name, _artistName)) {
  133. returnObject = {
  134. artist_name: value.name,
  135. artist_id: value.mbid || undefined,
  136. artist_link: value.website || value.facebook_tour_dates_url || undefined,
  137. img_url: value.image_url || value.thumb_url || undefined,
  138. };
  139. found = true;
  140. }
  141. }
  142. });
  143. if (!found) {
  144. returnObject = {
  145. artist_name: _array[0].name,
  146. artist_id: _array[0].mbid || undefined,
  147. artist_link: _array[0].website || _array[0].facebook_tour_dates_url || undefined,
  148. img_url: _array[0].image_url || _array[0].thumb_url || undefined,
  149. };
  150. }
  151. }
  152. return returnObject;
  153. };
  154. this.getObjectByJsonData = function (_data, _helperObject) {
  155. var requestResults = [];
  156. if (_data) {
  157. var _this = this;
  158. angular.forEach(_data.data, function (value, key) {
  159. if (typeof _helperObject.items === "undefined" || (_helperObject.items > 0 && requestResults.length < _helperObject.items)) {
  160. var tempResult;
  161. if (_helperObject.getNativeData === true || _helperObject.getNativeData === "true") {
  162. tempResult = value;
  163. } else {
  164. tempResult = _this.getItemByJsonData(value, _helperObject);
  165. }
  166. if (tempResult) {
  167. requestResults.push(tempResult);
  168. }
  169. }
  170. });
  171. }
  172. return requestResults;
  173. };
  174. this.getItemByJsonData = function (_item, _helperObject) {
  175. var returnObject = {};
  176. if (_item && _helperObject.model) {
  177. switch (_helperObject.model) {
  178. case "event":
  179. returnObject = this.getEventItemByJsonData(_item, _helperObject);
  180. break;
  181. default:
  182. return false;
  183. }
  184. }
  185. return returnObject;
  186. };
  187. this.getEventItemByJsonData = function (_item, _helperObject) {
  188. var eventObject = apingModels.getNew("event", this.getThisPlattformString());
  189. angular.extend(eventObject, {
  190. start_timestamp: _item.datetime ? new Date(_item.datetime).getTime() : undefined,
  191. start_date_time: _item.datetime ? new Date(_item.datetime) : undefined,
  192. event_url: _item.facebook_rsvp_url || undefined, //URL to the event
  193. ticket_url: _item.ticket_url || undefined, //URL to the ticket
  194. intern_id: _item.id || undefined, // INTERN ID of event
  195. caption: _item.title || undefined,
  196. source: _item.artists || undefined,
  197. sold_out: _item.ticket_status ? _item.ticket_status === "unavailable" : undefined,
  198. });
  199. if (_item.artists && _item.artists.length > 0) {
  200. var artistTempObject = this.getArtistFromArray(_item.artists, _helperObject.artist);
  201. if (typeof artistTempObject.artist_name !== "undefined") {
  202. eventObject.artist_name = artistTempObject.artist_name;
  203. }
  204. if (typeof artistTempObject.artist_id !== "undefined") {
  205. eventObject.artist_id = artistTempObject.artist_id;
  206. }
  207. if (typeof artistTempObject.artist_link !== "undefined") {
  208. eventObject.artist_link = artistTempObject.artist_link;
  209. }
  210. if (typeof artistTempObject.img_url !== "undefined" && _helperObject.showAvatar) {
  211. eventObject.img_url = artistTempObject.img_url;
  212. }
  213. }
  214. if (typeof _item.venue !== "undefined") {
  215. eventObject.place_name = _item.venue.name || undefined;
  216. eventObject.city = _item.venue.city || undefined;
  217. eventObject.country = _item.venue.country || undefined;
  218. eventObject.latitude = _item.venue.latitude || undefined;
  219. eventObject.longitude = _item.venue.longitude || undefined;
  220. }
  221. return eventObject;
  222. };
  223. }]);;"use strict";
  224. angular.module("jtt_bandsintown", [])
  225. .factory('bandsintownFactory', ['$http', 'bandsintownSearchDataService', function ($http, bandsintownSearchDataService) {
  226. var bandsintownFactory = {};
  227. bandsintownFactory.getArtist = function (_params) {
  228. var bandsintownSearchData = bandsintownSearchDataService.getNew("artist", _params);
  229. return $http.jsonp(
  230. bandsintownSearchData.url,
  231. {
  232. method: 'GET',
  233. params: bandsintownSearchData.object,
  234. }
  235. );
  236. };
  237. bandsintownFactory.getEventsFromArtist = function (_params) {
  238. var bandsintownSearchData = bandsintownSearchDataService.getNew("eventsFromArtist", _params);
  239. return $http.jsonp(
  240. bandsintownSearchData.url,
  241. {
  242. method: 'GET',
  243. params: bandsintownSearchData.object,
  244. }
  245. );
  246. };
  247. bandsintownFactory.getEventsFromArtistByLocation = function (_params) {
  248. var bandsintownSearchData = bandsintownSearchDataService.getNew("eventsFromArtistByLocation", _params);
  249. return $http.jsonp(
  250. bandsintownSearchData.url,
  251. {
  252. method: 'GET',
  253. params: bandsintownSearchData.object,
  254. }
  255. );
  256. };
  257. bandsintownFactory.getRecommendedEventsFromArtistByLocation = function (_params) {
  258. var bandsintownSearchData = bandsintownSearchDataService.getNew("recommendedEventsFromArtistByLocation", _params);
  259. return $http.jsonp(
  260. bandsintownSearchData.url,
  261. {
  262. method: 'GET',
  263. params: bandsintownSearchData.object,
  264. }
  265. );
  266. };
  267. return bandsintownFactory;
  268. }])
  269. .service('bandsintownSearchDataService', function () {
  270. this.getApiBaseUrl = function (_params) {
  271. return 'http://api.bandsintown.com/';
  272. };
  273. this.fillDataInObjectByList = function(_object, _params, _list) {
  274. angular.forEach(_list, function (value, key) {
  275. if(typeof _params[value] !== "undefined") {
  276. _object.object[value] = _params[value];
  277. }
  278. });
  279. return _object;
  280. };
  281. this.getNew = function (_type, _params) {
  282. var bandsintownSearchData = {
  283. object: {
  284. format:"json",
  285. api_version: "2.0",
  286. app_id:_params.app_id || "angular-bandsintown-api-factory",
  287. callback: "JSON_CALLBACK",
  288. },
  289. url: "",
  290. };
  291. if (typeof _params.limit !== "undefined") {
  292. bandsintownSearchData.object.limit = _params.limit;
  293. }
  294. switch (_type) {
  295. case "artist":
  296. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  297. 'artist_id'
  298. ]);
  299. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+".json";
  300. break;
  301. case "eventsFromArtist":
  302. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  303. 'date', 'artist_id'
  304. ]);
  305. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events.json";
  306. break;
  307. case "eventsFromArtistByLocation":
  308. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  309. 'date', 'artist_id', 'location', 'radius'
  310. ]);
  311. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events/search.json";
  312. break;
  313. case "recommendedEventsFromArtistByLocation":
  314. bandsintownSearchData = this.fillDataInObjectByList(bandsintownSearchData, _params, [
  315. 'date', 'artist_id', 'location', 'radius', 'only_recs'
  316. ]);
  317. bandsintownSearchData.url = this.getApiBaseUrl()+"artists/"+_params.artist+"/events/recommended";
  318. break;
  319. }
  320. return bandsintownSearchData;
  321. };
  322. });