diff --git a/go_book/OMDBAPI/go.mod b/go_book/OMDBAPI/go.mod new file mode 100644 index 0000000..371eb8f --- /dev/null +++ b/go_book/OMDBAPI/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.25.5 diff --git a/go_book/OMDBAPI/main.go b/go_book/OMDBAPI/main.go new file mode 100644 index 0000000..7e72c66 --- /dev/null +++ b/go_book/OMDBAPI/main.go @@ -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)) +} + diff --git a/go_book/proj1/main.go b/go_book/proj1/main.go index 7f9ea4c..431faef 100644 --- a/go_book/proj1/main.go +++ b/go_book/proj1/main.go @@ -1,30 +1,59 @@ package main import ( + "encoding/json" "fmt" - "strconv" - "go.uber.org/zap/buffer" + "io" + "net/http" + "os" + "time" ) -func addCommas(n int) string { - str := strconv.Itoa(n) - - // Add commas from right to left - var result buffer.Buffer - for i := range str { - if i > 0 && (len(str)-i)%3 == 0 { - fmt.Fprint(&result,",") - } - result.WriteByte(str[i]) +const URL = "https://api.github.com/search/issues" + +type IssuerSearchReport struct { + Total int `json:"total_count"` + Incomplete_results bool `json:"incomplete_results"` + Created_at time.Time `json:"created_at"` + Items []Issue +} + +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 } - - return result.String() + if resp.StatusCode != http.StatusOK { + resp.Body.Close() + return IssuerSearchReport{},fmt.Errorf("Error in request",resp.Status) + } + + 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() { - numbers := []int{12345, 1234567, 987654321, 12345, 123, 0} - - for _, num := range numbers { - fmt.Printf("%d -> %s\n", num, addCommas(num)) + result,err := IssuerSearch(os.Args[1:]) + if err != nil { + fmt.Println("Error in IssuerSearch") } + fmt.Printf("%#v\n",result) + fmt.Println(result) } \ No newline at end of file