Compare commits

..

2 Commits

Author SHA1 Message Date
21b4e3cebf 下载bbc音频并发邮件 2023-07-09 22:57:47 +08:00
4c20e29644 fix bug 2023-05-07 22:48:08 +08:00
4 changed files with 111 additions and 1 deletions

View File

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

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

View File

@ -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(), 0)
return filterItem(s, time.Now(), 1)
}))
}
func bbcLearn(w http.ResponseWriter, _ *http.Request) {