Daily email of Wikipedia's Current Events

kiwicopple

Copple

Posted on April 26, 2020

Daily email of Wikipedia's Current Events

tldr: I built https://currentevents.email

Intro

For many years I haven't been reading any news at all. *HackerNews doesn't count

While this is great for my cortisol levels, I do feel a bit uninformed.

I recently discovered that wikipedia has daily summary of key events happening around the world.

I decided that is the perfect replacement for news. I've been reading this every day to keep updated. Although it's not hard, I do find the processes of opening the page everyday a bit tedious.

Wikipedia supports RSS, but I've never been much of an RSS user. And so while thinking of something to build this weekend I figured I would create a daily, automated email of the events listed on Wikipedia.

Here is the result: https://currentevents.email

How I built it

The build didn't actually take too long. These are the things I used.

1. Get the content

First I fetch the html content using cheerio. The content requires a bit of cleaning - removing some of the styling etc - but it was a fairly simple process. Here are the key parts:

const request = require('request')
const cheerio = require('cheerio')
const moment = require('moment')

const currentMonth = moment().format('MMMM_YYYY')
const URL_TO_PARSE = `https://en.wikipedia.org/wiki/Portal:Current_events/${currentMonth}`

request(URL_TO_PARSE, async (err, response, body) => {
  const $ = cheerio.load(body)
  $('*').removeAttr('style')
  cleanseLinks($) // Not included here
  styleHeaders($) // Not included here
  styleLists($) // Not included here
  styleLinks($) // Not included here

  // Loop through all days this month
  var startOfMonth = moment.startOf('month').format('YYYY-MM-DD')
  var today = moment().subtract(1, 'day')
  for (var m = moment(startOfMonth); m.isBefore(today); m.add(1, 'days')) {
    const currentDayEvents = $('.description', `div#${m.format('YYYY_MMMM_d')}`)
    console.log(currentDayEvents.html())
  }
})

2. Create the email template

Anybody who has tried to create a nice email quickly finds that it's nearly impossible to design something which works on all email clients.

Luckily i found this email template on Github which did all the hard work.

I simply removed the content in the email template and replaced it with the currentDayEvents.html() in the code snippet above.

3. Sending it to subscribers

I needed a transactional email provider for people to subscribe and to send the emails. I chose SendGrid. They provide a hosted signup page and also an API for sending the email.

4. Sending it every day

The solution to this one was quite cool. I discovered that Github Actions can be run on a schedule, so I created a action that runs at 1 minute past midnight every day. Here is the action:

name: 'Current Events Bot'

on:
  schedule:
    - cron: '1 0 * * *' # Run at 1 min past midnight every day

jobs:
  bot:
    runs-on: ubuntu-16.04 # Download the server
    steps:
      - uses: actions/checkout@v2 # Check out the code
      - uses: actions/setup-node@v1 # Set up node
        with:
          node-version: '12.x'
      - run: npm install # Install the modules
      - run: npm run build # Run the "build" script that I created
      - name: Commit files # Commit all the changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m "Today's events" --allow-empty
      - name: Push changes # Push the changes to the repo
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.CURRENT_EVENTS_TOKEN }}
      - run: SENDGRID_API_KEY=${{ secrets.SENDGRID_API_KEY }} npm run send # Run the email script that I created

Conclusion

It took a few hours to build, even though it was quite simple. I also bought the domain name and hosted the content on Netlify, so the set up took a bit of time.

Feel free to comment if you want to get the full code.

💖 💪 🙅 🚩
kiwicopple
Copple

Posted on April 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related