learning_go/book_with_bridge/OMDBAPI/main.go
Alexander Pivkin 48f498a1dd OMDB_api
2025-12-27 19:20:28 +03:00

66 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"text/template"
"net/http"
"os"
)
const (
URL = "http://www.omdbapi.com"
KEY = "fa22ab55"
)
type SearchRes struct {
Title string `json:"Title"`
Year string `json:"Year"`
Genre string `json:"Genre"`
Ratings []Ratings
}
type Ratings struct {
Source string `json:"Source"`
Value string `json:"Value"`
}
const templ = `
=======================
Title: {{.Title}}
Year: {{.Year}}
Genre: {{.Genre}}
Rating:
{{range .Ratings}}
Source: {{.Source}}
Rating: {{.Value}}
{{end}}
=======================
`
var report = template.Must(template.New("MovieReport").Parse(templ))
func Search(s string) (SearchRes,error) {
resp,err := http.Get(URL + "?apikey=" + KEY + "&t=" + s)
if err != nil {
return SearchRes{},err
}
var res SearchRes
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
fmt.Print("Error in unmarshalling ",err)
}
return res,nil
}
func main() {
arg := os.Args[1]
// fmt.Println(arg)
res,_ := Search(arg)
if err := report.Execute(os.Stdout,res); err != nil {
fmt.Printf("Cannot show report ",err)
}
}