const date = require('date.js'); | |
const cheerio = require('cheerio'); | |
const { readFileSync } = require('fs'); | |
const html = readFileSync('active-popular.html', 'utf8'); | |
const parseFiction = ($, el) => ({ | |
image: $(el).find('img').attr('src'), | |
title: $(el).find('.fiction-title').children('a').text(), | |
id: $(el).find('.fiction-title').children('a').attr('href').split('/')[2], | |
type: $(el).find('span.label.bg-blue-hoki').text(), | |
tags: $(el).find('span.label.bg-blue-dark') | |
.map((i, el) => $(el).text()).get(), | |
}); | |
const parseLatest = (html) => { | |
const $ = cheerio.load(html); | |
const fictions = $('.fiction-list-item').map((i, el) => Object.assign({ | |
recent: $(el).find('li.list-item').map((i, el) => ({ | |
name: $(el).find('span.col-xs-8').text(), | |
created: date($(el).find('time').text()), | |
})).get(), | |
}, parseFiction($, el))).get(); | |
return fictions; | |
}; | |
const parsePopular = (html) => { | |
const parseStats = ($, el) => { | |
let stats = {}; | |
stats.rating = $(el).find('.star').attr('title'); | |
stats.latest = date($(el).find('time').attr('datetime')); | |
$(el).find('span').each((i, el) => { | |
const text = $(el).text().toLowerCase(); | |
const key = text.split(' ')[1]; | |
const value = parseInt(text.split(' ')[0], 10); | |
if (!key || !value) { return; } | |
stats[key] = value; | |
}); | |
return stats; | |
} | |
const $ = cheerio.load(html); | |
const fictions = $('.fiction-list-item').map((i, el) => { | |
return Object.assign(parseStats($, el), parseFiction($, el)); | |
}).get(); | |
return fictions; | |
} | |
console.log(JSON.stringify(parsePopular(html), 0, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment