diff --git a/download.go b/download.go index 6dbe32a..1af3e44 100644 --- a/download.go +++ b/download.go @@ -1,11 +1,14 @@ package main import ( + "encoding/json" "io" "io/ioutil" "log" "net/http" + "net/url" "os" + "strconv" "time" ) @@ -18,13 +21,70 @@ func saveURL(url string) (tmpFile string, contentType string, err error) { return "", "", err } defer r.Body.Close() + + contentType = r.Header.Get("Content-Type") + contentLen := r.Header.Get("Content-Length") + len, _ := strconv.Atoi(contentLen) + file, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_") // Use io.Copy to just dump the response body to the file. This supports huge files _, err = io.Copy(file, r.Body) if err != nil { - log.Fatal(err) + return "", contentType, err } tmpFile = file.Name() file.Close() + + //see if its a video url + if len < 4096 && contentType == "text/plain" { + u := readVideoURL(tmpFile) + if u != "" { + os.Remove(tmpFile) //remove json file + tmpFile, contentType, err = saveVideo(u) + } + } + + return +} + +func contentType2MediaType(contentType string) (mediaType string) { + switch contentType { + case "image/jpeg": + mediaType = "image" + case "text/plain": + mediaType = "json" + } + return +} + +//{"video_url":"http://153.37.232.145/vweixinp.tc.qq.com/1007_6475d21fb443453b8adeac7e59cc0e1c.f10.mp4?vkey=E79C6B0DA06E7A434D15E24774F91412386D1956768C400AC93ECA5320ACAAAF050E1E1BA5C22DFD81B5EE3FBA97E928E0FC2DC597CF611B3E6641BC1AEE2892736FFE29E993F200AA4A0811FEB4E234C48516131207DDE4&sha=0&save=1"} +type videoURL struct { + VideoURL string `json:"video_url"` +} + +//readVideoURL try to read a file into json structure +//containing video_url +// +func readVideoURL(path string) (u string) { + body, err := ioutil.ReadFile(path) + if err == nil { + var v = videoURL{} + err = json.Unmarshal([]byte(body), &v) + if err == nil { + _, err = url.ParseRequestURI(v.VideoURL) + if err == nil { + + return v.VideoURL + } + } + } + return "" +} + +func saveVideo(path string) (tmpFile string, contentType string, err error) { + u := readVideoURL(path) + if u != "" { + tmpFile, contentType, err = saveURL(u) + } return } diff --git a/download_test.go b/download_test.go new file mode 100644 index 0000000..3b87bb1 --- /dev/null +++ b/download_test.go @@ -0,0 +1,9 @@ +package main + +import "testing" + +func TestReadVideoURL(t *testing.T) { + u := readVideoURL("media_for_test/videourl.json") + expected := "http://153.37.232.148/vweixinp.tc.qq.com/1007_baa70feeaffb450682d848707e2d38d9.f10.mp4?vkey=6027E95AC6BADE010F39C1C08FA1E8E264C4702D52BE4B04C6BAF89A0857AB371971463C6C1D5D4779E1B748D1BC21041EC5AFBCD41BE4E7D0295A3DA96254505E8ACBDF1C427FD2FC92D5AB0CA9BFD16C0E02D99675DED4&sha=0&save=1" + AssertEqual(t, u, expected, "URL should be equal to "+expected) +}