Skip to content

Instantly share code, notes, and snippets.

@reality
Last active December 12, 2017 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reality/201218e4aa309f8d03ca to your computer and use it in GitHub Desktop.
Save reality/201218e4aa309f8d03ca to your computer and use it in GitHub Desktop.
wedinos scrape
/***
Copyright (c) 2014 Luke Slater (reality)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
See https://wiki.tripsit.me/wiki/WEDINOS_Mirror
***/
var cheerio = require('cheerio'),
request = require('request'),
_ = require('underscore')._,
async = require('async'),
fs = require('fs'),
path = require('path');
var page = 1;
var total = [];
async.forever(function(next) {
request('http://www.wedinos.org/db/samples/index/page:' + page, function(err,response,body) {
if(page === 256) {
return next(true);
}
$ = cheerio.load(body, {'normalizeWhitespace': true, xmlMode: true});
var results = [];
$('div.sampleResultsTab').each(
function(index) {
$this = $(this);
results.push({
'id': $this.find('h3').text()
});
}
);
$('div.sampleResults').each(
function(index) {
$this = $(this);
var fields = {};
fields.image = $this.find('img').attr('src') || null;
$this.find('p').each(
function(index) {
$this = $(this);
var field = $this.find('span').text().replace(/\s\s/g,' ').replace(/\:/,'').trim();
if(field === '') {
field = 'Not Completed Reason';
}
$this.find('span').text('');
var content = $this.text().replace(/\s\s/g,' ').trim();
fields[field] = content;
}
);
results[index] = _.extend(results[index], fields);
console.log('--> Got ' + results[index].id);
}
);
total = total.concat(results);
page++;
next();
});
}, function() {
var newIds = [];
var newTotal = JSON.parse(fs.readFileSync('results.json', 'utf-8'));
_.each(total, function(elem) {
var i = elem.id.split(' ')[1];
newTotal[elem.id.split(' ')[1]] = elem;
newIds.push(elem.id.split(' ')[1]);
});
var deleted = _.filter(newTotal, function(elem) { return !_.include(newIds, elem.id.split(' ')[1]); });
_.each(deleted, function(item) {
newTotal[item.id.split(' ')[1]].deleted = true;
});
console.log('seems like ' + deleted.length + ' were deleted (wat)');
async.eachSeries(_.keys(newTotal), function(id, next) {
if(!newTotal[id].image) {
console.log('No image for ' + id);
return next();
}
var imgPath = newTotal[id].image,
imgName = _.last(imgPath.split('/'));
fs.exists('public/img/'+imgName, function(exists) {
if(!exists) {
console.log('Trying to download ' + id);
console.log(newTotal[id].image);
request.get(newTotal[id].image).pipe(fs.createWriteStream('public/img/'+imgName))
.on('close', function () {
console.log('--> Saved ' + imgName);
newTotal[id].image = 'img/'+imgName;
setTimeout(next, 1000);
});
} else {
console.log('Already got so setting ' + id);
newTotal[id].image = 'img/'+imgName;
return next();
}
});
}, function() {
fs.writeFileSync('results.json', JSON.stringify(newTotal, null, ' '));
console.log('--> Done');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment