From dd1e8a98de85a9bb9e12c4c5345a44e383fc9fe9 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Tue, 7 May 2019 11:41:44 +0530 Subject: [PATCH 1/2] added breaking code --- media.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/media.go b/media.go index 1eed4bb2..c3d65d87 100644 --- a/media.go +++ b/media.go @@ -988,3 +988,132 @@ func (insta *Instagram) UploadPhoto(photo io.Reader, photoCaption string, qualit return uploadResult.Media, nil } + +// UploadVideo post image from io.Reader to instagram. +func (insta *Instagram) UploadVideo(photo io.Reader, photoCaption string, quality int, filterType int) (Item, error) { + out := Item{} + + uploadID := time.Now().Unix() + + photoName := fmt.Sprintf("pending_media_%d.mp4", uploadID) + var b bytes.Buffer + w := multipart.NewWriter(&b) + + w.WriteField("upload_id", strconv.FormatInt(uploadID, 10)) + w.WriteField("_uuid", insta.uuid) + w.WriteField("_csrftoken", insta.token) + + var compression = map[string]interface{}{ + "lib_name": "jt", + "lib_version": "1.3.0", + "quality": quality, + } + cBytes, _ := json.Marshal(compression) + w.WriteField("image_compression", toString(cBytes)) + + fw, err := w.CreateFormFile("photo", photoName) + if err != nil { + return out, err + } + + var buf bytes.Buffer + rdr := io.TeeReader(photo, &buf) + if _, err = io.Copy(fw, rdr); err != nil { + return out, err + } + if err := w.Close(); err != nil { + return out, err + } + req, err := http.NewRequest("POST", goInstaAPIUrl+"upload/video/", &b) + if err != nil { + return out, err + } + req.Header.Set("X-IG-Capabilities", "3Q4=") + req.Header.Set("X-IG-Connection-Type", "WIFI") + req.Header.Set("Cookie2", "$Version=1") + req.Header.Set("Accept-Language", "en-US") + req.Header.Set("Accept-Encoding", "gzip, deflate") + req.Header.Set("Content-type", w.FormDataContentType()) + req.Header.Set("Connection", "close") + req.Header.Set("User-Agent", goInstaUserAgent) + + resp, err := insta.c.Do(req) + if err != nil { + return out, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return out, err + } + + if resp.StatusCode != 200 { + return out, fmt.Errorf("invalid status code, result: %s", resp.Status) + } + + var result struct { + UploadID string `json:"upload_id"` + XsharingNonces interface{} `json:"xsharing_nonces"` + Status string `json:"status"` + } + err = json.Unmarshal(body, &result) + if err != nil { + return out, err + } + + if result.Status != "ok" { + return out, fmt.Errorf("unknown error, status: %s", result.Status) + } + + width, height, err := getImageDimensionFromReader(&buf) + if err != nil { + return out, err + } + + config := map[string]interface{}{ + "media_folder": "Instagram", + "source_type": 4, + "caption": photoCaption, + "upload_id": strconv.FormatInt(uploadID, 10), + "device": goInstaDeviceSettings, + "edits": map[string]interface{}{ + "crop_original_size": []int{width * 1.0, height * 1.0}, + "crop_center": []float32{0.0, 0.0}, + "crop_zoom": 1.0, + "filter_type": filterType, + }, + "extra": map[string]interface{}{ + "source_width": width, + "source_height": height, + }, + } + data, err := insta.prepareData(config) + if err != nil { + return out, err + } + + body, err = insta.sendRequest(&reqOptions{ + Endpoint: "media/configure/?", + Query: generateSignature(data), + IsPost: true, + }) + if err != nil { + return out, err + } + var uploadResult struct { + Media Item `json:"media"` + UploadID string `json:"upload_id"` + Status string `json:"status"` + } + err = json.Unmarshal(body, &uploadResult) + if err != nil { + return out, err + } + + if uploadResult.Status != "ok" { + return out, fmt.Errorf("invalid status, result: %s", resp.Status) + } + + return uploadResult.Media, nil +} From ef099f6f7dc168f9da3c5c51a034a4fdef0598e1 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Tue, 7 May 2019 15:43:17 +0530 Subject: [PATCH 2/2] Cleaned up the code a little, added other fields for video upload (refered from the python code) --- media.go | 87 +++++++++++++------------------------------------------- 1 file changed, 19 insertions(+), 68 deletions(-) diff --git a/media.go b/media.go index c3d65d87..6b4380aa 100644 --- a/media.go +++ b/media.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "mime/multipart" "net/http" neturl "net/url" @@ -990,37 +991,19 @@ func (insta *Instagram) UploadPhoto(photo io.Reader, photoCaption string, qualit } // UploadVideo post image from io.Reader to instagram. -func (insta *Instagram) UploadVideo(photo io.Reader, photoCaption string, quality int, filterType int) (Item, error) { +func (insta *Instagram) UploadVideo(video io.Reader, photoCaption string, quality int, filterType int) (Item, error) { out := Item{} uploadID := time.Now().Unix() - photoName := fmt.Sprintf("pending_media_%d.mp4", uploadID) var b bytes.Buffer w := multipart.NewWriter(&b) w.WriteField("upload_id", strconv.FormatInt(uploadID, 10)) w.WriteField("_uuid", insta.uuid) w.WriteField("_csrftoken", insta.token) + w.WriteField("media_type", "2") - var compression = map[string]interface{}{ - "lib_name": "jt", - "lib_version": "1.3.0", - "quality": quality, - } - cBytes, _ := json.Marshal(compression) - w.WriteField("image_compression", toString(cBytes)) - - fw, err := w.CreateFormFile("photo", photoName) - if err != nil { - return out, err - } - - var buf bytes.Buffer - rdr := io.TeeReader(photo, &buf) - if _, err = io.Copy(fw, rdr); err != nil { - return out, err - } if err := w.Close(); err != nil { return out, err } @@ -1030,11 +1013,12 @@ func (insta *Instagram) UploadVideo(photo io.Reader, photoCaption string, qualit } req.Header.Set("X-IG-Capabilities", "3Q4=") req.Header.Set("X-IG-Connection-Type", "WIFI") + req.Header.Set("Host", "i.instagram.com") req.Header.Set("Cookie2", "$Version=1") req.Header.Set("Accept-Language", "en-US") req.Header.Set("Accept-Encoding", "gzip, deflate") req.Header.Set("Content-type", w.FormDataContentType()) - req.Header.Set("Connection", "close") + req.Header.Set("Connection", "keep-alive") req.Header.Set("User-Agent", goInstaUserAgent) resp, err := insta.c.Do(req) @@ -1042,6 +1026,7 @@ func (insta *Instagram) UploadVideo(photo io.Reader, photoCaption string, qualit return out, err } defer resp.Body.Close() + fmt.Println("Status: ", resp.Status) body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -1053,67 +1038,33 @@ func (insta *Instagram) UploadVideo(photo io.Reader, photoCaption string, qualit } var result struct { - UploadID string `json:"upload_id"` - XsharingNonces interface{} `json:"xsharing_nonces"` - Status string `json:"status"` - } - err = json.Unmarshal(body, &result) - if err != nil { - return out, err + UploadURL string `json:"video_upload_urls"` + Status string } - if result.Status != "ok" { - return out, fmt.Errorf("unknown error, status: %s", result.Status) - } + fmt.Println("[response]", string(body)) - width, height, err := getImageDimensionFromReader(&buf) + err = json.Unmarshal(body, &result) if err != nil { + log.Println("[unmarshal]", err.Error()) return out, err } - config := map[string]interface{}{ - "media_folder": "Instagram", - "source_type": 4, - "caption": photoCaption, - "upload_id": strconv.FormatInt(uploadID, 10), - "device": goInstaDeviceSettings, - "edits": map[string]interface{}{ - "crop_original_size": []int{width * 1.0, height * 1.0}, - "crop_center": []float32{0.0, 0.0}, - "crop_zoom": 1.0, - "filter_type": filterType, - }, - "extra": map[string]interface{}{ - "source_width": width, - "source_height": height, - }, - } - data, err := insta.prepareData(config) - if err != nil { - return out, err - } + fmt.Println("HERE: ", result) - body, err = insta.sendRequest(&reqOptions{ - Endpoint: "media/configure/?", - Query: generateSignature(data), - IsPost: true, - }) - if err != nil { - return out, err - } var uploadResult struct { Media Item `json:"media"` UploadID string `json:"upload_id"` Status string `json:"status"` } - err = json.Unmarshal(body, &uploadResult) - if err != nil { - return out, err - } + // err = json.Unmarshal(body, &uploadResult) + // if err != nil { + // return out, err + // } - if uploadResult.Status != "ok" { - return out, fmt.Errorf("invalid status, result: %s", resp.Status) - } + // if uploadResult.Status != "ok" { + // return out, fmt.Errorf("invalid status, result: %s", resp.Status) + // } return uploadResult.Media, nil }