Skip to content

Commit

Permalink
Add debug options & fix inbox last permanent item
Browse files Browse the repository at this point in the history
  • Loading branch information
Davincible committed Aug 24, 2021
1 parent 155582f commit a0ea1d5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 47 deletions.
2 changes: 1 addition & 1 deletion collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func (item *Item) SaveTo(c *Collection) error {
// Call genereral save method first, as in the app this will always happen
err := item.Save()
if err != nil {
insta.WarnHandler(errors.New("Non fatal error, failed to save post to all"))
insta.warnHandler(errors.New("Non fatal error, failed to save post to all"))
}

data, err := json.Marshal(
Expand Down
73 changes: 41 additions & 32 deletions goinsta.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,30 @@ type Instagram struct {

c *http.Client

// Set to true to debug reponses
Debug bool

// Non-error message handlers.
// By default they will be printed out, alternatively you can e.g. pass them to a logger
InfoHandler func(...interface{})
WarnHandler func(...interface{})
infoHandler func(...interface{})
warnHandler func(...interface{})
debugHandler func(...interface{})
}

func defaultHandler(args ...interface{}) {
fmt.Println(args...)
}

func (insta *Instagram) SetInfoHandler(f func(...interface{})) {
insta.InfoHandler = f
insta.infoHandler = f
}

func (insta *Instagram) SetWarnHandler(f func(...interface{})) {
insta.WarnHandler = f
insta.warnHandler = f
}

func (insta *Instagram) SetDebugHandler(f func(...interface{})) {
insta.debugHandler = f
}

// SetHTTPClient sets http client. This further allows users to use this functionality
Expand Down Expand Up @@ -210,8 +218,9 @@ func New(username, password string) *Instagram {
},
Jar: jar,
},
InfoHandler: defaultHandler,
WarnHandler: defaultHandler,
infoHandler: defaultHandler,
warnHandler: defaultHandler,
debugHandler: defaultHandler,
}
insta.init()

Expand Down Expand Up @@ -381,9 +390,11 @@ func ImportConfig(config ConfigFile, args ...interface{}) (*Instagram, error) {
Proxy: http.ProxyFromEnvironment,
},
},
InfoHandler: defaultHandler,
WarnHandler: defaultHandler,
Account: config.Account,
Account: config.Account,

infoHandler: defaultHandler,
warnHandler: defaultHandler,
debugHandler: defaultHandler,
}
insta.userAgent = createUserAgent(insta.device)
insta.c.Jar, err = cookiejar.New(nil)
Expand Down Expand Up @@ -450,12 +461,12 @@ func (insta *Instagram) Login() (err error) {

err = insta.getPrefill()
if err != nil {
insta.WarnHandler("Non fatal error while fetching prefill:", err)
insta.warnHandler("Non fatal error while fetching prefill:", err)
}

err = insta.contactPrefill()
if err != nil {
insta.WarnHandler("Non fatal error while fetching contact prefill:", err)
insta.warnHandler("Non fatal error while fetching contact prefill:", err)
}

err = insta.sync()
Expand Down Expand Up @@ -506,15 +517,15 @@ func (insta *Instagram) OpenApp() (err error) {
defer wg.Done()
err := insta.getAccountFamily()
if err != nil {
insta.WarnHandler("Non fatal error while fetching account family:", err)
insta.warnHandler("Non fatal error while fetching account family:", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
if err := insta.getNdxSteps(); err != nil {
insta.WarnHandler("Non fatal error while fetching ndx steps:", err)
insta.warnHandler("Non fatal error while fetching ndx steps:", err)
}
}()

Expand All @@ -531,23 +542,23 @@ func (insta *Instagram) OpenApp() (err error) {
go func() {
defer wg.Done()
if err := insta.callNotifBadge(); err != nil {
insta.WarnHandler("Non fatal error while fetching notify badge", err)
insta.warnHandler("Non fatal error while fetching notify badge", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
if err := insta.banyan(); err != nil {
insta.WarnHandler("Non fatal error while fetching banyan", err)
insta.warnHandler("Non fatal error while fetching banyan", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
if err = insta.callMediaBlocked(); err != nil {
insta.WarnHandler("Non fatal error while fetching blocked media", err)
insta.warnHandler("Non fatal error while fetching blocked media", err)
}
}()

Expand All @@ -557,15 +568,15 @@ func (insta *Instagram) OpenApp() (err error) {
// no clue what theses values could be used for
_, err = insta.getCooldowns()
if err != nil {
insta.WarnHandler("Non fatal error while fetching cool downs", err)
insta.warnHandler("Non fatal error while fetching cool downs", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
if !insta.Discover.Next() {
insta.WarnHandler("Non fatal error while fetching explore page",
insta.warnHandler("Non fatal error while fetching explore page",
insta.Discover.Error())
}
}()
Expand All @@ -574,7 +585,7 @@ func (insta *Instagram) OpenApp() (err error) {
go func() {
defer wg.Done()
if err := insta.getConfig(); err != nil {
insta.WarnHandler("Non fatal error while fetching config", err)
insta.warnHandler("Non fatal error while fetching config", err)
}
}()

Expand All @@ -584,7 +595,7 @@ func (insta *Instagram) OpenApp() (err error) {
// no clue what theses values could be used for
_, err = insta.getScoresBootstrapUsers()
if err != nil {
insta.WarnHandler("Non fatal error while fetching bootstrap user scores", err)
insta.warnHandler("Non fatal error while fetching bootstrap user scores", err)
}
}()

Expand All @@ -601,15 +612,15 @@ func (insta *Instagram) OpenApp() (err error) {
go func() {
defer wg.Done()
if err := insta.sendAdID(); err != nil {
insta.WarnHandler("Non fatal error while sending ad id", err)
insta.warnHandler("Non fatal error while sending ad id", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
if err := insta.callStClPushPerm(); err != nil {
insta.WarnHandler("Non fatal error while calling store client push permissions", err)
insta.warnHandler("Non fatal error while calling store client push permissions", err)
}
}()

Expand All @@ -629,7 +640,7 @@ func (insta *Instagram) OpenApp() (err error) {
go func() {
defer wg.Done()
if err := insta.callContPointSig(); err != nil {
insta.WarnHandler("Non fatal error while calling contact point signal:", err)
insta.warnHandler("Non fatal error while calling contact point signal:", err)
}
}()

Expand Down Expand Up @@ -671,28 +682,26 @@ func (insta *Instagram) login() error {
if err != nil {
return err
}
body, h, reqErr := insta.sendRequest(
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlLogin,
Query: map[string]string{"signed_body": "SIGNATURE." + string(result)},
IsPost: true,
},
)
h.Clone()

insta.pass = ""
err = insta.verifyLogin(body)
if err == nil && reqErr != nil {
return reqErr
if err != nil {
return err
}
return err
return insta.verifyLogin(body)
}

func (insta *Instagram) verifyLogin(body []byte) error {
res := accountResp{}
err := json.Unmarshal(body, &res)
if err != nil {
return err
return fmt.Errorf("failed to parse json from login response with err: %s", err.Error())
}

if res.Status != "ok" {
Expand All @@ -702,7 +711,7 @@ func (insta *Instagram) verifyLogin(body []byte) error {
res.ErrorType, res.Message,
),
)
insta.WarnHandler(err)
insta.warnHandler(err)

switch res.ErrorType {
case "bad_password":
Expand Down Expand Up @@ -882,7 +891,7 @@ func (insta *Instagram) sync(args ...map[string]string) error {

id, err := strconv.Atoi(keyID)
if err != nil {
insta.WarnHandler(fmt.Errorf("Failed to parse public key id: %s", err))
insta.warnHandler(fmt.Errorf("Failed to parse public key id: %s", err))
}
insta.pubKey = key
insta.pubKeyID = id
Expand Down
13 changes: 11 additions & 2 deletions inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Conversation struct {
NewestCursor string `json:"newest_cursor"`
OldestCursor string `json:"oldest_cursor"`

LastPermanentItem Item `json:"last_permanent_item"`
LastPermanentItem InboxItem `json:"last_permanent_item"`
}

// InboxItem is any conversation message.
Expand All @@ -94,7 +94,7 @@ type InboxItem struct {
TqSeqID int `json:"tq_seq_id"`

// Type there are a few types:
// text, like, raven_media, action_log, media_share, reel_share
// text, like, raven_media, action_log, media_share, reel_share, link
Type string `json:"item_type"`

// Text is message text.
Expand All @@ -108,6 +108,15 @@ type InboxItem struct {
Reel *reelShare `json:"reel_share"`
Media *Item `json:"media_share"`
ActionLog *actionLog `json:"action_log"`
Link struct {
Text string `json:"text"`
Context struct {
Url string `json:"link_url"`
Title string `json:"link_title"`
Summary string `json:"link_summary"`
ImageUrl string `json:"link_image_url"`
} `json:"link_context"`
} `json:"link"`
}

type inboxResp struct {
Expand Down
2 changes: 1 addition & 1 deletion media.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func (item *Item) Download(folder, name string) (err error) {
return item.downloadCarousel(folder, name)
}

insta.WarnHandler(
insta.warnHandler(
fmt.Sprintf(
"Unable to download %s media (media type %d), this has not been implemented",
item.MediaToString(),
Expand Down
2 changes: 1 addition & 1 deletion profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (user *User) VisitProfile() (*Profile, error) {
defer wg.Done()
_, err := user.GetFeaturedAccounts()
if err != nil {
user.insta.WarnHandler(err)
user.insta.warnHandler(err)
}
}(wg)

Expand Down
5 changes: 5 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func (insta *Instagram) sendRequest(o *reqOptions) (body []byte, h http.Header,
if err == nil {
err = insta.isError(resp.StatusCode, body, resp.Status, o.Endpoint)
}
if insta.Debug {
insta.debugHandler(fmt.Errorf("Status code: %d : %s, body: %s,", resp.StatusCode, o.Endpoint, string(body)))
}
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -308,6 +311,8 @@ func (insta *Instagram) isError(code int, body []byte, status, endpoint string)
// ierr.ChallengeError.Process()

return ierr.ChallengeError
case "The password you entered is incorrect. Please try again.":
return ErrBadPassword
default:
return ierr
}
Expand Down
6 changes: 3 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (sb *Search) History() (*[]SearchHistory, error) {
return nil, err
}
if err := sb.NullState(); err != nil {
sb.insta.WarnHandler("Non fatal error while setting search null state", err)
sb.insta.warnHandler("Non fatal error while setting search null state", err)
}
return h, nil
}
Expand Down Expand Up @@ -281,12 +281,12 @@ func (sb *Search) search(query string, fn func(string) (*SearchResult, error)) (
}
h, err := sb.history()
if err != nil {
sb.insta.WarnHandler("Non fatal error while fetcihng recent search results",
sb.insta.warnHandler("Non fatal error while fetcihng recent search results",
err)
}
result.History = *h
if err := sb.NullState(); err != nil {
sb.insta.WarnHandler("Non fatal error while setting search null state", err)
sb.insta.warnHandler("Non fatal error while setting search null state", err)
}

var q string
Expand Down
14 changes: 7 additions & 7 deletions uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (insta *Instagram) Upload(o *UploadOptions) (*Item, error) {
return o.configureVideo()
}

insta.InfoHandler(fmt.Errorf("Unable to handle file upload with format %s", t))
insta.infoHandler(fmt.Errorf("Unable to handle file upload with format %s", t))
return nil, ErrInvalidFormat
}

Expand Down Expand Up @@ -718,7 +718,7 @@ func (o *UploadOptions) uploadAlbum() (*Item, error) {
// Validate file type
t := http.DetectContentType(buf.Bytes())
if !(t == "image/jpeg" || t == "video/mp4") {
insta.InfoHandler(fmt.Errorf("Unable to handle file upload with format %s", t))
insta.infoHandler(fmt.Errorf("Unable to handle file upload with format %s", t))
return nil, ErrInvalidFormat
}

Expand Down Expand Up @@ -815,11 +815,11 @@ func (o *UploadOptions) configure() (*Item, error) {
if res.Status != "ok" {
switch res.Message {
case "Transcode not finished yet.":
insta.InfoHandler(fmt.Errorf("%s, %s. Please wait.", res.Status, res.Message))
insta.infoHandler(fmt.Errorf("%s, %s. Please wait.", res.Status, res.Message))
time.Sleep(6 * time.Second)
return o.configure()
case "media_needs_reupload":
insta.InfoHandler(fmt.Errorf("Instagram asks for the video to be reuploaded, please wait."))
insta.infoHandler(fmt.Errorf("Instagram asks for the video to be reuploaded, please wait."))
err := o.postVideo()
if err != nil {
return nil, err
Expand Down Expand Up @@ -878,7 +878,7 @@ func (o *UploadOptions) uploadMultiStory() (*Item, error) {
o.width, o.height, o.duration = width, height, duration

size := float64(len(o.buf.Bytes())) / 1000000.0
o.insta.InfoHandler(
o.insta.infoHandler(
fmt.Sprintf(
"Uploading story video %d: duration: %ds, Size: %dx%d, %.2f Mb",
i+1, duration/1000, width, height, size,
Expand Down Expand Up @@ -931,7 +931,7 @@ func (o *UploadOptions) uploadVideo() error {
o.width, o.height, o.duration = width, height, duration

size := float64(len(o.buf.Bytes())) / 1000000.0
o.insta.InfoHandler(
o.insta.infoHandler(
fmt.Sprintf(
"Upload video: duration: %ds, Size: %dx%d, %.2f Mb",
duration/1000, width, height, size,
Expand Down Expand Up @@ -1010,7 +1010,7 @@ func (o *UploadOptions) segmentVideo(t int) error {
return err
}
o.offset += len(segment)
o.insta.InfoHandler(fmt.Sprintf("Uploaded video segment [%d/%d]", i+1, length))
o.insta.infoHandler(fmt.Sprintf("Uploaded video segment [%d/%d]", i+1, length))

}

Expand Down

0 comments on commit a0ea1d5

Please sign in to comment.