-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
63 lines (52 loc) · 1.53 KB
/
sketch.js
File metadata and controls
63 lines (52 loc) · 1.53 KB
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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Wikipedia
// Edited Video: https://youtu.be/RPz75gcHj18
let searchUrl = 'https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=';
let contentUrl = 'https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=';
let userInput;
let counter = 0;
function setup() {
noCanvas();
userInput = select('#userinput');
userInput.changed(startSearch);
goWiki(userInput.value());
function startSearch() {
counter = 0;
goWiki(userInput.value());
}
function goWiki(term) {
counter = counter + 1;
if (counter < 10) {
//let term = userInput.value();
let url = searchUrl + term;
loadJSON(url, gotSearch, 'jsonp');
}
}
function gotSearch(data) {
console.log(data);
let len = data[1].length;
let index = floor(random(len));
let title = data[1][index];
title = title.replace(/\s+/g, '_');
createDiv(title);
console.log('Querying: ' + title);
let url = contentUrl + title;
loadJSON(url, gotContent, 'jsonp');
}
function gotContent(data) {
console.log(data);
let page = data.query.pages;
let pageId = Object.keys(data.query.pages)[0];
console.log(pageId);
let content = page[pageId].revisions[0]['*'];
console.log(content);
let wordRegex = 'born';
let words = content.match(wordRegex);
let word = random(words);
console.log(word);
//let birth = page[word].revisions[0]['*'];
//console.log(birth);
}
}