-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
37 lines (32 loc) · 867 Bytes
/
scraper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package scraper
import (
"flag"
"log"
)
// ScrapeWithArgs parse from arguments url and tag
func ScrapeWithArgs(url string, tag string) string {
return scrape(url, tag)
}
// ScrapeWithFlags parse from flags --url and --tag
func ScrapeWithFlags() string {
url, tag := getFromFlags()
return scrape(url, tag)
}
func scrape(u string, tag string) string {
response := fetch(u)
parsed := parseHTMLPage(response, tag)
strJSON := toJSONfrom(parsed)
return strJSON
}
func getFromFlags() (string, string) {
urlArgHint := "enter url to parse"
tagArgHint := "(optional) enter a specific tag to retrieve only"
urlFlag := flag.String("url", "", urlArgHint)
tagFlag := flag.String("tag", "", tagArgHint)
flag.Parse()
if *urlFlag == "" {
log.Fatal("must specify --url flag")
}
println("parsing url:", *urlFlag, "| tag:", *tagFlag)
return *urlFlag, *tagFlag
}