From a0b984144034d197481ab80294d372f97309b229 Mon Sep 17 00:00:00 2001 From: Adrian Stanescu Date: Wed, 20 Nov 2019 12:39:26 +0000 Subject: [PATCH 1/2] Live.Create() Live.Start() Live.End() --- const.go | 5 ++ goinsta.go | 3 ++ live.go | 127 ++++++++++++++++++++++++++++++++++++++++++++ utilities/export.go | 2 +- utilities/import.go | 2 +- 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 live.go diff --git a/const.go b/const.go index 046afb81..89079076 100644 --- a/const.go +++ b/const.go @@ -133,4 +133,9 @@ const ( // upload urlUploadStory = "https://i.instagram.com/rupload_igphoto/103079408575885_0_-1340379573" + + // live + urlLiveCreate = "live/create" + urlLiveStart = "live/%s/start" + urlLiveEnd = "live/%s/end_broadcast/" ) diff --git a/goinsta.go b/goinsta.go index 1c8b7c61..562b71c6 100644 --- a/goinsta.go +++ b/goinsta.go @@ -70,6 +70,8 @@ type Instagram struct { Contacts *Contacts // Location instance Locations *LocationInstance + // Live instance + Live *Live c *http.Client } @@ -150,6 +152,7 @@ func (inst *Instagram) init() { inst.Feed = newFeed(inst) inst.Contacts = newContacts(inst) inst.Locations = newLocation(inst) + inst.Live = newLive(inst) } // SetProxy sets proxy for connection. diff --git a/live.go b/live.go new file mode 100644 index 00000000..f4c46718 --- /dev/null +++ b/live.go @@ -0,0 +1,127 @@ +package goinsta + +import ( + "encoding/json" + "fmt" +) + +type LiveCreateResponse struct { + BroadcastID int `json:"broadcast_id"` + UploadUrl string `json:"upload_url"` + // MaxTimeInSeconds int `json:"max_time_in_seconds"` + // SpeedTestUiTimeout int `json:"speed_test_ui_timeout"` + // StreamNetworkSpeedTestPayloadChunkSizeInBytes int `json:"stream_network_speed_test_payload_chunk_size_in_bytes"` + // StreamNetworkSpeedTestPayloadSizeInBytes int `json:"stream_network_speed_test_payload_size_in_bytes"` + // StreamNetworkSpeedTestPayloadTimeoutInSeconds int `json:"stream_network_speed_test_payload_timeout_in_seconds"` + // SpeedTestMinimumBandwidthThreshold int `json:"speed_test_minimum_bandwidth_threshold"` + // SpeedTestRetryMaxCount int `json:"speed_test_retry_max_count"` + // SpeedTestRetryTimeDelay int `json:"speed_test_retry_time_delay"` + // DisableSpeedTest int `json:"disable_speed_test"` + // StreamVideoAllowBFrames int `json:"stream_video_allow_b_frames"` + // StreamVideoWidth int `json:"stream_video_width"` + // StreamVideoBitRate int `json:"stream_video_bit_rate"` + // StreamVideoFps int `json:"stream_video_fps"` + // StreamAudioBitRate int `json:"stream_audio_bit_rate"` + // StreamAudioSampleRate int `json:"stream_audio_sample_rate"` + // StreamAudioChannels int `json:"stream_audio_channels"` + // HeartbeatInterval int `json:"heartbeat_interval"` + // BroadcasterUpdateFrequency int `json:"broadcaster_update_frequency"` + // StreamVideoAdaptiveBitrateConfig int `json:"stream_video_adaptive_bitrate_config"` + // StreamNetworkConnectionRetryCount int `json:"stream_network_connection_retry_count"` + // StreamNetworkConnectionRetryDelayInSeconds int `json:"stream_network_connection_retry_delay_in_seconds"` + // ConnectWith1rtt int `json:"connect_with_1rtt"` + // AvcRtmpPayload int `json:"avc_rtmp_payload"` + // AllowResolutionChange int `json:"allow_resolution_change"` +} + +type LiveStartResponse struct { + MediaID string `json:"media_id"` +} + +type Live struct { + inst *Instagram +} + +func newLive(inst *Instagram) *Live { + return &Live{inst: inst} +} + +func (live *Live) Create(width int, height int) (*LiveCreateResponse, error) { + insta := live.inst + response := &LiveCreateResponse{} + + data, err := insta.prepareData( + map[string]interface{}{ + "preview_width": width, + "preview_height": height, + "broadcast_type": "RTMP_SWAP_ENABLED", + "internal_only": 0, + }, + ) + if err != nil { + return response, err + } + + body, err := insta.sendRequest( + &reqOptions{ + Endpoint: urlLiveCreate, + Query: generateSignature(data), + IsPost: false, + }, + ) + + if err != nil { + return response, err + } + + fmt.Println(string(body)) + + err = json.Unmarshal(body, response) + + return response, err +} + +func (live *Live) Start(broadcastId string) (*LiveStartResponse, error) { + insta := live.inst + response := &LiveStartResponse{} + + data, err := insta.prepareData() + if err != nil { + return response, err + } + + body, err := insta.sendRequest( + &reqOptions{ + Endpoint: fmt.Sprintf(urlLiveStart, broadcastId), + Query: generateSignature(data), + IsPost: false, + }, + ) + + err = json.Unmarshal(body, response) + + return response, err +} + +func (live *Live) End(broadcastId string) error { + insta := live.inst + + data, err := insta.prepareData( + map[string]interface{}{ + "end_after_copyright_warning": false, + }, + ) + if err != nil { + return err + } + + _, err = insta.sendRequest( + &reqOptions{ + Endpoint: fmt.Sprintf(urlLiveEnd, broadcastId), + Query: generateSignature(data), + IsPost: true, + }, + ) + + return err +} diff --git a/utilities/export.go b/utilities/export.go index 79c8c8ee..8a0dc5c2 100644 --- a/utilities/export.go +++ b/utilities/export.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/base64" - "github.com/ahmdrz/goinsta" + "github.com/ahmdrz/goinsta/v2" ) // ExportAsBytes exports selected *Instagram object as []byte diff --git a/utilities/import.go b/utilities/import.go index f0b7556f..6b20235f 100644 --- a/utilities/import.go +++ b/utilities/import.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/base64" - "github.com/ahmdrz/goinsta" + "github.com/ahmdrz/goinsta/v2" ) // ImportFromBytes imports instagram configuration from an array of bytes. From 322fc6ed92dc45d97ee58db72025a2cb4c9f953d Mon Sep 17 00:00:00 2001 From: Adrian Stanescu Date: Wed, 27 Nov 2019 09:26:47 +0000 Subject: [PATCH 2/2] remove fmt.Println --- live.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/live.go b/live.go index f4c46718..7d54d5f1 100644 --- a/live.go +++ b/live.go @@ -74,8 +74,6 @@ func (live *Live) Create(width int, height int) (*LiveCreateResponse, error) { return response, err } - fmt.Println(string(body)) - err = json.Unmarshal(body, response) return response, err