|
| 1 | +package feed |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "regexp" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +var dateFormats = []string{ |
| 17 | + time.RFC1123Z, |
| 18 | + time.RFC1123, |
| 19 | + time.RFC3339, |
| 20 | + time.RFC3339Nano, |
| 21 | + "2006-01-02T15:04:05Z", |
| 22 | + "2006-01-02 15:04:05 -0700", |
| 23 | + "02 Jan 2006 15:04 -0700", |
| 24 | + "Mon, 02 Jan 2006 15:04:05 GMT", |
| 25 | + "02 Jan 2006 15:04 +0000", |
| 26 | + "2006-01-02", |
| 27 | + "January 2, 2006", |
| 28 | +} |
| 29 | + |
| 30 | +func cleanHTML(input string) string { |
| 31 | + // first, remove HTML tags |
| 32 | + tagRegex := regexp.MustCompile("<[^>]*>") |
| 33 | + cleaned := tagRegex.ReplaceAllString(input, "") |
| 34 | + |
| 35 | + // & convert HTML entities |
| 36 | + cleaned = strings.ReplaceAll(cleaned, " ", " ") |
| 37 | + cleaned = strings.ReplaceAll(cleaned, "&", "&") |
| 38 | + cleaned = strings.ReplaceAll(cleaned, "<", "<") |
| 39 | + cleaned = strings.ReplaceAll(cleaned, ">", ">") |
| 40 | + cleaned = strings.ReplaceAll(cleaned, """, "\"") |
| 41 | + |
| 42 | + // & normalize whitespace |
| 43 | + wsRegex := regexp.MustCompile(`\s+`) |
| 44 | + cleaned = wsRegex.ReplaceAllString(cleaned, " ") |
| 45 | + |
| 46 | + return strings.TrimSpace(cleaned) |
| 47 | +} |
| 48 | + |
| 49 | +func parseDate(item Item) time.Time { |
| 50 | + dateCandidates := []string{ |
| 51 | + item.PubDate, |
| 52 | + item.Date, |
| 53 | + item.Published, |
| 54 | + item.Updated, |
| 55 | + } |
| 56 | + |
| 57 | + for _, dateStr := range dateCandidates { |
| 58 | + if dateStr == "" { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + for _, format := range dateFormats { |
| 63 | + if t, err := time.Parse(format, dateStr); err == nil { |
| 64 | + return t |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + log.Printf("warn: Could not parse any date from item %s", item.Title) |
| 70 | + return time.Now() |
| 71 | +} |
| 72 | + |
| 73 | +func getDescription(item Item) string { |
| 74 | + candidates := []string{ |
| 75 | + item.Description, |
| 76 | + item.Content, |
| 77 | + item.Encoded, |
| 78 | + } |
| 79 | + |
| 80 | + for _, candidate := range candidates { |
| 81 | + if candidate != "" { |
| 82 | + return cleanHTML(candidate) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return "Visit post for details." |
| 87 | +} |
| 88 | + |
| 89 | +func getAuthor(item Item, channelTitle string) string { |
| 90 | + if item.Author != "" { |
| 91 | + return item.Author |
| 92 | + } |
| 93 | + if item.Creator != "" { |
| 94 | + return item.Creator |
| 95 | + } |
| 96 | + return channelTitle |
| 97 | +} |
| 98 | + |
| 99 | +func FetchFeed(url string) ([]BlogPost, error) { |
| 100 | + resp, err := http.Get(url) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("fetching feed %s: %w", url, err) |
| 103 | + } |
| 104 | + defer resp.Body.Close() |
| 105 | + |
| 106 | + body, err := io.ReadAll(resp.Body) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("reading res from %s: %w", url, err) |
| 109 | + } |
| 110 | + |
| 111 | + var feed Feed |
| 112 | + if err := xml.Unmarshal(body, &feed); err != nil { |
| 113 | + return nil, fmt.Errorf("parsing feed %s: %w", url, err) |
| 114 | + } |
| 115 | + |
| 116 | + var posts []BlogPost |
| 117 | + |
| 118 | + // we need to worry about both RSS *AND* Atom feeds |
| 119 | + items := feed.Channel.Items |
| 120 | + if len(items) == 0 { |
| 121 | + items = feed.Channel.Entries |
| 122 | + } |
| 123 | + if len(items) == 0 { |
| 124 | + items = feed.Entries |
| 125 | + } |
| 126 | + |
| 127 | + for _, item := range items { |
| 128 | + post := BlogPost{ |
| 129 | + Title: item.Title, |
| 130 | + Link: item.Link, |
| 131 | + Date: parseDate(item), |
| 132 | + Author: getAuthor(item, feed.Channel.Title), |
| 133 | + Summary: getDescription(item), |
| 134 | + } |
| 135 | + posts = append(posts, post) |
| 136 | + } |
| 137 | + |
| 138 | + return posts, nil |
| 139 | +} |
| 140 | + |
| 141 | +func FetchAllFeeds(feeds []string) []BlogPost { |
| 142 | + var ( |
| 143 | + wg sync.WaitGroup |
| 144 | + mu sync.Mutex |
| 145 | + posts []BlogPost |
| 146 | + ) |
| 147 | + |
| 148 | + for _, feedURL := range feeds { |
| 149 | + wg.Add(1) |
| 150 | + go func(url string) { |
| 151 | + defer wg.Done() |
| 152 | + |
| 153 | + feedPosts, err := FetchFeed(url) |
| 154 | + if err != nil { |
| 155 | + log.Printf("err fetching %s: %v", url, err) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + mu.Lock() |
| 160 | + posts = append(posts, feedPosts...) |
| 161 | + mu.Unlock() |
| 162 | + }(feedURL) |
| 163 | + } |
| 164 | + |
| 165 | + wg.Wait() |
| 166 | + |
| 167 | + sort.Slice(posts, func(i, j int) bool { |
| 168 | + return posts[i].Date.After(posts[j].Date) |
| 169 | + }) |
| 170 | + |
| 171 | + return posts |
| 172 | +} |
0 commit comments