diff --git a/bbclearn/bbc.go b/bbclearn/bbc.go index 3968a03..812a9b2 100644 --- a/bbclearn/bbc.go +++ b/bbclearn/bbc.go @@ -3,7 +3,12 @@ package bbclearn import ( "github.com/PuerkitoBio/goquery" "github.com/fthvgb1/wp-go/rss2" + "io" + "log" "net/http" + "os" + "path/filepath" + "rss/mail" "strings" "time" ) @@ -72,9 +77,54 @@ 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") diff --git a/bbclearn/bbc_test.go b/bbclearn/bbc_test.go index b87e341..6773498 100644 --- a/bbclearn/bbc_test.go +++ b/bbclearn/bbc_test.go @@ -68,3 +68,27 @@ 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) + } + }) + } +} diff --git a/mail/mail.go b/mail/mail.go new file mode 100644 index 0000000..a04feea --- /dev/null +++ b/mail/mail.go @@ -0,0 +1,36 @@ +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) +}