package main
import (
"io"
"log"
"net/http"
"os"
"regexp"
"rss/bbclearn"
"strings"
"time"
)
var zhihuReg = regexp.MustCompile(`(?is:(- (.*?)
))`)
var date = regexp.MustCompile(`(.*)`)
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 voaLearnEnglish(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fetch("https://feedx.best/rss/voalearningenglish.xml", func(s string) string {
return filterItem(s, time.Now().Day(), 1)
}))
}
func main() {
port := os.Getenv("port")
if port == "" {
port = ":80"
}
time.Local = time.FixedZone("CST", 3600*8)
http.HandleFunc("/pentitugua", penti) //喷嚏图挂
http.HandleFunc("/zhihuDaily", zhihuDaily) //知乎日报
http.HandleFunc("/tjxz", tjxz) //田间小站
http.HandleFunc("/bbcLearn", bbcLearn) //bbc 英语学习
http.HandleFunc("/theNewYorker", theNewYorker) //纽约客
http.HandleFunc("/voaLearnEnglish", voaLearnEnglish) //VOA Learning English
log.Fatal(http.ListenAndServe(port, nil))
}