From 702a48023cbd94f7c928e70bbdfd4451e62b0d35 Mon Sep 17 00:00:00 2001 From: Kannan Manickam Date: Tue, 27 Aug 2019 00:38:39 -0700 Subject: [PATCH] Add some minor improvements. * Added an ImportConfig function which will accept a parsed configuration file as a struct. * Added a SetHTTPTransport function which will enable setting a HTTP transport for tweaking low-level HTTP functionality. * Added a SetCookieJar function which enables using a custom cookie jar implementation. --- goinsta.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/goinsta.go b/goinsta.go index 2e09a1b8..c41460cc 100644 --- a/goinsta.go +++ b/goinsta.go @@ -77,6 +77,12 @@ func (inst *Instagram) SetHTTPClient(client *http.Client) { inst.c = client } +// SetHTTPTransport sets http transport. This further allows users to tweak the underlying +// low level transport for adding additional fucntionalities. +func (inst *Instagram) SetHTTPTransport(transport http.RoundTripper) { + inst.c.Transport = transport +} + // SetDeviceID sets device id func (inst *Instagram) SetDeviceID(id string) { inst.dID = id @@ -92,6 +98,20 @@ func (inst *Instagram) SetPhoneID(id string) { inst.pid = id } +// SetCookieJar sets the Cookie Jar. This further allows to use a custom implementation +// of a cookie jar which may be backed by a different data store such as redis. +func (inst *Instagram) SetCookieJar(jar http.CookieJar) error { + url, err := neturl.Parse(goInstaAPIUrl) + if err != nil { + return err + } + // First grab the cookies from the existing jar and we'll put it in the new jar. + cookies := inst.c.Jar.Cookies(url) + inst.c.Jar = jar + inst.c.Jar.SetCookies(url, cookies) + return nil +} + // New creates Instagram structure func New(username, password string) *Instagram { // this call never returns error @@ -209,21 +229,28 @@ func Export(inst *Instagram, writer io.Writer) error { // // This function does not set proxy automatically. Use SetProxy after this call. func ImportReader(r io.Reader) (*Instagram, error) { - url, err := neturl.Parse(goInstaAPIUrl) + bytes, err := ioutil.ReadAll(r) if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(r) + config := ConfigFile{} + err = json.Unmarshal(bytes, &config) if err != nil { return nil, err } + return ImportConfig(config) +} - config := ConfigFile{} - err = json.Unmarshal(bytes, &config) +// ImportConfig imports instagram configuration from a configuration object. +// +// This function does not set proxy automatically. Use SetProxy after this call. +func ImportConfig(config ConfigFile) (*Instagram, error) { + url, err := neturl.Parse(goInstaAPIUrl) if err != nil { return nil, err } + inst := &Instagram{ user: config.User, dID: config.DeviceID,