Compare commits
No commits in common. "21b4e3cebf84a9153999b85459a1730e20a0c62d" and "013b7a533d0056ffe1526ceabef36b485d2505d4" have entirely different histories.
21b4e3cebf
...
013b7a533d
@ -3,12 +3,7 @@ package bbclearn
|
||||
import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/fthvgb1/wp-go/rss2"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"rss/mail"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -77,54 +72,9 @@ func fetch(u string) (r rss2.Item) {
|
||||
r.PubDate = date.Format(time.RFC1123Z)
|
||||
r.Guid = u
|
||||
r.Description = content
|
||||
go downAndSendMail(dom, r.Title)
|
||||
return
|
||||
}
|
||||
|
||||
func downAndSendMail(doc *goquery.Document, title string) {
|
||||
type m struct {
|
||||
tit string
|
||||
content string
|
||||
f []string
|
||||
}
|
||||
mm := m{}
|
||||
var err error
|
||||
mm.tit = title
|
||||
mm.content, err = doc.Find(".widget-richtext .text").Html()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, ss := range []string{".bbcle-download-extension-pdf", ".bbcle-download-extension-mp3"} {
|
||||
uu, ok := doc.Find(ss).Attr("href")
|
||||
if ok {
|
||||
response, err := http.Get(uu)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
name := filepath.Base(uu)
|
||||
file, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0755)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
_, err = io.Copy(file, response.Body)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
mm.f = append(mm.f, name)
|
||||
}
|
||||
}
|
||||
if len(mm.f) < 1 {
|
||||
return
|
||||
}
|
||||
mail.SendMail(mm.tit, mm.content, mm.f...)
|
||||
for _, s := range mm.f {
|
||||
err := os.Remove(s)
|
||||
if err != nil {
|
||||
log.Printf("delete file %s err:%v\n", s, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func full(doc *goquery.Document, recentDay int) (r rss2.Item) {
|
||||
a := doc.Find("#bbcle-content .widget-container-full a")
|
||||
u, ok := a.Attr("href")
|
||||
|
@ -68,27 +68,3 @@ func Test_items(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_fetch(t *testing.T) {
|
||||
type args struct {
|
||||
u string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantR rss2.Item
|
||||
}{
|
||||
{
|
||||
name: "t1",
|
||||
args: args{u: "https://www.bbc.co.uk/learningenglish/chinese/features/take-away-english/ep-230703"},
|
||||
wantR: rss2.Item{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotR := fetch(tt.args.u); !reflect.DeepEqual(gotR, tt.wantR) {
|
||||
t.Errorf("fetch() = %v, want %v", gotR, tt.wantR)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
36
mail/mail.go
36
mail/mail.go
@ -1,36 +0,0 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func SendMail(subject string, content string, files ...string) {
|
||||
cm := os.Getenv("mail_cmd_path")
|
||||
if cm == "" {
|
||||
return
|
||||
}
|
||||
subjectParam := os.Getenv("mail_cmd_subject_param_name")
|
||||
if subjectParam == "" {
|
||||
return
|
||||
}
|
||||
contentParam := os.Getenv("mail_cmd_content_param_name")
|
||||
if contentParam == "" {
|
||||
return
|
||||
}
|
||||
fileParam := os.Getenv("mail_cmd_file_param_name")
|
||||
if fileParam == "" {
|
||||
return
|
||||
}
|
||||
v := []string{subjectParam, subject, contentParam, content}
|
||||
for _, f := range files {
|
||||
if f != "" {
|
||||
v = append(v, fileParam, f)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command(cm, v...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
log.Println(string(output), err)
|
||||
}
|
2
main.go
2
main.go
@ -69,7 +69,7 @@ func zhihuDaily(w http.ResponseWriter, req *http.Request) {
|
||||
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(), 1)
|
||||
return filterItem(s, time.Now(), 0)
|
||||
}))
|
||||
}
|
||||
func bbcLearn(w http.ResponseWriter, _ *http.Request) {
|
||||
|
Loading…
Reference in New Issue
Block a user