国庆节学golang写了个小工具:批量下载公众号话题音频
前几天发过帖子用python写的批量下载公众号话题音频工具,这次国庆学golang重写了下,代码:package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
)
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func InArray(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
func main() {
defer func() {
if err := recover(); err != nil {
fmt.Print("错误信息:")
fmt.Println(err)
}
}()
var url string
fmt.Print("输入话题地址:")
fmt.Scanln(&url)
if len(url) == 0 {
panic("话题地址为空")
}
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
response, _ := client.Do(reqest)
defer response.Body.Close()
bResp, _ := io.ReadAll(response.Body)
content := string(bResp)
var voiceids = regexp.MustCompile(`data-voiceid="(.*)"`).FindAllStringSubmatch(content, -1)
var titles = regexp.MustCompile(`data-title="(.*)" data-voiceid`).FindAllStringSubmatch(content, -1)
fileName := "wechat_topic_audio_list.txt"
// fmt.Print(voiceids, titles)
fileContent, _ := ioutil.ReadFile(fileName)
var voice_urls = regexp.MustCompile(`\n`).Split(string(fileContent), -1)
// fmt.Print(voice_urls)
var f2 *os.File
for k, v := range voiceids {
if InArray(voice_urls, "https://res.wx.qq.com/voice/getvoice?mediaid="+v) {
fmt.Println("已经下载过音频:" + titles)
continue
}
res, _ := http.Get("https://res.wx.qq.com/voice/getvoice?mediaid=" + v)
f, _ := os.Create(titles + ".mp3")
io.Copy(f, res.Body)
if Exists(fileName) {
f2, _ = os.OpenFile(fileName, os.O_APPEND, 0666)
} else {
f2, _ = os.Create(fileName)
}
defer f2.Close()
fmt.Println("正在下载音频:" + titles)
// _, _ = io.WriteString(f2, "https://res.wx.qq.com/voice/getvoice?mediaid="+v+"\n")
// _, _ =f2.Write([]byte("https://res.wx.qq.com/voice/getvoice?mediaid="+v+"\n"))
_, _ = f2.WriteString("https://res.wx.qq.com/voice/getvoice?mediaid=" + v + "\n")
// _, _ = ioutil.WriteFile(fileName, []byte("https://res.wx.qq.com/voice/getvoice?mediaid="+v+"\n"), 0666)
}
fmt.Print("下载完成")
}
输入话题地址https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5NjAxOTU4MA==&action=getalbum&album_id=1777378132866465795&scene=173 就行,下载效果:
另外增加个功能,就是第2次下载会忽略已经下载过的音频:
由于python打包的exe在win7下运行不了,不知道golang打包的是否正常运行,有win7的小伙伴可以反馈下,谢谢。
exe地址: https://wwn.lanzouy.com/i88ck0cvdk9g
golang还挺方便的,之后打算把我之前python写的批量下载公众号文章内容音频视频再重写了,算熟悉下golang。
微软的visual studio code提示:The "gopls" command is not available.
Run "go get -v golang.org/x/tools/gopls" to install.和The "go-outline" command is not available.
Run "go get -v github.com/ramya-rao-a/go-outline" to install. 报错:输入话题地址:https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5NjAxOTU4MA==&action=getalbum&album_id=1777378132866465795&scene=173
错误信息:runtime error: invalid memory address or nil pointer dereference
感谢楼主,拿来试试 暂时用不上。。感谢分享。 看着好,收藏起来 除了罗胖60秒,还有一些好的音频公众号频道么推介么?
感谢楼主,拿来试试 谢谢分享这个不错 这个居然不需要cookie吗,我爬文章就需要带cookie 多谢楼主分享
多谢楼主分享