Advertisement
Guest User

wikipediaIntent.js

a guest
Mar 21st, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. 'use strict';
  2. var https = require('https');
  3.  
  4. /**
  5. * URL prefix to download history content from Wikipedia
  6. */
  7. var urlPrefix = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&explaintext=&exsectionformat=plain&redirects=&titles=';
  8.  
  9. /**
  10. * Variable defining number of events to be read at one time
  11. */
  12. var paginationSize = 3;
  13.  
  14. /**
  15. * Variable defining the length of the delimiter between events
  16. */
  17. var delimiterSize = 2;
  18.  
  19.  
  20.  
  21.  
  22. /**
  23. * Gets a poster prepares the speech to reply to the user.
  24. */
  25. function handleFirstEventRequest(intent, session, response) {
  26. var daySlot = intent.slots.day;
  27. var repromptText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?";
  28. var monthNames = ["January", "February", "March", "April", "May", "June",
  29. "July", "August", "September", "October", "November", "December"
  30. ];
  31. var sessionAttributes = {};
  32. // Read the first 3 events, then set the count to 3
  33. sessionAttributes.index = paginationSize;
  34. var date = "";
  35.  
  36. // If the user provides a date, then use that, otherwise use today
  37. // The date is in server time, not in the user's time zone. So "today" for the user may actually be tomorrow
  38. if (daySlot && daySlot.value) {
  39. date = new Date(daySlot.value);
  40. } else {
  41. date = new Date();
  42. }
  43.  
  44. var prefixContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
  45. var cardContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
  46. var cardTitle = "Events on " + monthNames[date.getMonth()] + " " + date.getDate();
  47.  
  48. getJsonEventsFromWikipedia(monthNames[date.getMonth()], date.getDate(), function (events) {
  49. var speechText = "",
  50. i;
  51. sessionAttributes.text = events;
  52. session.attributes = sessionAttributes;
  53. if (events.length == 0) {
  54. speechText = "There is a problem connecting to Wikipedia at this time. Please try again later.";
  55. cardContent = speechText;
  56. response.tell(speechText);
  57. } else {
  58. for (i = 0; i < paginationSize; i++) {
  59. cardContent = cardContent + events[i];
  60. speechText = speechText + events[i];
  61. }
  62. speechText = speechText + "Wanna go deeper in history?";
  63. var speechOutput = prefixContent + speechText;
  64. var repromptOutput = repromptText;
  65. response.askWithCard(speechOutput, cardTitle, cardContent);
  66. }
  67. });
  68. }
  69.  
  70. /**
  71. * Gets a poster prepares the speech to reply to the user.
  72. */
  73. function handleNextEventRequest(intent, session, response) {
  74. var cardTitle = "More events on this day in history",
  75. sessionAttributes = session.attributes,
  76. result = sessionAttributes.text,
  77. speechText = "",
  78. cardContent = "",
  79. repromptText = "Do you want to know more about what happened on this date?",
  80. i;
  81. if (!result) {
  82. speechText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?";
  83. cardContent = speechText;
  84. } else if (sessionAttributes.index >= result.length) {
  85. speechText = "There are no more events for this date. Try another date by saying get events for august thirtieth.";
  86. cardContent = "There are no more events for this date. Try another date by saying, get events for august thirtieth.";
  87. } else {
  88. for (i = 0; i < paginationSize; i++) {
  89. if (sessionAttributes.index>= result.length) {
  90. break;
  91. }
  92. speechText = speechText + result[sessionAttributes.index];
  93. cardContent = cardContent + result[sessionAttributes.index];
  94. sessionAttributes.index++;
  95. }
  96. if (sessionAttributes.index < result.length) {
  97. speechText = speechText + " Wanna go deeper in history?";
  98. cardContent = cardContent + " Wanna go deeper in history?";
  99. }
  100. }
  101. var speechOutput = speechText;
  102. var repromptOutput = repromptText;
  103. response.askWithCard(speechOutput, repromptOutput, cardTitle, cardContent);
  104. }
  105.  
  106. function getJsonEventsFromWikipedia(day, date, eventCallback) {
  107. var url = urlPrefix + day + '_' + date;
  108. console.log("yippee requestId: " + url);
  109.  
  110. https.get(url, function(res) {
  111. var body = '';
  112.  
  113. res.on('data', function (chunk) {
  114. body += chunk;
  115. });
  116.  
  117. res.on('end', function () {
  118. var stringResult = parseJson(body);
  119. eventCallback(stringResult);
  120. });
  121. }).on('error', function (e) {
  122. console.log("Got error: ", e);
  123. });
  124. }
  125.  
  126. function parseJson(inputText) {
  127. // sizeOf (/nEvents/n) is 10
  128. var text = inputText.substring(inputText.indexOf("\\nEvents\\n")+10, inputText.indexOf("\\n\\n\\nBirths")),
  129. retArr = [],
  130. retString = "",
  131. endIndex,
  132. startIndex = 0;
  133.  
  134. if (text.length == 0) {
  135. return retArr;
  136. }
  137.  
  138. while(true) {
  139. endIndex = text.indexOf("\\n", startIndex+delimiterSize);
  140. var eventText = (endIndex == -1 ? text.substring(startIndex) : text.substring(startIndex, endIndex));
  141. // replace dashes returned in text from Wikipedia's API
  142. eventText = eventText.replace(/\\u2013\s*/g, '');
  143. // add comma after year so Alexa pauses before continuing with the sentence
  144. eventText = eventText.replace(/(^\d+)/,'$1,');
  145. eventText = 'In ' + eventText;
  146. startIndex = endIndex+delimiterSize;
  147. retArr.push(eventText);
  148. if (endIndex == -1) {
  149. break;
  150. }
  151. }
  152. if (retString != "") {
  153. retArr.push(retString);
  154. }
  155. retArr.reverse();
  156. return retArr;
  157. }
  158. module.exports = handleFirstEventRequest;
  159. module.exports = handleNextEventRequest;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement