Skip to content

Commit 4d08d17

Browse files
committed
initial
1 parent cd44c85 commit 4d08d17

File tree

8 files changed

+179
-240
lines changed

8 files changed

+179
-240
lines changed

abckohlerrss.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

cmd/abckohlerreportrss-cgi/main.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

cmd/abckohlerreportrss/main.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

cmd/phpbb2rss/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"github.com/arran4/phpbb2-rss"
7+
"io"
8+
"log"
9+
"os"
10+
)
11+
12+
func main() {
13+
var forumURL string
14+
var out io.Writer = os.Stdout
15+
16+
setOutputFile := func(filename string) error {
17+
if closer, ok := out.(io.Closer); ok {
18+
_ = closer.Close()
19+
}
20+
file, err := os.Create(filename)
21+
if err != nil {
22+
return err
23+
}
24+
out = file
25+
return nil
26+
}
27+
28+
flag.StringVar(&forumURL, "url", "", "URL of the PHPBB2 forum 24-hour page")
29+
flag.Func("output", "Output file for the RSS feed", setOutputFile)
30+
flag.Parse()
31+
32+
if forumURL == "" {
33+
log.Fatal("A forum URL must be provided using the -url flag.")
34+
}
35+
36+
rss, err := phpbb2rss.FetchAndGenerateRSS(forumURL)
37+
if err != nil {
38+
log.Fatalf("Error generating RSS feed: %v", err)
39+
}
40+
41+
_, err = fmt.Fprintf(out, "%s", rss)
42+
if err != nil {
43+
log.Fatalf("Error writing RSS feed: %v", err)
44+
}
45+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/arran4/abc-kohler-report-rss
1+
module github.com/arran4/phpbb2-rss
22

33
go 1.23
44

goreleaser.yaml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
version: 2
2-
project_name: abckohlerreportrss
2+
project_name: phpbb2rss
33
builds:
44
-
5-
id: "abckohlerreportrss"
6-
binary: "abckohlerreportrss"
7-
dir: cmd/abckohlerreportrss
5+
id: "phpbb2rss"
6+
binary: "phpbb2rss"
7+
dir: cmd/phpbb2rss
88
env:
99
- CGO_ENABLED=0
10-
# -
11-
# id: "abckohlerreportrss-cgi"
12-
# binary: "abckohlerreportrss-cgi"
13-
# dir: cmd/abckohlerreportrss-cgi
14-
# env:
15-
# - CGO_ENABLED=0
16-
# goos: [linux]
17-
# goarch: [amd64, arm64]
18-
# flags: ["-tags=netgo", "-trimpath"]
1910
archives:
2011
-
2112
format_overrides:

phpbb2rss.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)