Skip to content

Commit b537d40

Browse files
author
Uğur Özyılmazel
committed
Add -header flag
1 parent 04431df commit b537d40

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ usage: scraper-cli [flags...]
6262
-referer HTTP referer header
6363
-selector CSS style selector path such as: a.btn div li
6464
-version display version information
65+
-header request header(s)
6566
-help, -h display help
6667
6768
@@ -74,6 +75,8 @@ usage: scraper-cli [flags...]
7475
7576
$ PROMPTAPI_TOKEN="your-api-key" scraper-cli -url "https://promptapi.com"
7677
$ scraper-cli -url "https://promptapi.com" -token "your-api-key"
78+
$ scraper-cli -url "https://promptapi.com" -token "your-api-key" -header "X-Referer: https://www.google.com"
79+
$ scraper-cli -url "https://promptapi.com" -token "your-api-key" -header "X-Referer: https://www.google.com" -header "X-Custom-Header: Hello"
7780
```
7881
7982
---

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/promptapi/scraper-cli
22

33
go 1.15
44

5-
require github.com/promptapi/scraper-go v0.1.2
5+
require github.com/promptapi/scraper-go v0.1.4

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
github.com/promptapi/scraper-go v0.1.2 h1:iYCVbrONu7hNkV2US1S7Qtn55w+wmDuZS17DPH/ifuE=
22
github.com/promptapi/scraper-go v0.1.2/go.mod h1:ABmGIUogizw4JUmpR5gRXvT4KLgjgrJ8LFOnonjd2cw=
3+
github.com/promptapi/scraper-go v0.1.3 h1:wMgvae6Vj8asPN0YNBcNtYZjgAi8zhxLfI4cbroJ93I=
4+
github.com/promptapi/scraper-go v0.1.3/go.mod h1:ABmGIUogizw4JUmpR5gRXvT4KLgjgrJ8LFOnonjd2cw=
5+
github.com/promptapi/scraper-go v0.1.4 h1:x5ijz9D8K9VicMVakesyYzpWDu5tl/XYgMbVSHgLwRU=
6+
github.com/promptapi/scraper-go v0.1.4/go.mod h1:ABmGIUogizw4JUmpR5gRXvT4KLgjgrJ8LFOnonjd2cw=

pkg/app/app.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/url"
1010
"os"
11+
"strings"
1112

1213
scraper "github.com/promptapi/scraper-go"
1314
)
@@ -26,6 +27,7 @@ var (
2627
optCookie *string
2728
optReferer *string
2829
optSelector *string
30+
optHeaders headersFlag
2931

3032
usage = `
3133
usage: scraper-cli [flags...]
@@ -58,6 +60,7 @@ usage: scraper-cli [flags...]
5860
-referer HTTP referer header
5961
-selector CSS style selector path such as: a.btn div li
6062
-version display version information
63+
-header request header(s)
6164
-help, -h display help
6265
6366
@@ -70,11 +73,24 @@ usage: scraper-cli [flags...]
7073
7174
$ PROMPTAPI_TOKEN="your-api-key" scraper-cli -url "https://promptapi.com"
7275
$ scraper-cli -url "https://promptapi.com" -token "your-api-key"
76+
$ scraper-cli -url "https://promptapi.com" -token "your-api-key" -header "X-Referer: https://www.google.com"
77+
$ scraper-cli -url "https://promptapi.com" -token "your-api-key" -header "X-Referer: https://www.google.com" -header "X-Custom-Header: Hello"
7378
7479
7580
`
7681
)
7782

83+
type headersFlag []string
84+
85+
func (h *headersFlag) String() string {
86+
return "headers"
87+
}
88+
89+
func (h *headersFlag) Set(value string) error {
90+
*h = append(*h, strings.TrimSpace(value))
91+
return nil
92+
}
93+
7894
// CLIApplication represents app structure
7995
type CLIApplication struct {
8096
Out io.Writer
@@ -94,6 +110,7 @@ func NewCLIApplication() *CLIApplication {
94110
optCookie = flag.String("cookie", "n/a", "URL Encoded cookie header")
95111
optReferer = flag.String("referer", "n/a", "HTTP referer header")
96112
optSelector = flag.String("selector", "n/a", "CSS style selector path such as: a.btn div li")
113+
flag.Var(&optHeaders, "header", "")
97114

98115
flag.Parse()
99116

@@ -164,7 +181,19 @@ func (c *CLIApplication) Scrape() error {
164181
}
165182

166183
result := new(scraper.Result)
167-
err := s.Scrape(params, result)
184+
extraHeaders := []*scraper.ExtraHeader{}
185+
186+
if len(optHeaders) > 0 {
187+
for _, headerValue := range optHeaders {
188+
valuePair := strings.Split(headerValue, ":")
189+
reqHeader := new(scraper.ExtraHeader)
190+
reqHeader.Name = valuePair[0]
191+
reqHeader.Value = valuePair[1]
192+
extraHeaders = append(extraHeaders, reqHeader)
193+
}
194+
}
195+
196+
err := s.Scrape(params, extraHeaders, result)
168197
if err != nil {
169198
return err
170199
}

0 commit comments

Comments
 (0)