Skip to content

Instantly share code, notes, and snippets.

@Colouratura
Created June 3, 2017 00:46
Show Gist options
  • Save Colouratura/81095742b62e6d3112f885c1816d29c7 to your computer and use it in GitHub Desktop.
Save Colouratura/81095742b62e6d3112f885c1816d29c7 to your computer and use it in GitHub Desktop.
( function ( $ ) {
var conflicts = [
[ 'Monster', 'Ghosts', 'Demon/Devil', 'Beings', 'Gods', 'Cryptids' ],
[ 'Books', 'Diary/Journal', 'Items/Objects' ],
[ 'Lost Episodes', 'Television' ],
[ 'Pokemon', 'Zelda', 'Video Games' ],
[ 'Holders', 'Ritual' ],
[ 'Holders', 'Items/Objects' ],
[ 'Holders', 'Places' ],
[ 'HPL', 'Lovecraftian', 'Suggested Reading' ]
];
var containsMultiple = function ( categories, conflictagories ) {
var contains = 0;
categories.forEach( function ( item ) {
if ( conflictagories.includes( item ) )
contains++;
} );
return ( contains > 2 );
};
var getCategories = function ( page ) {
return new Promise( function ( resolve, reject ) {
var uri = 'http://creepypasta.wikia.com/api.php',
url = uri + '?action=query&titles=' + encodeURIComponent( page ) + '&prop=categories&format=json';
$.get( url )
.then( function ( data ) {
if ( data.error ) reject( data );
data = data.query.pages;
for ( var id in data ) {
if ( typeof data[ id ].categories !== undefined )
resolve( data[ id ].categories );
else
resolve( [ 0 ] );
}
} )
.fail( function ( error ) {
reject( error );
} );
} );
};
var checkConflictagories = function ( pages ) {
if ( pages.length > -1 ) {
pages.forEach( function ( page ) {
setTimeout( function () {
getCategories( page )
.then( function ( cats ) {
try {
cats = cats.map( function ( n ) {
return n.title.split( ':' )[ 1 ];
} );
conflicts.forEach( function ( confSet ) {
if ( cats.length > -1 && containsMultiple( cats, confSet ) ) {
console.log( page + ' has conflictagories!' );
}
} );
} catch ( e ) { }
} )
.catch( function ( err ) {
if ( typeof err.status === 'function' )
console.log( err.status() );
else
console.log( err );
} );
}, 1000 );
} );
}
};
var getAllPages = function ( cont ) {
let uri = 'http://creepypasta.wikia.com/api.php',
url = uri + '?action=query&list=allpages&apfrom=A&aplimit=500&apnamespace=0&format=json';
if ( cont ) {
url += '&apfrom=' + cont;
}
$.get( url )
.then( function ( data ) {
console.log( data );
var pages = data.query.allpages.map( function ( n ) {
return n.title;
} );
checkConflictagories( pages );
setTimeout( function () {
getAllPages( data[ 'query-continue' ].allpages.apfrom );
}, 60000 );
} )
.fail( function ( err ) {
console.log( err.status() );
} );
};
getAllPages();
}( $ ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment