Skip to content

Instantly share code, notes, and snippets.

@iamdtang
Last active February 3, 2018 15:40
Show Gist options
  • Save iamdtang/8c9c7ac7ffc55e9069ec6ce3eb6e9d8e to your computer and use it in GitHub Desktop.
Save iamdtang/8c9c7ac7ffc55e9069ec6ce3eb6e9d8e to your computer and use it in GitHub Desktop.
extract a list of Medium posts from the page
// scraping from the page
function getPosts() {
let domPosts = Array.from(document.querySelectorAll('.postArticle')).reverse();
return domPosts.map((domPost) => {
return {
href: domPost.querySelector('.js-postField').parentNode.href,
title: domPost.querySelector('h3').textContent.trim(),
date: domPost.querySelector('time').getAttribute('datetime')
};
});
}
getPosts();
// sorting all of them
const fs = require('fs');
let posts = JSON.parse(fs.readFileSync('medium-posts.json'));
let sorted = posts.sort((p1, p2) => {
let date1 = new Date(p1.date);
let date2 = new Date(p2.date);
if (date1 < date2) {
return -1;
}
if (date1 > date2) {
return 1;
}
// a must be equal to b
return 0;
});
fs.writeFileSync('medium-posts-sorted.json', JSON.stringify(sorted));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment