syndicate.go (view raw)
1//
2// Copyright (c) 2024 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "bytes"
20 notrand "math/rand"
21 "strings"
22 "time"
23
24 "github.com/mmcdole/gofeed"
25)
26
27func syndicate(user *WhatAbout, url string) {
28 data, err := fetchsome(url)
29 if err != nil {
30 dlog.Printf("error fetching feed: %s", err)
31 return
32 }
33 parser := gofeed.NewParser()
34 rss, err := parser.Parse(bytes.NewReader(data))
35 if err != nil {
36 dlog.Printf("error parsing feed: %s", err)
37 return
38 }
39 reverseItems(rss.Items)
40 for _, item := range rss.Items {
41 dlog.Printf("link: %s", item.Link)
42 grabhonk(user, item.Link)
43 }
44}
45
46func syndicator() {
47 for {
48 dur := 8 * time.Hour
49 dur += time.Duration(notrand.Int63n(int64(dur / 4)))
50 time.Sleep(dur)
51 users := allusers()
52 for _, ui := range users {
53 user, _ := butwhatabout(ui.Username)
54 honkers := gethonkers(user.ID)
55 for _, h := range honkers {
56 if strings.HasSuffix(h.XID, ".rss") {
57 syndicate(user, h.XID)
58 }
59 }
60 }
61 }
62}
63
64func reverseItems(items []*gofeed.Item) {
65 for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
66 items[i], items[j] = items[j], items[i]
67 }
68}