This commit is contained in:
xing 2023-04-14 02:03:56 +08:00
parent 1ebcd5256e
commit ddd4bc86f3
8 changed files with 20291 additions and 2 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
.idea .idea
.gitignore .gitignore
fetchdapenti.iml rss.iml
rss
fetchdapenti

3
Dockerfile Normal file
View File

@ -0,0 +1,3 @@
FROM alpine:latest
COPY rss /opt/rss
CMD ["/opt/rss"]

12542
bbclearn/a.html Normal file

File diff suppressed because it is too large Load Diff

7471
bbclearn/b.html Normal file

File diff suppressed because it is too large Load Diff

99
bbclearn/bbc.go Normal file
View File

@ -0,0 +1,99 @@
package bbclearn
import (
"fmt"
"github.com/PuerkitoBio/goquery"
strings2 "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/rss2"
"net/http"
"strings"
"time"
)
func LearnParse(s string, recentDay int) string {
document, err := goquery.NewDocumentFromReader(strings.NewReader(s))
if err != nil {
return ""
}
var item []rss2.Item
item = append(item, full(document, recentDay))
item = append(item, items(document, recentDay)...)
rss := rss2.Rss2{
Title: "BBC 英语教学",
Link: "http://www.bbc.co.uk/learningenglish/chinese/",
LastBuildDate: time.Now().Format(time.RFC1123Z),
Items: item,
}
return rss.GetXML()
}
func dateFilter(u string, recentDay int) (r bool) {
uu := strings.Split(u, "-")
if len(uu) < 2 {
return
}
date, err := time.Parse("060102", uu[len(uu)-1])
if err != nil {
return
}
t := time.Now()
if t.Year() != date.Year() || t.Month() != date.Month() {
return
}
fmt.Println(time.Now().Day()-recentDay, date.Day())
if t.Day()-recentDay > date.Day() {
return
}
r = true
return
}
func fetch(u string) (r rss2.Item) {
res, err := http.Get(u)
if err != nil {
return
}
dom, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return
}
s := dom.Find("#bbcle-content .widget-container-left")
content, err := goquery.OuterHtml(s.Find(".widget-list,.widget-pagelink").Remove().End())
if err != nil {
return
}
r.Title = s.Find(`div[data-widget-index="3"] h3`).Text()
r.PubDate = strings.TrimSpace(s.Find(".widget-bbcle-featuresubheader").Text())
r.PubDate = strings2.Replace(r.PubDate, map[string]string{
"\n": "",
})
r.PubDate = strings2.CutSpecialDuplicate(r.PubDate, " ")
r.Guid = u
r.Description = content
return
}
func full(doc *goquery.Document, recentDay int) (r rss2.Item) {
a := doc.Find("#bbcle-content .widget-container-full a")
u, ok := a.Attr("href")
if !ok {
return
}
if !dateFilter(u, recentDay) {
return
}
r = fetch(u)
return
}
func items(doc *goquery.Document, recentDay int) (r []rss2.Item) {
doc.Find("#bbcle-content > div > div.widget-container.widget-container-full > div.widget.widget-image.widget-image-two_column > div a").Each(func(i int, s *goquery.Selection) {
u, ok := s.Attr("href")
if !ok || !dateFilter(u, recentDay) {
return
}
r = append(r, fetch(u))
})
return
}

70
bbclearn/bbc_test.go Normal file
View File

@ -0,0 +1,70 @@
package bbclearn
import (
"github.com/PuerkitoBio/goquery"
"github.com/fthvgb1/wp-go/rss2"
"os"
"reflect"
"strings"
"testing"
)
func Test_full(t *testing.T) {
type args struct {
doc *goquery.Document
recentDay int
}
tests := []struct {
name string
args args
want rss2.Item
}{
{
name: "t1",
args: args{doc: func() *goquery.Document {
f, _ := os.ReadFile("b.html")
d, _ := goquery.NewDocumentFromReader(strings.NewReader(string(f)))
return d
}(), recentDay: 1},
//want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := full(tt.args.doc, tt.args.recentDay); got != tt.want {
t.Errorf("full() = %v, want %v", got, tt.want)
}
})
}
}
func Test_items(t *testing.T) {
type args struct {
doc *goquery.Document
recentDay int
}
tests := []struct {
name string
args args
wantR rss2.Item
}{
{
name: "t1",
args: args{
doc: func() *goquery.Document {
f, _ := os.ReadFile("b.html")
d, _ := goquery.NewDocumentFromReader(strings.NewReader(string(f)))
return d
}(),
recentDay: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotR := items(tt.args.doc, tt.args.recentDay); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("items() = %v, want %v", gotR, tt.wantR)
}
})
}
}

10
go.mod
View File

@ -1,3 +1,11 @@
module fetchdapenti module rss
go 1.19 go 1.19
require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/fthvgb1/wp-go v0.0.0-20230411054214-125764711dbd // indirect
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 // indirect
golang.org/x/net v0.7.0 // indirect
)

94
main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"io"
"log"
"net/http"
"os"
"regexp"
"rss/bbclearn"
"strings"
"time"
)
var zhihuReg = regexp.MustCompile(`(?is:(<item>(.*?)</item>))`)
var date = regexp.MustCompile(`<pubDate>(.*)</pubDate>`)
func fetch(u string, fn ...func(s string) string) string {
res, err := http.Get(u)
if err != nil {
return ""
}
s, err := io.ReadAll(res.Body)
if err != nil {
return ""
}
html := string(s)
for _, f := range fn {
html = f(html)
}
return html
}
func dayLimit(today, forwardDay int, s string) string {
da := date.FindStringSubmatch(s)
if len(da) <= 1 {
return s
}
t, err := time.Parse(time.RFC1123Z, da[1])
if err != nil {
return s
}
if today-forwardDay > t.Day() {
return ""
}
return s
}
func filterItem(html string, today, recentDay int) string {
return zhihuReg.ReplaceAllStringFunc(html, func(s string) string {
return dayLimit(today, recentDay, s)
})
}
func penti(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, fetch("https://feedx.best/rss/pentitugua.xml", func(s string) string {
return strings.ReplaceAll(s, "www.dapenti.com:99", "imgc.1see.org")
}))
}
func zhihuDaily(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, fetch("https://feedx.best/rss/zhihudaily.xml", func(s string) string {
return filterItem(s, time.Now().Day(), 1)
}))
}
func tjxz(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fetch("https://feedx.best/rss/tjxz.xml", func(s string) string {
return filterItem(s, time.Now().Day(), 0)
}))
}
func bbcLearn(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, fetch("https://www.bbc.co.uk/learningenglish/chinese", func(s string) string {
return bbclearn.LearnParse(s, 1)
}))
}
func theNewYorker(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fetch("https://feedx.best/rss/newyorker.xml", func(s string) string {
return filterItem(s, time.Now().Day(), 1)
}))
}
func main() {
port := os.Getenv("port")
if port == "" {
port = ":80"
}
http.HandleFunc("/pentitugua", penti)
http.HandleFunc("/zhihuDaily", zhihuDaily)
http.HandleFunc("/tjxz", tjxz)
http.HandleFunc("/bbcLearn", bbcLearn)
http.HandleFunc("/theNewYorker", theNewYorker)
log.Fatal(http.ListenAndServe(port, nil))
}