PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/seasons/seasons.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 59 lines | 47 code | 8 blank | 4 comment | 3 complexity | 11b33f33bab11978315d9c512d76cf14 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. "use strict";
  3. env.ddg_spice_seasons = function(api_result){
  4. // Make sure we have a result
  5. if (!(api_result &&
  6. api_result.holidays &&
  7. api_result.holidays.length == 4)) {
  8. return Spice.failed('seasons');
  9. }
  10. // Grab the matching search terms and figure out the season
  11. var script = $('[src*="/js/spice/seasons/"]')[0];
  12. var source = $(script).attr("src");
  13. var query = source.match(/seasons\/[0-9]{4}\/[a-zA-Z]{2}\/([a-z]+)/)[1];
  14. var season = $.inArray(query, [ "spring", "summer", "autumn", "winter" ]);
  15. // Somehow we got an unknown season, bail out
  16. if (season < 0) {
  17. return Spice.failed('seasons');
  18. }
  19. // Grab the correct season from api results and generate output data
  20. var event = api_result.holidays[season];
  21. var date = DDG.getDateFromString(event.date.iso);
  22. var result = {
  23. date: date,
  24. location: event.country.name,
  25. season: DDG.capitalize(query)
  26. };
  27. DDG.require("moment.js", function() {
  28. Spice.add({
  29. id: "seasons",
  30. name: "Seasons",
  31. data: result,
  32. meta: {
  33. sourceName: "timeanddate.com",
  34. sourceUrl: event.url
  35. },
  36. templates: {
  37. group: 'text',
  38. options: {
  39. moreAt: true
  40. }
  41. },
  42. normalize: function(item){
  43. return {
  44. title: moment(result.date).format("dddd, MMMM Do"),
  45. subtitle: result.location + " – First day of " + result.season + " in " + result.date.getFullYear()
  46. };
  47. }
  48. });
  49. });
  50. };
  51. }(this));