選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
2.5KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strconv"
  12. "time"
  13. )
  14. func saveURL(url string) (tmpFile string, contentType string, err error) {
  15. req, err := http.NewRequest("GET", url, nil)
  16. if err != nil {
  17. return
  18. }
  19. return saveHTTPRequest(req)
  20. }
  21. //{"video_url":"http://153.37.232.145/vweixinp.tc.qq.com/1007_6475d21fb443453b8adeac7e59cc0e1c.f10.mp4?vkey=E79C6B0DA06E7A434D15E24774F91412386D1956768C400AC93ECA5320ACAAAF050E1E1BA5C22DFD81B5EE3FBA97E928E0FC2DC597CF611B3E6641BC1AEE2892736FFE29E993F200AA4A0811FEB4E234C48516131207DDE4&sha=0&save=1"}
  22. type videoURL struct {
  23. VideoURL string `json:"video_url"`
  24. }
  25. //readVideoURL try to read a file into json structure
  26. //containing video_url
  27. //
  28. func readVideoURL(path string) (u string) {
  29. body, err := ioutil.ReadFile(path)
  30. if err == nil {
  31. var v = videoURL{}
  32. err = json.Unmarshal([]byte(body), &v)
  33. if err == nil {
  34. _, err = url.ParseRequestURI(v.VideoURL)
  35. if err == nil {
  36. return v.VideoURL
  37. }
  38. }
  39. }
  40. return ""
  41. }
  42. func buildHTTPReqWithHeader(httpMethod, targetURL string, headers map[string]string) (req *http.Request, err error) {
  43. req, err = http.NewRequest(httpMethod, targetURL, nil)
  44. if err != nil {
  45. return
  46. }
  47. if headers != nil {
  48. for k, v := range headers {
  49. req.Header.Set(k, v)
  50. }
  51. }
  52. return
  53. }
  54. func saveHTTPRequest(req *http.Request) (tmpFile string, contentType string, err error) {
  55. var myClient = &http.Client{Timeout: 20 * time.Second}
  56. r, err := myClient.Do(req)
  57. if err != nil {
  58. log.Printf("Fail to get URL: %s", req.RequestURI)
  59. log.Println(err)
  60. return "", "", err
  61. }
  62. defer r.Body.Close()
  63. contentType = r.Header.Get("Content-Type")
  64. contentLen := r.Header.Get("Content-Length")
  65. len, _ := strconv.Atoi(contentLen)
  66. log.Println(r)
  67. file, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_")
  68. // Use io.Copy to just dump the response body to the file. This supports huge files
  69. _, err = io.Copy(file, r.Body)
  70. if err != nil {
  71. log.Println(err)
  72. return "", contentType, err
  73. }
  74. tmpFile = file.Name()
  75. file.Close()
  76. //see if its a video url
  77. if len < 4096 && contentType == "text/plain" {
  78. u := readVideoURL(tmpFile)
  79. if u != "" {
  80. fmt.Println("saveing video url: " + u)
  81. os.Remove(tmpFile) //remove json file
  82. tmpFile, contentType, err = saveURL(u)
  83. }
  84. }
  85. return
  86. }
  87. func saveURLwithHTTPHeader(url string, headers map[string]string) (tmpFile string, contentType string, err error) {
  88. log.Println("saveURL: " + url)
  89. req, err := buildHTTPReqWithHeader("GET", url, headers)
  90. if err != nil {
  91. return
  92. }
  93. return saveHTTPRequest(req)
  94. }