-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikicite.js
79 lines (65 loc) · 1.77 KB
/
wikicite.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
/****************************************
* Enter a search term for Wikipedia
* submit search term and navigate to that page
* Capture the results of the citations at the bottom in the reference section
* output results in a JSON file
*
* Requires CasperJS and PhantomJS
*****************************************/
var casper = require("casper").create({
verbose: true,
logLevel: 'error', // debug, info, warning, error
pageSettings: {
loadImages: false,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
},
clientScripts: ["vendor/jquery.min.js", "vendor/lodash.js"]
});
var fs = require('fs');
var url = 'https://en.wikipedia.org/wiki/Main_Page';
var searchKey = 'worm';
var title = [];
var link = [];
var output = [];
function outputJSON() {
output.push({
title: title,
link: link
});
return JSON.stringify(output);
};
function getTitle() {
var title = $('ol.references li a.external');
return _.map(title, function(e) {
return e.innerHTML;
});
};
function getLink() {
var link = $('ol.references li a.external');
return _.map(link, function(e){
return e.getAttribute('href');
});
};
casper.start(url, function() {
this.echo(this.getTitle());
});
casper.then(function() {
this.fill('form#searchform', {
search: searchKey
}, true);
});
casper.then(function() {
console.log('Search Successful, new page is ' + this.getTitle() + ' : ' + this.getCurrentUrl());
});
casper.then(function() {
title = this.evaluate(getTitle);
});
casper.then(function() {
link = this.evaluate(getLink);
});
casper.run(function() {
var data = outputJSON();
fs.write(searchKey + '.json', data, 'w');
this.echo("\n File Written Successfully").exit();
});