Jay's Dev Diary 🔖

Actions are cool but syntax is finicky

Used some swift to pull my RSS feeds for YouTube and my blog. It then puts the last 3 on GitHub Readme. That was a bit of a challenge cause Xcode was giving me the proper errors.

// Struct for fetching from YouTube cause that was weird
import Foundation
import FeedKit // <- Awesome 3rd party library that's easy to use

struct YouTubeReader {
    let parser: FeedParser

    init() {
        self.parser = FeedParser(
            URL: URL(
                string: "https://www.youtube.com/feeds/videos.xml?channel_id=UC6na4Lq0ozPBjHD1X42szEQ"
            )!
        )
    }

    func load() throws -> [Post] {
        let posts: [Post] = try parser
            .parse()
            .get()
            .atomFeed?
            .entries?
            .compactMap { $0 }
            .prefix(3)
            .map {
                var link: String? = nil
                for videoLink in $0.links ?? [] {
                    link = videoLink.attributes?.href
                }
                return Post(title: ($0.title ?? "") as String, link: link ?? "")
            } ?? []

//        print(posts)

        return posts
    }
}

After that was squared away, I then used GitHub Actions to schedule a Chron job (every night) to fire the swift package and update the readme. Actions are written in YML files and space driven, so the wrong number of spaces is a huge issue 🙄

It works though.

Here's the GitHub action code:

name: Fetch latest blog and YouTube video
on:
  workflow_dispatch:
  push:
    branches:
      - master
  pull_request:
  schedule:
    - cron: "0 0 * * *" # runs every day at midnight
jobs:
  build:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
      - run: cd Readme && swift run README $(pwd)/../TEMPLATES/Readme.md.tpl $(pwd)/../README.md
      - run: |
          git config user.name heyjaywilson
          git config user.email contact@cctplus.dev
          git add README.md
          git diff --quiet && git diff --staged --quiet || git commit -m "[generated]: Update latest blog posts in `README.md` file"
          git push origin master

Tagged with: