package service import ( "encoding/json" "fmt" "github.com/astaxie/beego/httplib" "strconv" "sync" "time" "weather_go/models" ) type HttpWeatherService struct { } func SyncNmcCityAndNmcProvince() { timeUnix := time.Now().Unix() s := strconv.FormatInt(timeUnix, 10) url := "http://www.nmc.cn/rest/province/all?_=" + s + "000" //http://www.nmc.cn/rest/province/all?_=1741052160653 //http://www.nmc.cn/rest/province/all?_=1741052160653 fmt.Println(url) req := httplib.Get(url) provinceListJson, err := req.String() if err != nil { fmt.Println(err) } //fmt.Println(provinceListJson) var provinces []models.NmcProvince json.Unmarshal([]byte(provinceListJson), &provinces) //fmt.Println(provinces) fmt.Println(len(provinces)) for _, item := range provinces { if models.GetNmcProvince(&item) { //存在 fmt.Println("存在") } else { //不存在 models.NmcProvinceAdd(&item) fmt.Println("不存在,插入数据库") time.Sleep(time.Millisecond * 3000) //http://www.nmc.cn/rest/province/ASX?_=1741055184606 //“http://www.nmc.cn/rest/province/" + code + "?_=" + String.valueOf(System.currentTimeMillis()); fmt.Println(item) timeUnix := time.Now().Unix() s := strconv.FormatInt(timeUnix, 10) url := "http://www.nmc.cn/rest/province/" + item.Code + "?_=" + s + "000" fmt.Println(url) req := httplib.Get(url) //req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) cityListJson, err := req.String() if err != nil { fmt.Println(err) } var citys []models.NmcCity json.Unmarshal([]byte(cityListJson), &citys) fmt.Println(len(citys)) now := time.Now() //获取当前时间 for _, itemCity := range citys { fmt.Println(itemCity) itemCity.CreateDate = now itemCity.LastUpdateDate = now models.NmcCityAdd(&itemCity) } } } } func (s *HttpWeatherService) GetNmcNowWeatherSyn() { citys := models.GetNmcCityList() fmt.Println(citys) fmt.Println(len(citys)) if len(citys) == 0 { SyncNmcCityAndNmcProvince() } for _, item := range citys { time.Sleep(time.Millisecond * 300) fmt.Println(*item) timeUnix := time.Now().Unix() s := strconv.FormatInt(timeUnix, 10) url := "http://www.nmc.cn/rest/weather?stationid=" + item.Code + "&_=" + s + "000" fmt.Println(url) req := httplib.Get(url) //req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) str, err := req.String() if err != nil { fmt.Println(err) } var nmcNowWeather = new(models.NmcNowWeather) now := time.Now() //获取当前时间 nmcNowWeather.Weather = str nmcNowWeather.Code = item.Code nmcNowWeather.WeatherDate = now.Format("2006-01-02") id, errInsert := models.AddNmcNowWeather(nmcNowWeather) if errInsert != nil { println(errInsert.Error()) } fmt.Println(id) fmt.Println(str) } } func (s *HttpWeatherService) GetNmcNowWeatherSync() { citys := models.GetNmcCityList() fmt.Println(citys) fmt.Println(len(citys)) if len(citys) == 0 { SyncNmcCityAndNmcProvince() } var wg sync.WaitGroup for _, item := range citys { time.Sleep(time.Millisecond * 300) wg.Add(1) // 启动一个goroutine就登记+1 go func(city *models.NmcCity) { defer wg.Done() // goroutine结束就登记-1 fmt.Println(*city) timeUnix := time.Now().Unix() s := strconv.FormatInt(timeUnix, 10) url := "http://www.nmc.cn/rest/weather?stationid=" + item.Code + "&_=" + s + "000" fmt.Println(url) req := httplib.Get(url) //req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) str, err := req.String() if err != nil { fmt.Println(err) } var nmcNowWeather = new(models.NmcNowWeather) now := time.Now() //获取当前时间 nmcNowWeather.Weather = str nmcNowWeather.Code = item.Code nmcNowWeather.WeatherDate = now.Format("2006-01-02") id, errInsert := models.AddNmcNowWeather(nmcNowWeather) if errInsert != nil { println(errInsert.Error()) } fmt.Println(id) fmt.Println(str) }(item) } wg.Wait() // 等待所有登记的goroutine都结束 }