Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chanxuehong/wechat
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1
Choose a base ref
...
head repository: TreedomCN/wechat
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Oct 23, 2018

  1. add download hd audio media

    busyfree committed Oct 23, 2018

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    4661f49 View commit details
Showing with 77 additions and 3 deletions.
  1. +77 −3 mp/media/client_media.go
80 changes: 77 additions & 3 deletions mp/media/client_media.go
Original file line number Diff line number Diff line change
@@ -8,14 +8,13 @@ package media
import (
"errors"
"fmt"
"gopkg.in/chanxuehong/wechat.v1/json"
"gopkg.in/chanxuehong/wechat.v1/mp"
"io"
"mime"
"net/http"
"net/url"
"os"

"gopkg.in/chanxuehong/wechat.v1/json"
"gopkg.in/chanxuehong/wechat.v1/mp"
)

// 下载多媒体到文件.
@@ -35,6 +34,81 @@ func (clt *Client) DownloadMedia(mediaId, filepath string) (written int64, err e
return clt.downloadMediaToWriter(mediaId, file)
}

func (clt *Client) DownloadHDAudio(mediaId, filepath string) (written int64, err error) {
file, err := os.Create(filepath)
if err != nil {
return
}
defer func() {
file.Close()
if err != nil {
os.Remove(filepath)
}
}()

return clt.downloadHDAudioToWriter(mediaId, file)
}

// 下载多媒体到 io.Writer.
func (clt *Client) downloadHDAudioToWriter(mediaId string, writer io.Writer) (written int64, err error) {
token, err := clt.Token()
if err != nil {
return
}

hasRetried := false
RETRY:
finalURL := "https://api.weixin.qq.com/cgi-bin/media/get/jssdk?media_id=" + url.QueryEscape(mediaId) +
"&access_token=" + url.QueryEscape(token)

httpResp, err := clt.HttpClient.Get(finalURL)
if err != nil {
return
}
defer httpResp.Body.Close()

if httpResp.StatusCode != http.StatusOK {
err = fmt.Errorf("http.Status: %s", httpResp.Status)
return
}

ContentType, _, _ := mime.ParseMediaType(httpResp.Header.Get("Content-Type"))
if ContentType != "text/plain" && ContentType != "application/json" { // 返回的是媒体流
return io.Copy(writer, httpResp.Body)
}

// 返回的是错误信息
var result mp.Error
if err = json.NewDecoder(httpResp.Body).Decode(&result); err != nil {
return
}

switch result.ErrCode {
case mp.ErrCodeOK:
return // 基本不会出现
case mp.ErrCodeInvalidCredential, mp.ErrCodeAccessTokenExpired: // 失效(过期)重试一次
mp.LogInfoln("[WECHAT_RETRY] err_code:", result.ErrCode, ", err_msg:", result.ErrMsg)
mp.LogInfoln("[WECHAT_RETRY] current token:", token)

if !hasRetried {
hasRetried = true

if token, err = clt.TokenRefresh(); err != nil {
return
}
mp.LogInfoln("[WECHAT_RETRY] new token:", token)

result = mp.Error{}
goto RETRY
}
mp.LogInfoln("[WECHAT_RETRY] fallthrough, current token:", token)
fallthrough
default:
err = &result
return
}
}

// 下载多媒体到 io.Writer.
// 请注意, 视频文件不支持下载
func (clt *Client) DownloadMediaToWriter(mediaId string, writer io.Writer) (written int64, err error) {