-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04f8f1c
commit 14cc7c4
Showing
7 changed files
with
135 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package login | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func (s *Session) Kaptcha(kaptcha func(*Session) (string, error)) error { | ||
if s == nil { | ||
s = New() | ||
} | ||
if s.login == nil { | ||
return ErrNilLogin | ||
} | ||
|
||
data, err := kaptcha(s) | ||
if err != nil { | ||
time.Sleep(s.interval) | ||
if s.retry > 0 { | ||
s.retry-- | ||
return s.Kaptcha(kaptcha) | ||
} | ||
return fmt.Errorf("max wrong retry: failed to get kaptcha: %s", err) | ||
} | ||
|
||
if err = s.login(s, data); err != nil { | ||
time.Sleep(s.interval) | ||
if s.retry > 0 { | ||
s.retry-- | ||
return s.Kaptcha(kaptcha) | ||
} | ||
return fmt.Errorf("max wrong retry: failed to login: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package login | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/sunshineplan/gohttp" | ||
) | ||
|
||
const defaultWrongRetry = 5 | ||
const defaultRetryInterval = 5 * time.Second | ||
|
||
var ErrNilLogin = errors.New("nil login function") | ||
|
||
var SetAgent = gohttp.SetAgent | ||
|
||
type Session struct { | ||
*gohttp.Session | ||
login func(*Session, interface{}) error | ||
retry int | ||
interval time.Duration | ||
} | ||
|
||
func New() *Session { | ||
return &Session{ | ||
Session: gohttp.NewSession(), | ||
retry: defaultWrongRetry, | ||
interval: defaultRetryInterval, | ||
} | ||
} | ||
|
||
func (s *Session) SetSession(session *gohttp.Session) *Session { | ||
s.Session = session | ||
return s | ||
} | ||
|
||
func (s *Session) SetWrongRetry(n int) *Session { | ||
s.retry = n | ||
return s | ||
} | ||
|
||
func (s *Session) SetRetryInterval(d time.Duration) *Session { | ||
s.interval = d | ||
return s | ||
} | ||
|
||
func (s *Session) SetLogin(fn func(*Session, interface{}) error) *Session { | ||
s.login = fn | ||
return s | ||
} | ||
|
||
func (s *Session) Login() error { | ||
return s.login(s, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters