下载bbc音频并发邮件

This commit is contained in:
xing 2023-07-09 22:57:47 +08:00
parent 4c20e29644
commit 21b4e3cebf
3 changed files with 110 additions and 0 deletions

View File

@ -3,7 +3,12 @@ package bbclearn
import ( import (
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/fthvgb1/wp-go/rss2" "github.com/fthvgb1/wp-go/rss2"
"io"
"log"
"net/http" "net/http"
"os"
"path/filepath"
"rss/mail"
"strings" "strings"
"time" "time"
) )
@ -72,9 +77,54 @@ func fetch(u string) (r rss2.Item) {
r.PubDate = date.Format(time.RFC1123Z) r.PubDate = date.Format(time.RFC1123Z)
r.Guid = u r.Guid = u
r.Description = content r.Description = content
go downAndSendMail(dom, r.Title)
return 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) { func full(doc *goquery.Document, recentDay int) (r rss2.Item) {
a := doc.Find("#bbcle-content .widget-container-full a") a := doc.Find("#bbcle-content .widget-container-full a")
u, ok := a.Attr("href") u, ok := a.Attr("href")

View File

@ -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)
}
})
}
}

36
mail/mail.go Normal file
View File

@ -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)
}