From 730a9b63652d9251c2546fd8e9e84809541debb6 Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Wed, 15 Nov 2023 12:44:25 -0300 Subject: [PATCH] Add OPML support --- flake.lock | 27 +++++++++++++++++++++++++++ flake.nix | 35 +++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ openring.go | 36 +++++++++++++++++++++++++++++++++--- 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..acd5e4d --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1699099776, + "narHash": "sha256-X09iKJ27mGsGambGfkKzqvw5esP1L/Rf8H3u3fCqIiU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "85f1ba3e51676fa8cc604a3d863d729026a6b8eb", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..e2de7c8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + description = "Sharpie monorepo"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = inputs@{ self, nixpkgs, ... }: + let + # System types to support. + supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" ]; + + # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + # Nixpkgs instantiated for supported system types. + nixpkgsFor = forAllSystems (system: import nixpkgs { + inherit system; + }); + in + { + packages = forAllSystems (system: + let + pkgs = nixpkgsFor."${system}"; + in + { + default = pkgs.buildGoModule { + name = "openring"; + src = ./.; + + vendorHash = "sha256-Uw9ygbfx2HQwQPZiGzRFsXJfzd752s19d4S4QA0S9fo="; + }; + }); + }; +} diff --git a/go.mod b/go.mod index fc1fcc1..80e509d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0 github.com/SlyMarbo/rss v1.0.1 + github.com/gilliek/go-opml v1.0.0 // indirect github.com/mattn/go-runewidth v0.0.4 github.com/microcosm-cc/bluemonday v1.0.2 ) diff --git a/go.sum b/go.sum index b6cc966..9ea5d34 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/SlyMarbo/rss v1.0.1/go.mod h1:JNF+T33oj4m5WLCQXpBTCgO+SxRbYVgdiiimHNg github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gilliek/go-opml v1.0.0 h1:X8xVjtySRXU/x6KvaiXkn7OV3a4DHqxY8Rpv6U/JvCY= +github.com/gilliek/go-opml v1.0.0/go.mod h1:fOxmtlzyBvUjU6bjpdjyxCGlWz+pgtAHrHf/xRZl3lk= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s= diff --git a/openring.go b/openring.go index 138605f..c0a8c0d 100644 --- a/openring.go +++ b/openring.go @@ -10,13 +10,14 @@ // Copyright: 2020 skuzzymiglet // Copyright: 2021 Gianluca Arbezzano // Copyright: 2021 sourque +// Copyright: 2023 Victor Freire package main import ( "bufio" "html" "html/template" - "io/ioutil" + "io" "log" "net/url" "os" @@ -27,6 +28,7 @@ import ( "git.sr.ht/~sircmpwn/getopt" "github.com/SlyMarbo/rss" + "github.com/gilliek/go-opml/opml" "github.com/mattn/go-runewidth" "github.com/microcosm-cc/bluemonday" ) @@ -50,6 +52,22 @@ func (us *urlSlice) Set(val string) error { return nil } +// flattenOutlines receives a list of outlines and flattlen them +func flattenOutlines(inputs []opml.Outline) []opml.Outline { + var acc []opml.Outline + for _, x := range inputs { + if len(x.Outlines) == 0 { + // we don't need it anymore + x.Outlines = nil + acc = append(acc, x) + } else { + acc = append(acc, flattenOutlines(x.Outlines)...) + } + } + + return acc +} + type Article struct { Date time.Time Link string @@ -65,12 +83,13 @@ func main() { perSource = getopt.Int("p", 1, "articles to take from each source") summaryLen = getopt.Int("l", 256, "length of summaries") urlsFile = getopt.String("S", "", "file with URLs of sources") + opmlFile = getopt.String("O", "", "OPML file") sources []*url.URL ) getopt.Var((*urlSlice)(&sources), "s", "list of sources") getopt.Usage = func() { - log.Fatalf("Usage: %s [-s https://source.rss...] < in.html > out.html", + log.Fatalf("Usage: %s [-O opml.xml] [-s https://source.rss...] < in.html > out.html", os.Args[0]) } @@ -91,7 +110,18 @@ func main() { file.Close() } - input, err := ioutil.ReadAll(os.Stdin) + if *opmlFile != "" { + doc, err := opml.NewOPMLFromFile(*opmlFile) + if err != nil { + panic(err) + } + outlines := flattenOutlines(doc.Body.Outlines) + for _, x := range outlines { + (*urlSlice)(&sources).Set(x.XMLURL) + } + } + + input, err := io.ReadAll(os.Stdin) if err != nil { panic(err) } -- 2.45.2