From 53c54d375c783b209b82538a5b8c9806e6fc7d4c Mon Sep 17 00:00:00 2001 From: Mikhail Klementyev Date: Wed, 20 Jul 2016 01:08:19 +0300 Subject: [PATCH] Add base implementation --- main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..739209d --- /dev/null +++ b/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/jaytaylor/html2text" + "golang.org/x/net/html/charset" + kingpin "gopkg.in/alecthomas/kingpin.v2" +) + +var ( + url = kingpin.Flag("url", "Url").String() +) + +func main() { + + kingpin.Parse() + + client := &http.Client{} + + req, err := http.NewRequest("GET", *url, nil) + if err != nil { + log.Fatalln(err) + } + + req.Header.Set("User-Agent", "Wi 0.0") + + resp, err := client.Do(req) + if err != nil { + log.Fatalln(err) + } + + defer resp.Body.Close() + + utf8, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type")) + if err != nil { + fmt.Println("Encoding error:", err) + return + } + + body, err := ioutil.ReadAll(utf8) + if err != nil { + fmt.Println("IO error:", err) + return + } + + text, err := html2text.FromString(string(body)) + if err != nil { + panic(err) + } + + fmt.Println(text) +}