|
| 1 | +package phpbb2rss |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "github.com/PuerkitoBio/goquery" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type RSS struct { |
| 13 | + XMLName xml.Name `xml:"rss"` |
| 14 | + Version string `xml:"version,attr"` |
| 15 | + Channel Channel `xml:"channel"` |
| 16 | +} |
| 17 | + |
| 18 | +type Channel struct { |
| 19 | + Title string `xml:"title"` |
| 20 | + Link string `xml:"link"` |
| 21 | + Description string `xml:"description"` |
| 22 | + Items []Item `xml:"item"` |
| 23 | +} |
| 24 | + |
| 25 | +type Item struct { |
| 26 | + Title string `xml:"title"` |
| 27 | + Link string `xml:"link"` |
| 28 | + Description string `xml:"description"` |
| 29 | + PubDate string `xml:"pubDate"` |
| 30 | + GUID string `xml:"guid"` |
| 31 | +} |
| 32 | + |
| 33 | +func FetchAndGenerateRSS(forumURL string) (string, error) { |
| 34 | + resp, err := http.Get(forumURL) |
| 35 | + if err != nil { |
| 36 | + return "", fmt.Errorf("failed to fetch forum page: %w", err) |
| 37 | + } |
| 38 | + defer resp.Body.Close() |
| 39 | + |
| 40 | + if resp.StatusCode != http.StatusOK { |
| 41 | + return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 42 | + } |
| 43 | + |
| 44 | + doc, err := goquery.NewDocumentFromReader(resp.Body) |
| 45 | + if err != nil { |
| 46 | + return "", fmt.Errorf("failed to parse page content: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + var items []Item |
| 50 | + doc.Find(".forumline tr").Each(func(i int, s *goquery.Selection) { |
| 51 | + topic := s.Find(".topictitle a").First() |
| 52 | + title := strings.TrimSpace(topic.Text()) |
| 53 | + topicLink, topicExists := topic.Attr("href") |
| 54 | + latestPostLink, latestPostExists := s.Find("a:contains('View latest post')").Attr("href") |
| 55 | + if !topicExists || title == "" { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + link := fmt.Sprintf("%s/%s", forumURL, topicLink) |
| 60 | + if latestPostExists { |
| 61 | + link = fmt.Sprintf("%s/%s", forumURL, latestPostLink) |
| 62 | + } |
| 63 | + |
| 64 | + pubDateRaw := strings.TrimSpace(s.Find(".postdetails").Last().Text()) |
| 65 | + parsedDate, err := time.Parse("Mon Jan 02, 2006 3:04 pm", pubDateRaw) |
| 66 | + if err != nil { |
| 67 | + parsedDate = time.Now() |
| 68 | + } |
| 69 | + |
| 70 | + replies := strings.TrimSpace(s.Find("td:nth-child(4) .postdetails").Text()) |
| 71 | + posts := strings.TrimSpace(s.Find("td:nth-child(5) .postdetails").Text()) |
| 72 | + author := strings.TrimSpace(s.Find(".name a").First().Text()) |
| 73 | + lastCommenter := strings.TrimSpace(s.Find(".row2 a[href*='profile']").Last().Text()) |
| 74 | + pages := parsePageCount(s.Find("span.gensmall").Text()) |
| 75 | + category := strings.TrimSpace(s.Find(".forumlink").Text()) |
| 76 | + |
| 77 | + description := fmt.Sprintf("Category: %s\nAuthor: %s\nLast Commenter: %s\nReplies: %s\nPosts: %s\nPages: %s", category, author, lastCommenter, replies, posts, pages) |
| 78 | + |
| 79 | + titleWithCategory := fmt.Sprintf("[%s] %s", category, title) |
| 80 | + |
| 81 | + items = append(items, Item{ |
| 82 | + Title: titleWithCategory, |
| 83 | + Link: link, |
| 84 | + Description: description, |
| 85 | + PubDate: parsedDate.Format(time.RFC1123), |
| 86 | + GUID: link, |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + rss := RSS{ |
| 91 | + Version: "2.0", |
| 92 | + Channel: Channel{ |
| 93 | + Title: "PHPBB2 Forum Topics", |
| 94 | + Link: forumURL, |
| 95 | + Description: "RSS feed for topics from a PHPBB2 forum page", |
| 96 | + Items: items, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + xmlData, err := xml.MarshalIndent(rss, "", " ") |
| 101 | + if err != nil { |
| 102 | + return "", fmt.Errorf("failed to marshal RSS feed: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + return fmt.Sprintf("%s%s", xml.Header, xmlData), nil |
| 106 | +} |
| 107 | + |
| 108 | +func parsePageCount(gensmallText string) string { |
| 109 | + if !strings.Contains(gensmallText, "Goto page") { |
| 110 | + return "1" |
| 111 | + } |
| 112 | + pageLinks := strings.Split(gensmallText, ",") |
| 113 | + return fmt.Sprintf("%d", len(pageLinks)) |
| 114 | +} |
0 commit comments