2023-04-13 18:03:56 +00:00
|
|
|
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 ""
|
|
|
|
}
|
2023-04-14 06:43:32 +00:00
|
|
|
defer res.Body.Close()
|
2023-04-13 18:03:56 +00:00
|
|
|
html := string(s)
|
|
|
|
for _, f := range fn {
|
|
|
|
html = f(html)
|
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:31:40 +00:00
|
|
|
func dayLimit(now time.Time, forwardDay int, s, format string) string {
|
2023-04-13 18:03:56 +00:00
|
|
|
da := date.FindStringSubmatch(s)
|
|
|
|
if len(da) <= 1 {
|
|
|
|
return s
|
|
|
|
}
|
2023-04-14 12:39:29 +00:00
|
|
|
t, err := time.Parse(format, da[1])
|
2023-04-13 18:03:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return s
|
|
|
|
}
|
2023-05-02 16:31:40 +00:00
|
|
|
day := now.Sub(t).Hours() / 24
|
|
|
|
if day-float64(forwardDay) > 0 {
|
2023-04-13 18:03:56 +00:00
|
|
|
return ""
|
|
|
|
}
|
2023-05-02 16:31:40 +00:00
|
|
|
|
2023-04-13 18:03:56 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:31:40 +00:00
|
|
|
func filterItem(html string, now time.Time, recentDay int) string {
|
2023-04-13 18:03:56 +00:00
|
|
|
return zhihuReg.ReplaceAllStringFunc(html, func(s string) string {
|
2023-05-02 16:31:40 +00:00
|
|
|
return dayLimit(now, recentDay, s, time.RFC1123Z)
|
2023-04-13 18:03:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-05-02 16:31:40 +00:00
|
|
|
return filterItem(s, time.Now(), 1)
|
2023-04-13 18:03:56 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func tjxz(w http.ResponseWriter, r *http.Request) {
|
2023-04-13 18:37:40 +00:00
|
|
|
|
2023-04-13 18:03:56 +00:00
|
|
|
io.WriteString(w, fetch("https://feedx.best/rss/tjxz.xml", func(s string) string {
|
2023-05-02 16:31:40 +00:00
|
|
|
return filterItem(s, time.Now(), 0)
|
2023-04-13 18:03:56 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
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 {
|
2023-05-02 16:31:40 +00:00
|
|
|
return filterItem(s, time.Now(), 1)
|
2023-04-13 18:03:56 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:02:35 +00:00
|
|
|
func voaLearnEnglish(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.WriteString(w, fetch("https://feedx.best/rss/voalearningenglish.xml", func(s string) string {
|
2023-05-02 16:31:40 +00:00
|
|
|
return filterItem(s, time.Now(), 1)
|
2023-04-14 06:02:35 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-04-13 18:03:56 +00:00
|
|
|
func main() {
|
|
|
|
port := os.Getenv("port")
|
|
|
|
if port == "" {
|
|
|
|
port = ":80"
|
|
|
|
}
|
2023-04-13 18:37:40 +00:00
|
|
|
time.Local = time.FixedZone("CST", 3600*8)
|
2023-04-14 06:02:35 +00:00
|
|
|
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
|
2023-04-13 18:03:56 +00:00
|
|
|
log.Fatal(http.ListenAndServe(port, nil))
|
|
|
|
}
|