|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/skx/subcommands" |
| 12 | + "golang.org/x/net/html" |
| 13 | +) |
| 14 | + |
| 15 | +// Structure for our options and state. |
| 16 | +type feedsCommand struct { |
| 17 | + |
| 18 | + // We embed the NoFlags option, because we accept no command-line flags. |
| 19 | + subcommands.NoFlags |
| 20 | +} |
| 21 | + |
| 22 | +// ErrNoFeeds is used if no feeds are found in a remote URL |
| 23 | +var ErrNoFeeds = errors.New("NO-FEED") |
| 24 | + |
| 25 | +// Info returns the name of this subcommand. |
| 26 | +func (t *feedsCommand) Info() (string, string) { |
| 27 | + return "feeds", `Extract RSS feeds from remote URLS. |
| 28 | +
|
| 29 | +Details: |
| 30 | +
|
| 31 | +This command fetches the contents of the specified URL, much like |
| 32 | +the 'http-get' command would, and extracts any specified RSS feed |
| 33 | +from the contents of that remote URL. |
| 34 | +
|
| 35 | +Examples: |
| 36 | +
|
| 37 | + $ sysbox feeds https://blog.steve.fi/` |
| 38 | +} |
| 39 | + |
| 40 | +func (t *feedsCommand) FindFeeds(base string) ([]string, error) { |
| 41 | + |
| 42 | + ret := []string{} |
| 43 | + |
| 44 | + if !strings.HasPrefix(base, "http") { |
| 45 | + base = "https://" + base |
| 46 | + } |
| 47 | + |
| 48 | + // Make the request |
| 49 | + response, err := http.Get(base) |
| 50 | + if err != nil { |
| 51 | + return ret, err |
| 52 | + } |
| 53 | + |
| 54 | + // Get the body. |
| 55 | + defer response.Body.Close() |
| 56 | + |
| 57 | + z := html.NewTokenizer(response.Body) |
| 58 | + |
| 59 | + for { |
| 60 | + tt := z.Next() |
| 61 | + switch tt { |
| 62 | + case html.ErrorToken: |
| 63 | + err := z.Err() |
| 64 | + if err == io.EOF { |
| 65 | + if len(ret) > 0 { |
| 66 | + return ret, nil |
| 67 | + } |
| 68 | + return ret, ErrNoFeeds |
| 69 | + } |
| 70 | + return ret, fmt.Errorf("%s", z.Err()) |
| 71 | + case html.StartTagToken, html.SelfClosingTagToken: |
| 72 | + t := z.Token() |
| 73 | + if t.Data == "link" { |
| 74 | + isRSS := false |
| 75 | + u := "" |
| 76 | + for _, attr := range t.Attr { |
| 77 | + if attr.Key == "type" && (attr.Val == "application/rss+xml" || attr.Val == "application/atom+xml") { |
| 78 | + isRSS = true |
| 79 | + } |
| 80 | + |
| 81 | + if attr.Key == "href" { |
| 82 | + u = attr.Val |
| 83 | + } |
| 84 | + } |
| 85 | + if isRSS { |
| 86 | + if !strings.HasPrefix(u, "http") { |
| 87 | + u, _ = url.JoinPath(base, u) |
| 88 | + } |
| 89 | + ret = append(ret, u) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Nothing found? |
| 96 | + if len(ret) == 0 { |
| 97 | + return ret, ErrNoFeeds |
| 98 | + } |
| 99 | + return ret, nil |
| 100 | +} |
| 101 | + |
| 102 | +// Execute is invoked if the user specifies `feeds` as the subcommand. |
| 103 | +func (t *feedsCommand) Execute(args []string) int { |
| 104 | + |
| 105 | + // Ensure we have only a single URL |
| 106 | + if len(args) != 1 { |
| 107 | + fmt.Printf("Usage: feeds URL\n") |
| 108 | + return 1 |
| 109 | + } |
| 110 | + |
| 111 | + // The URL |
| 112 | + url := args[0] |
| 113 | + |
| 114 | + // We'll default to https if the protocol isn't specified. |
| 115 | + if !strings.HasPrefix(url, "http") { |
| 116 | + url = "https://" + url |
| 117 | + } |
| 118 | + |
| 119 | + out, err := t.FindFeeds(url) |
| 120 | + if err != nil { |
| 121 | + if err == ErrNoFeeds { |
| 122 | + fmt.Printf("No Feeds found in %s\n", url) |
| 123 | + } else { |
| 124 | + fmt.Printf("Error processing %s: %s\n", url, err) |
| 125 | + return 1 |
| 126 | + } |
| 127 | + } else { |
| 128 | + for _, x := range out { |
| 129 | + fmt.Printf("%s\n", x) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return 0 |
| 134 | +} |
0 commit comments