Compare commits

..

3 Commits

Author SHA1 Message Date
013b7a533d fix bug 2023-05-03 00:31:40 +08:00
233738f52f 完善 2023-04-14 23:26:53 +08:00
d675a17741 完善 2023-04-14 22:46:06 +08:00
2 changed files with 26 additions and 23 deletions

View File

@ -1,9 +1,7 @@
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"
@ -18,6 +16,9 @@ func LearnParse(s string, recentDay int) string {
var item []rss2.Item
item = append(item, full(document, recentDay))
item = append(item, items(document, recentDay)...)
if len(item) < 1 {
return ""
}
rss := rss2.Rss2{
Title: "BBC 英语教学",
Link: "https://www.bbc.co.uk/learningenglish/chinese/",
@ -27,21 +28,25 @@ func LearnParse(s string, recentDay int) string {
return rss.GetXML()
}
func dateFilter(u string, recentDay int) (r bool) {
func parseTime(u string) (date time.Time, err error) {
uu := strings.Split(u, "-")
if len(uu) < 2 {
return
}
date, err := time.Parse("060102", uu[len(uu)-1])
date, err = time.Parse("060102", uu[len(uu)-1])
if err != nil {
return
}
return
}
func dateFilter(u string, recentDay int) (r bool) {
date, err := parseTime(u)
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() {
if t.Sub(date).Hours()/24-float64(recentDay) > 0 {
return
}
r = true
@ -63,12 +68,8 @@ func fetch(u string) (r rss2.Item) {
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, " ")
date, _ := parseTime(u)
r.PubDate = date.Format(time.RFC1123Z)
r.Guid = u
r.Description = content
return

18
main.go
View File

@ -31,7 +31,7 @@ func fetch(u string, fn ...func(s string) string) string {
return html
}
func dayLimit(today, forwardDay int, s, format string) string {
func dayLimit(now time.Time, forwardDay int, s, format string) string {
da := date.FindStringSubmatch(s)
if len(da) <= 1 {
return s
@ -40,15 +40,17 @@ func dayLimit(today, forwardDay int, s, format string) string {
if err != nil {
return s
}
if today-forwardDay > t.Day() {
day := now.Sub(t).Hours() / 24
if day-float64(forwardDay) > 0 {
return ""
}
return s
}
func filterItem(html string, today, recentDay int) string {
func filterItem(html string, now time.Time, recentDay int) string {
return zhihuReg.ReplaceAllStringFunc(html, func(s string) string {
return dayLimit(today, recentDay, s, time.RFC1123Z)
return dayLimit(now, recentDay, s, time.RFC1123Z)
})
}
@ -60,14 +62,14 @@ func penti(w http.ResponseWriter, req *http.Request) {
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)
return filterItem(s, time.Now(), 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)
return filterItem(s, time.Now(), 0)
}))
}
func bbcLearn(w http.ResponseWriter, _ *http.Request) {
@ -78,13 +80,13 @@ func bbcLearn(w http.ResponseWriter, _ *http.Request) {
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)
return filterItem(s, time.Now(), 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)
return filterItem(s, time.Now(), 1)
}))
}