-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (56 loc) · 1.38 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"github.com/JustinTulloss/gogetter"
"github.com/JustinTulloss/hut"
)
var service *hut.Service
var scraper *gogetter.Scraper
func handler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
service.ErrorReply(err, w)
return
}
decodedUrl, err := url.QueryUnescape(r.Form.Get("url"))
if err != nil {
service.ErrorReply(err, w)
return
}
tags, httpErr := scraper.ScrapeTags(decodedUrl)
if httpErr != nil {
service.HttpErrorReply(w, httpErr.Error(), http.StatusInternalServerError)
return
}
service.Reply(tags, w)
}
func main() {
var err error
service = hut.NewService(nil)
service.Router.HandleFunc("/", handler)
scraper, err = gogetter.NewScraper("", service.Env.GetBool("check_robots_txt"))
if err != nil {
service.Log.Fatal("Could not create a scraper", "err", err)
}
flag.Parse()
protocol := service.Env.GetString("protocol")
if protocol == "http" {
service.Start()
} else if len(flag.Args()) != 0 {
tags, err := scraper.ScrapeTags(flag.Arg(0))
if err != nil {
fmt.Printf("Could not fetch: %s\n", err)
return
}
jsonTags, err := json.MarshalIndent(tags, "", "\t")
if err != nil {
fmt.Printf("Could not marshal json: %s\n", err)
}
fmt.Println(string(jsonTags))
} else {
panic("Need to use this properly and I need to print usage info!")
}
}