You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.9 KiB
Go

9 months ago
package service
import (
9 months ago
"encoding/json"
9 months ago
"fmt"
"github.com/astaxie/beego/httplib"
"strconv"
"sync"
"time"
"weather_go/models"
9 months ago
)
type HttpWeatherService struct {
}
9 months ago
func SyncNmcCityAndNmcProvince() {
9 months ago
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)
}
9 months ago
//fmt.Println(provinceListJson)
9 months ago
var provinces []models.NmcProvince
json.Unmarshal([]byte(provinceListJson), &provinces)
//fmt.Println(provinces)
9 months ago
fmt.Println(len(provinces))
9 months ago
for _, item := range provinces {
if models.GetNmcProvince(&item) {
//存在
fmt.Println("存在")
} else {
//不存在
models.NmcProvinceAdd(&item)
fmt.Println("不存在,插入数据库")
9 months ago
time.Sleep(time.Millisecond * 3000)
9 months ago
//http://www.nmc.cn/rest/province/ASX?_=1741055184606
//“http://www.nmc.cn/rest/province/" + code + "?_=" + String.valueOf(System.currentTimeMillis());
9 months ago
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)
}
9 months ago
}
}
9 months ago
}
9 months ago
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() {
9 months ago
citys := models.GetNmcCityList()
fmt.Println(citys)
fmt.Println(len(citys))
9 months ago
if len(citys) == 0 {
9 months ago
SyncNmcCityAndNmcProvince()
}
9 months ago
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")
9 months ago
id, errInsert := models.AddNmcNowWeather(nmcNowWeather)
if errInsert != nil {
9 months ago
println(errInsert.Error())
}
fmt.Println(id)
fmt.Println(str)
}(item)
}
wg.Wait() // 等待所有登记的goroutine都结束
}