-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiquote-api.js
243 lines (180 loc) · 6.43 KB
/
wikiquote-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var WikiquoteApi = (function() {
var wqa = {};
var API_URL = "https://it.wikiquote.org/w/api.php";
/**
* Query based on "titles" parameter and return page id.
* If multiple page ids are returned, choose the first one.
* Query includes "redirects" option to automatically traverse redirects.
* All words will be capitalized as this generally yields more consistent results.
*/
wqa.queryTitles = function(titles, success, error) {
var xhr = new XMLHttpRequest();
xhr.open("GET", ""+API_URL+"?format=json&action=query&titles="+titles+"&redirects=''", true);
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
// JSON.parse does not evaluate the attacker's scripts.
var result = JSON.parse(xhr.responseText);
var pages = result.query.pages;
var pageId = -1;
for(var p in pages) {
var page = pages[p];
// api can return invalid records, these are marked as "missing"
if(!("missing" in page)) {
pageId = page.pageid;
break;
}
}
if(pageId > 0) {
success(pageId);
} else {
error("No results for: "+titles+"");
}
//console.log('xhr' + pageId)
}
}
return xhr.send();
};
/**
* Get the sections for a given page.
* This makes parsing for quotes more manageable.
* Returns an array of all "1.x" sections as these usually contain the quotes.
* If no 1.x sections exists, returns section 1. Returns the titles that were used
* in case there is a redirect.
*/
wqa.getSectionsForPage = function(pageId, success, error) {
var xhr = new XMLHttpRequest();
xhr.open("GET", ""+API_URL+"?format=json&action=parse&prop=sections&pageid="+pageId+"", true);
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
// JSON.parse does not evaluate the attacker's scripts.
var result = JSON.parse(xhr.responseText);
var sectionArray = [];
var sections = result.parse.sections;
for(var s in sections) {
var splitNum = sections[s].number.split('.');
if(splitNum.length > 1 && splitNum[0] === "1") {
sectionArray.push(sections[s].index);
}
}
// Use section 1 if there are no "1.x" sections
if(sectionArray.length === 0) {
sectionArray.push("1");
}
success({ titles: result.parse.title, sections: sectionArray });
//console.log('xhr' + pageId)
}
}
return xhr.send();
};
/**
* Get all quotes for a given section. Most sections will be of the format:
* <h3> title </h3>
* <ul>
* <li>
* Quote text
* <ul>
* <li> additional info on the quote </li>
* </ul>
* </li>
* <ul>
* <ul> next quote etc... </ul>
*
* The quote may or may not contain sections inside <b /> tags.
*
* For quotes with bold sections, only the bold part is returned for brevity
* (usually the bold part is more well known).
* Otherwise the entire text is returned. Returns the titles that were used
* in case there is a redirect.
*/
wqa.getQuotesForSection = function(pageId, sectionIndex, success, error) {
var xhr = new XMLHttpRequest();
xhr.open("GET", ""+API_URL+"?format=json&action=parse&noimages=''&pageid="+pageId+"§ion="+sectionIndex+"", true);
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
// JSON.parse does not evaluate the attacker's scripts.
var result = JSON.parse(xhr.responseText);
var quotes = result.parse.text["*"];
var quoteArray = []
// Find top level <li> only
var $lis = $(quotes).find('li:not(li li)');
$lis.each(function() {
// Remove all children that aren't <b>
// $(this).children().remove(':not(b), :not(i)');
// var $bolds = $(this).find('b');
//
// // If the section has bold text, use it. Otherwise pull the plain text.
// if($bolds.length > 0) {
// quoteArray.push($bolds.html());
// } else {
// }
$(this).find('sup').remove()
// Remove all children that aren't <b>
$(this).find('a').each(function () {
$(this).attr('href', 'http://it.wikipedia.org'+$(this).attr('href'))
})
quoteArray.push($(this).html());
});
success({ titles: result.parse.title, quotes: quoteArray });
}
}
return xhr.send();
};
/**
* Search using opensearch api. Returns an array of search results.
*/
wqa.openSearch = function(titles, success, error) {
jQuery.ajax({
url: API_URL,
dataType: "jsonp",
data: {
format: "json",
action: "opensearch",
namespace: 0,
suggest: "",
search: titles
},
success: function(result, status){
success(result[1]);
},
error: function(xhr, result, status){
error("Error with opensearch for " + titles);
}
});
};
/**
* Get a random quote for the given title search.
* This function searches for a page id for the given title, chooses a random
* section from the list of sections for the page, and then chooses a random
* quote from that section. Returns the titles that were used in case there
* is a redirect.
*/
wqa.getRandomQuote = function(titles, success, error) {
var errorFunction = function(msg) {
error(msg);
};
var chooseQuote = function(quotes) {
var randomNum = Math.floor(Math.random()*quotes.quotes.length);
success({ titles: quotes.titles, quote: quotes.quotes[randomNum] });
};
var getQuotes = function(pageId, sections) {
var randomNum = Math.floor(Math.random()*sections.sections.length);
wqa.getQuotesForSection(pageId, sections.sections[randomNum], chooseQuote, errorFunction);
};
var getSections = function(pageId) {
wqa.getSectionsForPage(pageId, function(sections) { getQuotes(pageId, sections); }, errorFunction);
};
wqa.queryTitles(titles, getSections, errorFunction);
};
/**
* Capitalize the first letter of each word
*/
wqa.capitalizeString = function(input) {
var inputArray = input.split(' ');
var output = [];
for(s in inputArray) {
output.push(inputArray[s].charAt(0).toUpperCase() + inputArray[s].slice(1));
}
return output.join(' ');
};
return wqa;
}());