javascript - Get Wikipedia Article excluding ones with Title in a list -


so want random wikipedia article don't want grab ones title or category in specific list have (for bad word filtering).

i using javascript , i'm not familiar wikipedia api have query string generate random article , grab extract i'm not sure on how excluding. didn't see in documentation or searching google on how that.

the code working , fetching random articles need filter them.

my actual javascript code fetching

if (tempscript) return;         if (!isretry) {             attempts = 0;             minchars = minimumcharacters;             maxchars = maximumcharacters;             button.disabled = true;             button.style.cursor = "wait";         }         tempscript = document.createelement("script");         tempscript.type = "text/javascript";         tempscript.id = "tempscript";         tempscript.src = "http://en.wikipedia.org/w/api.php" + "?action=query&generator=random&prop=extracts" + "&exchars=" + maxchars + "&format=json&callback=oncomplete&requestid=" + math.floor(math.random() * 999999).tostring();         document.body.appendchild(tempscript); 

you should change url inclide categories in prop, , set cllimit maximum of 500:

tempscript.src = "http://en.wikipedia.org/w/api.php" + "?action=query&generator=random&prop=categories|extracts&cllimit=500&exchars=" + maxchars + "&format=json&callback=oncomplete&requestid=" + math.floor(math.random() * 999999).tostring(); 

then, if page has categories, list them in returned json object.

in callback function, need following:

var badarticles = ['poop', 'pee', 'underpants'],     badcategories = ['images of poop', 'images of pee', 'images of underpants'],     page = response.query.pages; (var in page) {     page = page[i]; // `i` pageid in loop     break; // don't want loop continue within new `page` object }  //exit callback when pagename in bad articles list  if (badarticles.indexof(page.title) !== -1) return false;  if (page.categories) {     (var i=0;i<page.categories.length;i++) {          //exit callback when pagename has category in bad categories list         if (badcategories.indexof(page.categories[i].title)) return false;      } } 

that should work. did not test it, expect work, based on format of mediawiki api's response. if doesn't work, please leave comment.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -