PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/in_theaters/in_theaters.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 130 lines | 102 code | 13 blank | 15 comment | 24 complexity | 1ba9e0508175574606386d519b4030a6 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function(env) {
  2. "use strict";
  3. // A change in the Rotten Tomatoes API returns images that end in _tmb.
  4. // This changes this to _det.
  5. function toDetail(img) {
  6. if(/resizing\.flixster\.com/.test(img)) {
  7. // Everything before the size of the image can be removed and it would still work.
  8. img = img.replace(/.+\/\d+x\d+\/(.+)/, "http://$1");
  9. // Better use the _det size (which is smaller) instead of the _ori size.
  10. return img.replace(/_ori/, "_det");
  11. }
  12. // Otherwise, use the old string replacement strategy.
  13. return img.replace(/tmb\.(jpg|png)/, "det.$1");
  14. }
  15. function get_image(critics_rating) {
  16. if(!critics_rating) {
  17. return;
  18. }
  19. // The filename is the same as the critics_rating, but
  20. // lowercased and with spaces converted to dashes.
  21. critics_rating = critics_rating.toLowerCase().replace(/ /, '-');
  22. return DDG.get_asset_path('in_theaters', critics_rating + ((DDG.is3x || DDG.is2x) ? '.retina.png' : '.png'));
  23. }
  24. env.ddg_spice_in_theaters = function(api_result) {
  25. if(!api_result || api_result.error) {
  26. return Spice.failed('in_theaters');
  27. }
  28. var mod_api_result = [];
  29. var filter_rating;
  30. var query_array = DDG.get_query().toLowerCase().split(" ");
  31. var ratings = ["r","pg-13","pg","g","pg13","unrated"];
  32. // Check whether our query contains any rating
  33. $.each(ratings, function(index, value) {
  34. if(($.inArray(value, query_array)) !== -1) {
  35. if(value === "pg13") {
  36. filter_rating = "pg-13";
  37. } else {
  38. filter_rating = value;
  39. }
  40. }
  41. });
  42. Spice.add({
  43. id: 'in_theaters',
  44. name: 'Movies',
  45. data: api_result.movies,
  46. signal: 'high',
  47. meta: {
  48. sourceName: 'Rotten Tomatoes',
  49. sourceUrl: 'http://www.rottentomatoes.com/movie/in-theaters/',
  50. total: api_result.movies,
  51. itemType: 'Movies'
  52. },
  53. normalize: function(item) {
  54. if (filter_rating && item.mpaa_rating.toLowerCase() !== filter_rating) {
  55. return null;
  56. }
  57. var position;
  58. // We add these so that we can position the Rotten Tomatoes images.
  59. if(item.ratings.critics_rating === "Fresh" || item.ratings.critics_rating === "Certified Fresh") {
  60. position = "-256px -144px";
  61. } else if(item.ratings.critics_rating === "Rotten") {
  62. position = "-272px -144px";
  63. }
  64. // Modify the image from _tmb.jpg to _det.jpg
  65. var image = toDetail(item.posters.detailed)
  66. if(item.alternate_ids && item.alternate_ids.imdb) {
  67. return {
  68. rating: item.ratings.critics_score >= 0 ? item.ratings.critics_score / 20 : 0,
  69. //image: image,
  70. icon_image: get_image(item.ratings.critics_rating),
  71. abstract: Handlebars.helpers.ellipsis(item.synopsis, 200),
  72. heading: item.title,
  73. //img: image,
  74. //img_m: image,
  75. url: item.links.alternate,
  76. is_retina: ((DDG.is3x || DDG.is2x) ? 'is_retina' : 'no_retina')
  77. };
  78. }
  79. },
  80. templates: {
  81. group: 'movies',
  82. options: {
  83. subtitle_content: Spice.in_theaters.subtitle_content,
  84. rating: false,
  85. buy: Spice.in_theaters.buy
  86. }
  87. },
  88. onItemShown: function(item) {
  89. $.ajaxSetup({ cache: true });
  90. if(item.alternate_ids && item.alternate_ids.imdb) {
  91. $.getJSON("/js/spice/movie_image/tt" + item.alternate_ids.imdb, function(data) {
  92. if(data && data.movie_results && data.movie_results.length > 0 && data.movie_results[0].poster_path) {
  93. var image = "https://image.tmdb.org/t/p/w185" + data.movie_results[0].poster_path;
  94. item.$html.find(".tile__media__img").attr("src", "https://images.duckduckgo.com/iu/?f=1&u=" + encodeURIComponent(image));
  95. $.extend(item, {
  96. image: image,
  97. img: image,
  98. img_m: image
  99. });
  100. }
  101. });
  102. }
  103. }
  104. });
  105. }
  106. // Convert minutes to hr. min. format.
  107. // e.g. {{time 90}} will return 1 hr. 30 min.
  108. Handlebars.registerHelper("InTheaters_time", function(runtime) {
  109. var hours = '',
  110. minutes = runtime;
  111. if(runtime >= 60) {
  112. hours = Math.floor(runtime / 60) + ' hr. ';
  113. minutes = (runtime % 60);
  114. }
  115. return hours + (minutes > 0 ? minutes + ' min.' : '');
  116. });
  117. }(this));