PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/movie/spice.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 118 lines | 74 code | 24 blank | 20 comment | 28 complexity | eed4cf7fa696a950691ea365dd9b625f MD5 | raw file
Possible License(s): Apache-2.0
  1. var req = document.getElementById('search_form_input').value;
  2. function ddg_spice_movie(movie) {
  3. var result = req.replace(/^movie\s+/g,'');
  4. // validity check
  5. if (movie['total'] > 0 && movie['movies']) {
  6. // more than one result
  7. if (movie['total'] > 1) {
  8. // Default to first result incase nothing is more relevant
  9. result = movie["movies"][0];
  10. // check which movie title is most relevant
  11. for (var i = 0, aMovie; aMovie = movie.movies[i]; i++){
  12. if (DDG.isRelevant(aMovie.title, ["movie","film"]) ) {
  13. result = aMovie;
  14. break;
  15. }
  16. }
  17. } else {
  18. result = movie["movies"][0];
  19. }
  20. // Create snippet to be shown
  21. snippet = '';
  22. // Check presence of synopsis, and create element
  23. if (result.synopsis) synopsis = result.synopsis.substring(0,135) + "...";
  24. else if (result.critics_consensus && result.critics_consensus.length > 0) synopsis = result.critics_consensus.substring(0,135) + "...";
  25. else synopsis = '';
  26. var names = [];
  27. // Loop through abridged cast members, add to cast element
  28. for (var i=0; i < result.abridged_cast.length; i++){
  29. var pre = '';
  30. if ( i == result.abridged_cast.length - 1 && result.abridged_cast.length != 1 ) pre = 'and ';
  31. var name = result.abridged_cast[i].name;
  32. var url = 'http://www.rottentomatoes.com/celebrity/' + result.abridged_cast[i].id + '/';
  33. names.push(pre+'<a onlcick="fl=1" href="'+url+'">'+name+'</a>');
  34. }
  35. var cast = '';
  36. if (names.length > 1) cast = ', starring '+names.join(', ');
  37. // check for default poster
  38. var poster = result.posters.thumbnail;
  39. if (poster === 'http://images.rottentomatoescdn.com/images/redesign/poster_default.gif') poster = '';
  40. // Make title for header
  41. var header = result.title.substring(0,49);
  42. if (result.title.length > 50) header += '...';
  43. //Movie Score
  44. //var score = 'with an audience score of'+ result.audience_score +'%';
  45. var rating, audience, critics, reaction, releaseDate, tomato;
  46. var currentTime = new Date();
  47. // Is a release date planned?
  48. if (result.release_dates.theater) {
  49. var rDate = result.release_dates.theater.split("-");
  50. var opened = new Date(rDate[0], rDate[1]-1, rDate[2], 00, 00, 00); // Date uses month-1 notation
  51. rating = (currentTime - opened < 0) ? "an upcoming " : "";
  52. releaseDate = (rating) ? opened.toDateString().slice(4) : result.year;
  53. }
  54. else if (result.year > currentTime.getFullYear()) {
  55. rating = "an upcoming ";
  56. releaseDate = result.year;
  57. }
  58. reaction = (rating) ? " want to see)" : " approved)";
  59. // Who's reviewed it?
  60. audience = (result.ratings.audience_score === -1) ? "" : result.ratings.audience_score+ "% audience"+reaction;
  61. critics = (result.ratings.critics_score === -1) ? "" : result.ratings.critics_score+ "% critics, ";
  62. critics += (audience) ? "" : reaction;
  63. // Is it fresh or rotten?
  64. tomato = (result.ratings.critics_rating) ? result.ratings.critics_rating+", " : "";
  65. if (!rating){
  66. releaseDate = result.year;
  67. if (result.mpaa_rating === "R" || result.mpaa_rating === "NC-17" || result.mpaa_rating == "Unrated"){
  68. rating = "an ";
  69. } else {
  70. rating = "a ";
  71. }
  72. }
  73. rating += result.mpaa_rating;
  74. // Call nra function as per Spice Plugin Guidelines
  75. items = [[]];
  76. items[0]['a'] = result.title + ' ('+result.year+ ') is '
  77. +rating+ ' movie ('
  78. +tomato
  79. +critics
  80. +audience
  81. +cast + '. '
  82. +synopsis;
  83. items[0]['h'] = header+" ("+releaseDate+")";
  84. // Source name and url for the More at X link.
  85. items[0]['s'] = 'Rotten Tomatoes';
  86. items[0]['u'] = result.links.alternate;
  87. // Force vertical expansion (no scrollbar)
  88. // items[0]['f'] = 1;
  89. // Thumbnail url
  90. items[0]['i'] = poster;
  91. // The rendering function is nra.
  92. nra(items);
  93. }
  94. }