sdf
This commit is contained in:
parent
0124fc6ace
commit
c1bae790ed
3
go_book/OMDBAPI/go.mod
Normal file
3
go_book/OMDBAPI/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module main
|
||||||
|
|
||||||
|
go 1.25.5
|
||||||
50
go_book/OMDBAPI/main.go
Normal file
50
go_book/OMDBAPI/main.go
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Plug any
|
||||||
|
|
||||||
|
type PlugStruct struct {}
|
||||||
|
|
||||||
|
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)
|
||||||
|
fmt.Println(Search(arg))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,30 +1,59 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"io"
|
||||||
"go.uber.org/zap/buffer"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func addCommas(n int) string {
|
const URL = "https://api.github.com/search/issues"
|
||||||
str := strconv.Itoa(n)
|
|
||||||
|
|
||||||
// Add commas from right to left
|
type IssuerSearchReport struct {
|
||||||
var result buffer.Buffer
|
Total int `json:"total_count"`
|
||||||
for i := range str {
|
Incomplete_results bool `json:"incomplete_results"`
|
||||||
if i > 0 && (len(str)-i)%3 == 0 {
|
Created_at time.Time `json:"created_at"`
|
||||||
fmt.Fprint(&result,",")
|
Items []Issue
|
||||||
}
|
}
|
||||||
result.WriteByte(str[i])
|
|
||||||
|
type Issue struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
User User
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func IssuerSearch(s []string) (IssuerSearchReport,error) {
|
||||||
|
new_s := s[0]
|
||||||
|
resp,err := http.Get(URL + "?q=" + new_s)
|
||||||
|
if err != nil {
|
||||||
|
return IssuerSearchReport{},err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
resp.Body.Close()
|
||||||
|
return IssuerSearchReport{},fmt.Errorf("Error in request",resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.String()
|
var result IssuerSearchReport
|
||||||
|
|
||||||
|
body,_ := io.ReadAll(resp.Body)
|
||||||
|
if err := json.Unmarshal(body,&result); err != nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
return IssuerSearchReport{},nil
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
return result,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
numbers := []int{12345, 1234567, 987654321, 12345, 123, 0}
|
result,err := IssuerSearch(os.Args[1:])
|
||||||
|
if err != nil {
|
||||||
for _, num := range numbers {
|
fmt.Println("Error in IssuerSearch")
|
||||||
fmt.Printf("%d -> %s\n", num, addCommas(num))
|
|
||||||
}
|
}
|
||||||
|
fmt.Printf("%#v\n",result)
|
||||||
|
fmt.Println(result)
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user