11package app
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "flag"
57 "fmt"
68 "io"
@@ -19,29 +21,56 @@ var (
1921 optURL * string
2022 optAPIToken * string
2123 optCountryCode * string
24+ optAuthUsername * string
25+ optAuthPassword * string
26+ optCookie * string
27+ optReferer * string
28+ optSelector * string
2229
2330 usage = `
24- usage: scraper-cli [options ...]
31+ usage: scraper-cli [flags ...]
2532
2633 scraper-cli is a command-line interface for Prompt API's Scraper API. Details
2734 can be found:
2835
29- https://promptapi.com/marketplace/description/scraper-api
36+ https://promptapi.com/marketplace/description/scraper-api
3037
3138 you need to signup for Prompt API to get your PROMPTAPI_TOKEN. you can signup
3239 from:
3340
34- https://promptapi.com/#signup-form
41+ https://promptapi.com/#signup-form
3542
3643 application looks for PROMPTAPI_TOKEN environment variable. if you pass
3744 "token" flag, this will override environment variable lookup.
3845
39- example token usage :
46+ required flag(s) :
4047
41- $ PROMPTAPI_TOKEN="your-api-key" scraper-cli -url "https://promptapi.com" # or
42- $ scraper-cli -url "https://promptapi.com" -token "your-api-key"
48+ -url web url/address to scrape
49+
50+
51+ optional flags:
52+
53+ -country 2 character country code
54+ -token promptapi apikey instead of PROMPTAPI_TOKEN env-var
55+ -username for HTTP Realm auth username
56+ -password for HTTP Realm auth password
57+ -cookie URL Encoded cookie header
58+ -referer HTTP referer header
59+ -selector CSS style selector path such as: a.btn div li
60+ -version display version information
61+ -help, -h display help
62+
63+
64+ examples:
65+
66+ $ scraper-cli -help
67+ $ scraper-cli -url "https://promptapi.com"
68+ $ scraper-cli -url "https://promptapi.com" -country "EE"
69+ $ scraper-cli -url "https://promptapi.com" -country "EE" -selector "a.btn div li"
70+
71+ $ PROMPTAPI_TOKEN="your-api-key" scraper-cli -url "https://promptapi.com"
72+ $ scraper-cli -url "https://promptapi.com" -token "your-api-key"
4373
44- options:
4574
4675`
4776)
@@ -55,12 +84,16 @@ type CLIApplication struct {
5584func NewCLIApplication () * CLIApplication {
5685 flag .Usage = func () {
5786 fmt .Fprint (os .Stderr , usage )
58- flag .PrintDefaults ()
5987 }
6088 optVersionInformation = flag .Bool ("version" , false , "display version information" )
6189 optURL = flag .String ("url" , "" , "web url/address to scrape" )
6290 optAPIToken = flag .String ("token" , "n/a" , "use this flag to override PROMPTAPI_TOKEN environment variable" )
63- optCountryCode = flag .String ("country" , "n/a" , "2 character country code." )
91+ optCountryCode = flag .String ("country" , "n/a" , "2 character country code" )
92+ optAuthUsername = flag .String ("username" , "n/a" , "for HTTP Realm auth username" )
93+ optAuthPassword = flag .String ("password" , "n/a" , "for HTTP Realm auth password" )
94+ optCookie = flag .String ("cookie" , "n/a" , "URL Encoded cookie header" )
95+ optReferer = flag .String ("referer" , "n/a" , "HTTP referer header" )
96+ optSelector = flag .String ("selector" , "n/a" , "CSS style selector path such as: a.btn div li" )
6497
6598 flag .Parse ()
6699
@@ -96,6 +129,10 @@ func (c *CLIApplication) Validate() error {
96129 return err
97130 }
98131
132+ if * optAPIToken != "n/a" {
133+ os .Setenv ("PROMPTAPI_TOKEN" , * optAPIToken )
134+ }
135+
99136 return nil
100137}
101138
@@ -109,14 +146,42 @@ func (c *CLIApplication) Scrape() error {
109146
110147 if * optCountryCode != "n/a" {
111148 params .Country = * optCountryCode
112- fmt .Printf ("%v\n " , params .Country )
149+ }
150+ if * optAuthUsername != "n/a" {
151+ params .AuthUsername = * optAuthUsername
152+ }
153+ if * optAuthPassword != "n/a" {
154+ params .AuthPassword = * optAuthPassword
155+ }
156+ if * optCookie != "n/a" {
157+ params .Cookie = * optCookie
158+ }
159+ if * optReferer != "n/a" {
160+ params .Referer = * optReferer
161+ }
162+ if * optSelector != "n/a" {
163+ params .Selector = * optSelector
113164 }
114165
115166 result := new (scraper.Result )
116167 err := s .Scrape (params , result )
117168 if err != nil {
118169 return err
119170 }
120- fmt .Fprintf (c .Out , "Content-Length: %v" , result .Headers ["Content-Length" ])
171+
172+ outData := result .Data
173+
174+ if len (result .DataSelector ) > 0 {
175+ buffer := new (bytes.Buffer )
176+ encoder := json .NewEncoder (buffer )
177+ encoder .SetEscapeHTML (false )
178+ err := encoder .Encode (result .DataSelector )
179+ if err != nil {
180+ return err
181+ }
182+ outData = buffer .String ()
183+ }
184+
185+ fmt .Fprintf (c .Out , outData )
121186 return nil
122187}
0 commit comments