Documentation
¶
Overview ¶
Package miner is the core of this project, use to request for http api.
Example:
package main import ( "fmt" "github.com/admpub/marmot/miner" ) func main() { // Use Default Worker, You can Also New One: // worker:=miner.New(nil) miner.SetLogLevel(miner.DEBUG) _, err := miner.SetURL("https://www.whitehouse.gov").Go() if err != nil { fmt.Println(err.Error()) } else { fmt.Println(miner.String()) } }
Index ¶
- Constants
- Variables
- func CloneHeader(h map[string][]string) map[string][]string
- func CopyM(h http.Header) http.Header
- func Delete() (body []byte, e error)
- func FixCharset(body []byte, extra interface{}, detectCharset bool, defaultEncoding ...string) ([]byte, error)
- func Get() (body []byte, e error)
- func GetCookies() []*http.Cookie
- func Go() (body []byte, e error)
- func GoByMethod(method string) (body []byte, e error)
- func JSONToString() (string, error)
- func MergeCookie(before []*http.Cookie, after []*http.Cookie) []*http.Cookie
- func NewClient(timeouts ...time.Duration) *http.Client
- func NewHTTPClient(options ...com.HTTPClientOptions) *http.Client
- func NewJar() *cookiejar.Jar
- func NewProxyClient(proxystring string, timeouts ...time.Duration) (*http.Client, error)
- func OtherGo(method, contenttype string) (body []byte, e error)
- func OutputMaps(info string, args map[string][]string)
- func Post() (body []byte, e error)
- func PostFile() (body []byte, e error)
- func PostJSON() (body []byte, e error)
- func PostXML() (body []byte, e error)
- func Put() (body []byte, e error)
- func PutFile() (body []byte, e error)
- func PutJSON() (body []byte, e error)
- func PutXML() (body []byte, e error)
- func RandomUserAgent() string
- func SetGlobalTimeout(num int)
- func String() string
- func TooShortSizes(data []byte, sizes float64) error
- func UserAgentInit()
- func Wait(waittime int)
- type Worker
- func Clear() *Worker
- func ClearAll() *Worker
- func ClearCookie() *Worker
- func New(ipstring interface{}, timeout ...time.Duration) (*Worker, error)
- func NewAPI(timeout ...time.Duration) *Worker
- func NewWorker(ipstring interface{}, timeout ...time.Duration) (*Worker, error)
- func NewWorkerByClient(client *http.Client) *Worker
- func SetAfterAction(fc func(context.Context, *Worker)) *Worker
- func SetBeforeAction(fc func(context.Context, *Worker)) *Worker
- func SetBinary(data []byte) *Worker
- func SetContext(ctx context.Context) *Worker
- func SetCookie(v string) *Worker
- func SetCookieByFile(file string) (*Worker, error)
- func SetDetectCharset(on bool) *Worker
- func SetFileInfo(fileName, fileFormName string) *Worker
- func SetForm(form url.Values) *Worker
- func SetFormParm(k, v string) *Worker
- func SetHeader(header http.Header) *Worker
- func SetHeaderParm(k, v string) *Worker
- func SetMaxRetries(maxRetries int) *Worker
- func SetMethod(method string) *Worker
- func SetPesterOptions(options ...pester.ApplyOptions) *Worker
- func SetRefer(refer string) *Worker
- func SetResponseCharset(charset string) *Worker
- func SetURL(url string) *Worker
- func SetUserAgent(ua string) *Worker
- func SetWaitTime(num int) *Worker
- func (worker *Worker) Clear() *Worker
- func (worker *Worker) ClearAll() *Worker
- func (worker *Worker) ClearCookie() *Worker
- func (worker *Worker) Delete() (body []byte, e error)
- func (worker *Worker) Get() (body []byte, e error)
- func (worker *Worker) GetCookies() []*http.Cookie
- func (worker *Worker) Go() (body []byte, e error)
- func (worker *Worker) GoByMethod(method string) (body []byte, e error)
- func (worker *Worker) JSONToString() (string, error)
- func (worker *Worker) OtherGo(method, contentType string) (body []byte, e error)
- func (worker *Worker) OtherGoBinary(method, contentType string) (body []byte, e error)
- func (worker *Worker) Post() (body []byte, e error)
- func (worker *Worker) PostFile() (body []byte, e error)
- func (worker *Worker) PostJSON() (body []byte, e error)
- func (worker *Worker) PostXML() (body []byte, e error)
- func (worker *Worker) Put() (body []byte, e error)
- func (worker *Worker) PutFile() (body []byte, e error)
- func (worker *Worker) PutJSON() (body []byte, e error)
- func (worker *Worker) PutXML() (body []byte, e error)
- func (worker *Worker) SetAfterAction(fc func(context.Context, *Worker)) *Worker
- func (worker *Worker) SetBeforeAction(fc func(context.Context, *Worker)) *Worker
- func (worker *Worker) SetBinary(data []byte) *Worker
- func (worker *Worker) SetContext(ctx context.Context) *Worker
- func (worker *Worker) SetCookie(v string) *Worker
- func (worker *Worker) SetCookieByFile(file string) (*Worker, error)
- func (worker *Worker) SetDetectCharset(on bool) *Worker
- func (worker *Worker) SetFileInfo(fileName, fileFormName string) *Worker
- func (worker *Worker) SetForm(form url.Values) *Worker
- func (worker *Worker) SetFormParm(k, v string) *Worker
- func (worker *Worker) SetHeader(header http.Header) *Worker
- func (worker *Worker) SetHeaderParm(k, v string) *Worker
- func (worker *Worker) SetHost(host string) *Worker
- func (worker *Worker) SetMaxRetries(maxRetries int) *Worker
- func (worker *Worker) SetMethod(method string) *Worker
- func (worker *Worker) SetPesterOptions(options ...pester.ApplyOptions) *Worker
- func (worker *Worker) SetRefer(refer string) *Worker
- func (worker *Worker) SetResponseCharset(charset string) *Worker
- func (worker *Worker) SetURL(url string) *Worker
- func (worker *Worker) SetUserAgent(ua string) *Worker
- func (worker *Worker) SetWaitTime(num int) *Worker
- func (worker *Worker) String() string
Constants ¶
const ( // WaitTime Default wait time WaitTime = 5 GET = "GET" POST = "POST" POSTJSON = "POSTJSON" POSTXML = "POSTXML" POSTFILE = "POSTFILE" PUT = "PUT" PUTJSON = "PUTJSON" PUTXML = "PUTXML" PUTFILE = "PUTFILE" DELETE = "DELETE" OTHER = "OTHER" // this stand for you can use other method this lib not own. HTTPFORMContentType = "application/x-www-form-urlencoded" HTTPJSONContentType = "application/json" HTTPXMLContentType = "text/xml" HTTPFILEContentType = "multipart/form-data" )
Variables ¶
var ( Debugf = log.Debugf CheckRedirect = func(req *http.Request, via []*http.Request) error { Debugf("[GoWorker] Redirect:%v", req.URL) return nil } )
var ( // Save Cookie, No timeout! Client = NewClient() // Not Save Cookie NoCookieClient = NewHTTPClient( httpClientOptions.Timeout(time.Second*time.Duration(DefaultTimeOut)), httpClientOptions.CheckRedirect(CheckRedirect), ) )
Default Client
var ( DefaultHeader = map[string][]string{ "User-Agent": { defaultUserAgent, }, } // DefaultTimeOut http get and post No timeout DefaultTimeOut = 30 )
var Pool = &_Workers{ws: make(map[string]*Worker)}
Pool for many Worker, every Worker can only serial execution
var UserAgentf http.FileSystem
var UserAgents = map[int]string{}
UserAgents Global User-Agent provide
Functions ¶
func CloneHeader ¶
CloneHeader Clone a header, If not exist Ua, Set our Ua!
func CopyM ¶
CopyM Header map[string][]string ,can use to copy a http header, so that they are not effect each other
func FixCharset ¶
func GetCookies ¶
func GoByMethod ¶
func JSONToString ¶
JSONToString This make effect only your Worker exec serial! Attention! Change Your JSON'like Raw data to string
func MergeCookie ¶
MergeCookie Merge Cookie, not use
func NewHTTPClient ¶
func NewHTTPClient(options ...com.HTTPClientOptions) *http.Client
NewHTTPClient New a client
func NewProxyClient ¶
NewProxyClient New a Proxy client, Default save cookie, Can timeout We should support some proxy way such as http(s) or socks
func OutputMaps ¶
OutputMaps Just debug a map
func SetGlobalTimeout ¶
func SetGlobalTimeout(num int)
SetGlobalTimeout Set global timeout, it can only by this way!
func String ¶
func String() string
ToString This make effect only your Worker exec serial! Attention! Change Your Raw data To string
func TooShortSizes ¶
TooShortSizes if a file size small than sizes(KB) ,it will be throw a error
Types ¶
type Worker ¶
type Worker struct { // In order fast chain func call I put the basic config below URL string // Which url we want Method string // Get/Post method Header http.Header // Http header Data url.Values // Sent by form data FileName string // FileName which sent to remote FileFormName string // File Form Name which sent to remote BinaryData []byte // Sent by binary data, can together with File Wait int // Wait Time Client *http.Client // Our Client Request *http.Request // Debug Response *http.Response // Debug Raw []byte // Raw data we get // The name below is not so good but has already been used in many project, so bear it. PreURL string // Pre url StatusCode int // the last url response code, such as 404 FetchTimes int // Url fetch number times ErrorTimes int // Url fetch error times IP string // worker proxy ip, just for user to record their proxy ip, default: localhost // AOP like Java Ctx context.Context BeforeAction func(context.Context, *Worker) AfterAction func(context.Context, *Worker) PesterOptions []pester.ApplyOptions // ResponseCharset is the character encoding of the response body. // Leave it blank to allow automatic character encoding of the response body. ResponseCharset string // DetectCharset can enable character encoding detection for non-utf8 response bodies // without explicit charset declaration. This feature uses https://github.com/webx-top/chardet DetectCharset bool MaxRetries int // contains filtered or unexported fields }
Worker is the main object to sent http request and return result of response
var DefaultWorker *Worker
DefaultWorker Global Worker
func ClearCookie ¶
func ClearCookie() *Worker
func NewWorker ¶
NewWorker New a worker, if ipstring is a proxy address, New a proxy client. Proxy address such as:
http://[user]:[password@]ip:port, [] stand it can choose or not. case: socks5://127.0.0.1:1080
func NewWorkerByClient ¶
NewWorkerByClient New Worker by Your Client
func SetContext ¶
func SetCookieByFile ¶
func SetDetectCharset ¶
func SetFileInfo ¶
func SetFormParm ¶
func SetHeaderParm ¶
func SetMaxRetries ¶
func SetPesterOptions ¶
func SetPesterOptions(options ...pester.ApplyOptions) *Worker
func SetResponseCharset ¶
func SetUserAgent ¶
func SetWaitTime ¶
func (*Worker) GetCookies ¶
GetCookies Get Cookies
func (*Worker) JSONToString ¶
JSONToString This make effect only your worker exec serial! Attention! Change Your JSON'like Raw data to string
func (*Worker) OtherGo ¶
OtherGo Other Method
Method = "OPTIONS" ; Section 9.2 | "GET" ; Section 9.3 | "HEAD" ; Section 9.4 | "POST" ; Section 9.5 | "PUT" ; Section 9.6 | "DELETE" ; Section 9.7 | "TRACE" ; Section 9.8 | "CONNECT" ; Section 9.9 | extension-method extension-method = token token = 1*<any CHAR except CTLs or separators>
Content Type
"application/x-www-form-urlencoded" "application/json" "text/xml" "multipart/form-data"
func (*Worker) OtherGoBinary ¶
func (*Worker) Post ¶
Post Almost include bellow:
"application/x-www-form-urlencoded" "application/json" "text/xml" "multipart/form-data"
func (*Worker) SetAfterAction ¶
func (*Worker) SetBeforeAction ¶
func (*Worker) SetContext ¶
Set Context so Action can soft
func (*Worker) SetCookieByFile ¶
SetCookieByFile Set Cookie by file.
func (*Worker) SetDetectCharset ¶
func (*Worker) SetFileInfo ¶
func (*Worker) SetFormParm ¶
func (*Worker) SetHeaderParm ¶
func (*Worker) SetMaxRetries ¶
func (*Worker) SetPesterOptions ¶
func (worker *Worker) SetPesterOptions(options ...pester.ApplyOptions) *Worker