DEV Community

Cover image for Adventures of a Hobbyist ~ Part six
Andrew Bone
Andrew Bone

Posted on • Updated on

Adventures of a Hobbyist ~ Part six

A slow week

What is this?

I'm learning node for an opensource project, I've decided to document my journey as a way to keep me motivated and to get helpful suggestions or help when the time comes.

Here are the previous posts in the series, the list is getting long so I may start only including the last post, or maybe I'll make an index post.

What do you mean "a slow week"

Last week, as some of you may know, I was on vacation meaning my return to the office involved lots of catching up, leaving little room for side projects, like programming.

But I still wanted to do something even if it was small so I wrote a simple node script.

What was the script?

Two weeks ago a user called @markmatute asked for node exercises to help them learn, I thought up a simple one and gave it to them.

The simple gist was for a CLI tool that takes a username as an argument and returns the 10 most recent dev.to posts by that user. On a side note, dev.to has an RSS feed, who knew.

I decided to tackle my own exercise, I mean, how hard could it be.

The code

As I've said before I'm still learning so if you see any errors or just sloppy code let me know in the comments it really helps with the learning experience.

// https://www.npmjs.com/package/rss-parser
const RSS = require('rss-parser');
const rf = new RSS();

// third argument, which is the user input one
const arg = process.argv[2];

// function to take RSS data and log it to the screen
function print(result) {
  console.log(`Author: ${result.title}`);
  console.log(`Description: ${result.description}`);
  // only keep the first 10 items
  let items = result.items.length > 10 ? result.items.slice(0, 9) : result.items;
  //iterate through the items
  for (let item of items) {
    console.log('\n'+item.pubDate.substring(0, item.pubDate.length - 15) + ':');
    console.log(item.title);
    // only show this line if there is something on it
    item.content.trim() && console.log(item.content.trim().replace(/<[^>]*>/g, '').split('\n')[0].split(/\.|!|\?/)[0]);
  }
}

// start everything off by calling the function with 
// URL and argument
rf.parseURL(`https://dev.to/feed/${arg}`).then(print).catch(err => {throw err});

Enter fullscreen mode Exit fullscreen mode

As you can see, it's quite simple I use RSS-parser to read the RSS feed and then have a loop to go through the items and print them out in the format I wanted.

The output

I used @ben as an example, he's got a lot of posts so it was easy to see if it was working.

node .\devtofeed.js ben
Enter fullscreen mode Exit fullscreen mode
Author: Ben Halpern
Description: A Canadian living in New York, having a lot of fun cultivating this community! Creator and webmaster of dev.to.

Thu, 30 Aug 2018:
The Right Idea Becomes the Wrong Idea Over Time
2008: Don't build your server-side application with JavaScript

Tue, 28 Aug 2018:
Who's looking for open source contributors? (August 28 edition)
Please shamelessly promote your project

Sun, 26 Aug 2018:
Four Key Elements of a Healthy Framework Ecosystem
I wanted to share a great comment from this thread on Rails

Thu, 23 Aug 2018:
The Rails Ecosystem is Healthier than Ever
This is the first post in a new "flare tag" we're trying out: #healthydebate

Thu, 23 Aug 2018:
How to Host an "Ask Me Anything" on dev.to
I have been really happy to see more folks hosting AMAs

Mon, 20 Aug 2018:
Who's looking for open source contributors? (August 20 edition)
Please shamelessly promote your project

Sat, 18 Aug 2018:
What part of your first dev job were you least prepared for?

Fri, 17 Aug 2018:
Follow Friday! (v6)
Who have you been keeping up with on dev

Fri, 17 Aug 2018:
How many computers do you use?
Do you do everything on one laptop you carry around with you
Enter fullscreen mode Exit fullscreen mode

Signing off

This really was a short post, next week, provided life is back to normal, I plan on looking at node routers and then maybe js routers on top of that. Thank you for reading.

🦄❤🧠

Top comments (0)