diff --git a/README.md b/README.md index 981f6fc..9962b0b 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,22 @@ go install github.com/Akimon658/ogjson@latest Then you can use `ogjson` command. +## Advanced usage + +### Change `User-Agent` + +There are some websites that `ogjson` cannot access by default because of `User-Agent`. +To avoid it, you can use `user-agent` flag. + +```bash +ogjson -user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" +``` + +``` +$ curl "http://localhost:8080/?url=https://docs.github.com" +{"Policy":{"TrustedTags":["meta","link","title"]},"Title":"GitHub.com Help Documentation","Type":"article","URL":{"Source":"https://docs.github.com","Scheme":"https","Opaque":"","User":null,"Host":"docs.github.com","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Value":"http://ghdocs-prod.azurewebsites.net:80/en"},"SiteName":"GitHub Docs","Image":[{"URL":"https://assets-git.f3mw1.com/images/modules/open_graph/github-logo.png","SURL":"","Type":"","Width":0,"Height":0,"Alt":""}],"Video":[],"Audio":[],"Description":"Get started, troubleshoot, and make the most of GitHub. Documentation for new users, developers, administrators, and all of GitHub's products.","Determiner":"","Locale":"","LocaleAlt":[],"Favicon":"/assets/cb-803/images/site/favicon.svg"} +``` + ## License [MIT](./LICENSE) diff --git a/main.go b/main.go index 25ee4c5..d4251ef 100644 --- a/main.go +++ b/main.go @@ -2,25 +2,50 @@ package main import ( "encoding/json" + "flag" "log" "net/http" "github.com/otiai10/opengraph" ) +type handler func(http.ResponseWriter, *http.Request) error + +func (f handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if err := f(w, r); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - ogp, err := opengraph.Fetch(r.FormValue("url")) + ua := flag.String("user-agent", "Ogjson/1.1", "Value of User-Agent") + flag.Parse() + + http.Handle("/", handler(func(w http.ResponseWriter, r *http.Request) error { + url := r.FormValue("url") + req, err := http.NewRequest("GET", url, nil) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + return err } + req.Header.Add("User-Agent", *ua) - w.Header().Set("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } - if err = json.NewEncoder(w).Encode(ogp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + og := opengraph.New(url) + if err = og.Parse(resp.Body); err != nil { + return err } - }) + + w.Header().Set("Content-Type", "application/json") + if err = json.NewEncoder(w).Encode(og); err != nil { + return err + } + + return nil + })) log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil)) }