WeatherConsoleApp.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. )
  9. var city string
  10. var apiKey string = "&limit=5&appid=f7047650c6b759c44bb67724cbb2ccad"
  11. var GeoResponse string = "http://api.openweathermap.org/geo/1.0/direct?q="
  12. var jsonStr string
  13. var CurrentWeather = "https://api.open-meteo.com/v1/forecast?latitude="
  14. var LonWeather = "&longitude="
  15. var endResp = "&timezone=auto&daily=temperature_2m_max"
  16. var Lat float64
  17. var Lon float64
  18. var jsonWeaherStr string
  19. var latStr string
  20. var lonStr string
  21. type Geo struct {
  22. Name string `json:"name"`
  23. Lat float64 `json:"lat"`
  24. Lon float64 `json:"lon"`
  25. Country string `json:"country"`
  26. State string `json:"state,omitempty"`
  27. }
  28. type WeatherJson struct {
  29. Latitude float64 `json:"latitude"`
  30. Longitude float64 `json:"longitude"`
  31. GenerationtimeMs float64 `json:"generationtime_ms"`
  32. UtcOffsetSeconds int `json:"utc_offset_seconds"`
  33. Timezone string `json:"timezone"`
  34. TimezoneAbbreviation string `json:"timezone_abbreviation"`
  35. Elevation float64 `json:"elevation"`
  36. DailyUnits struct {
  37. Time string `json:"time"`
  38. Temperature2MMax string `json:"temperature_2m_max"`
  39. } `json:"daily_units"`
  40. Daily struct {
  41. Time []string `json:"time"`
  42. Temperature2MMax []float64 `json:"temperature_2m_max"`
  43. } `json:"daily"`
  44. }
  45. func main() {
  46. fmt.Println("Введите название города на английском, чтобы узнать погоду:")
  47. for {
  48. fmt.Scan(&city)
  49. MakeRequest(city)
  50. data := []byte(jsonStr)
  51. u := make([]Geo, 0)
  52. err := json.Unmarshal(data, &u)
  53. if err != nil {
  54. fmt.Println("We can not get json Geo")
  55. fmt.Println(err.Error())
  56. }
  57. for _, info := range u {
  58. if strings.Compare(city, info.Name) == 0 {
  59. fmt.Println("Город:", city)
  60. fmt.Println("Широта:", info.Lat, "\nДолгота:", info.Lon)
  61. Lat = info.Lat
  62. Lon = info.Lon
  63. latStr = fmt.Sprintf("%f", Lat)
  64. lonStr = fmt.Sprintf("%f", Lon)
  65. WeatherRequest()
  66. break
  67. }
  68. }
  69. }
  70. }
  71. func MakeRequest(city string) {
  72. resp, err := http.Get(GeoResponse + city + apiKey)
  73. if err != nil {
  74. fmt.Println("We can not get json Geo")
  75. fmt.Println(err.Error())
  76. }
  77. body, err := ioutil.ReadAll(resp.Body)
  78. if err != nil {
  79. fmt.Println("We can not get json Geo")
  80. fmt.Println(err.Error())
  81. }
  82. jsonStr = string(body)
  83. }
  84. func WeatherRequest() {
  85. var ResGet = CurrentWeather + latStr + LonWeather + lonStr + endResp
  86. resp, err := http.Get(ResGet)
  87. if err != nil {
  88. fmt.Println("We can not get Current Weather")
  89. fmt.Println(err.Error())
  90. return
  91. }
  92. body, err := ioutil.ReadAll(resp.Body)
  93. if err != nil {
  94. fmt.Println("We can not read Current Weather")
  95. fmt.Println(err.Error())
  96. }
  97. jsonWeaherStr = string(body)
  98. dataWeather := []byte(jsonWeaherStr)
  99. var u2 WeatherJson
  100. err2 := json.Unmarshal(dataWeather, &u2)
  101. if err2 != nil {
  102. fmt.Println("We can not get json Weather")
  103. fmt.Println(err2.Error())
  104. }
  105. fmt.Println("Сегодня до", u2.Daily.Temperature2MMax[0], "градусов по Цельсию.")
  106. fmt.Println("Завтра до", u2.Daily.Temperature2MMax[1], "градусов по Цельсию.")
  107. fmt.Println("Послезавтра до", u2.Daily.Temperature2MMax[2], "градусов по Цельсию.")
  108. defer resp.Body.Close()
  109. }