Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 16cb868

Browse files
authored
Merge pull request #4 from Akimon658/user-agent
Add `user-agent` flag
2 parents da6a875 + a0d15a6 commit 16cb868

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ go install github.com/Akimon658/ogjson@latest
3232

3333
Then you can use `ogjson` command.
3434

35+
## Advanced usage
36+
37+
### Change `User-Agent`
38+
39+
There are some websites that `ogjson` cannot access by default because of `User-Agent`.
40+
To avoid it, you can use `user-agent` flag.
41+
42+
```bash
43+
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"
44+
```
45+
46+
```
47+
$ curl "http://localhost:8080/?url=https://docs.github.com"
48+
{"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://github.githubassets.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"}
49+
```
50+
3551
## License
3652
[MIT](./LICENSE)
3753

main.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,50 @@ package main
22

33
import (
44
"encoding/json"
5+
"flag"
56
"log"
67
"net/http"
78

89
"github.com/otiai10/opengraph"
910
)
1011

12+
type handler func(http.ResponseWriter, *http.Request) error
13+
14+
func (f handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
15+
if err := f(w, r); err != nil {
16+
http.Error(w, err.Error(), http.StatusInternalServerError)
17+
}
18+
}
19+
1120
func main() {
12-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
13-
ogp, err := opengraph.Fetch(r.FormValue("url"))
21+
ua := flag.String("user-agent", "Ogjson/1.1", "Value of User-Agent")
22+
flag.Parse()
23+
24+
http.Handle("/", handler(func(w http.ResponseWriter, r *http.Request) error {
25+
url := r.FormValue("url")
26+
req, err := http.NewRequest("GET", url, nil)
1427
if err != nil {
15-
http.Error(w, err.Error(), http.StatusInternalServerError)
28+
return err
1629
}
30+
req.Header.Add("User-Agent", *ua)
1731

18-
w.Header().Set("Content-Type", "application/json")
32+
resp, err := http.DefaultClient.Do(req)
33+
if err != nil {
34+
return err
35+
}
1936

20-
if err = json.NewEncoder(w).Encode(ogp); err != nil {
21-
http.Error(w, err.Error(), http.StatusInternalServerError)
37+
og := opengraph.New(url)
38+
if err = og.Parse(resp.Body); err != nil {
39+
return err
2240
}
23-
})
41+
42+
w.Header().Set("Content-Type", "application/json")
43+
if err = json.NewEncoder(w).Encode(og); err != nil {
44+
return err
45+
}
46+
47+
return nil
48+
}))
2449

2550
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
2651
}

0 commit comments

Comments
 (0)