Пример #1
0
func fetchAqiFromCNOfficial(city string) ([]*AqiData, error) {
	url := fmt.Sprintf(cnOfficialUrl, city)
	bytes, err := getHttpResponseContent(url)
	if err != nil {
		return nil, err
	}
	if Debug {
		fmt.Printf("***Get %s AQI from CN: %s\n", city, strings.TrimSpace(string(bytes)))
	}

	aqiItems := make([]AqiCNOfficialItem, 1)
	if unmarshalErr := json.Unmarshal(bytes, &aqiItems); unmarshalErr != nil {
		return nil, unmarshalErr
	}
	aqiDataArr := make([]*AqiData, 0)
	for _, aqiItem := range aqiItems {
		aqiData := &AqiData{}
		aqiData.City = city
		aqiData.Aqi = aqiItem.Aqi
		//2006-01-02T15:04:05Z
		time, parseErr := utils.ConvertToUnixTime("2006-01-02T15:04:05Z", aqiItem.TimePoint)
		if parseErr != nil {
			return aqiDataArr, parseErr
		}
		aqiData.Time = time
		aqiData.Datasource = CNOfficial
		aqiDataArr = append(aqiDataArr, aqiData)
	}
	sort.Sort(byTime(aqiDataArr))
	return aqiDataArr, nil
}
Пример #2
0
func fetchAqiFromStateair(city string) ([]*AqiData, error) {
	cityCode := usEmbassyStateairMap[city]
	url := fmt.Sprintf(usEmbassyStateairUrl, cityCode)
	bytes, err := getHttpResponseContent(url)
	if err != nil {
		return nil, err
	}
	if Debug {
		fmt.Printf("***Get %s AQI from USEmbassy Stateair: %s\n", city, strings.TrimSpace(string(bytes)))
	}
	feed := rss2_0Feed{}
	if unmarshalErr := xml.Unmarshal(bytes, &feed); unmarshalErr != nil {
		return nil, unmarshalErr
	}
	aqiDataArr := make([]*AqiData, 0)
	rssChannel := feed.Channel
	if rssChannel != nil {
		rssItems := rssChannel.Items
		for _, rssItem := range rssItems {
			aqiData := &AqiData{}
			aqiData.City = city
			aqiData.Aqi = rssItem.AQI
			time, parseErr := utils.ConvertToUnixTime("01/02/2006 3:04:05 PM", rssItem.Date)
			if parseErr != nil {
				return aqiDataArr, parseErr
			}
			aqiData.Time = time
			aqiData.Datasource = USEmbassy
			aqiDataArr = append(aqiDataArr, aqiData)
		}
	}
	sort.Sort(byTime(aqiDataArr))
	latestAqiData := aqiDataArr[0]
	if latestAqiData.Aqi < 0 {
		errMsg := fmt.Sprintf("Invalid aqi data from Stateair: %+v", latestAqiData)
		return aqiDataArr, errors.New(errMsg)
	}

	if time.Now().Unix()-latestAqiData.Time > 60*60*3 {
		errMsg := fmt.Sprintf("Can not get recent 3 hours aqi data from Stateair: %+v", latestAqiData)
		return aqiDataArr, errors.New(errMsg)
	}

	return aqiDataArr, nil
}
Пример #3
0
func convertPageTimeToTime(content string) (int64, error) {
	// &nbsp;&nbsp;采价时间:2013-06-19上午9:00之前
	prefix := "采价时间:"
	suffix := "之前"
	start := strings.Index(content, prefix) + len(prefix)
	if start < 0 {
		return -1, errors.New("Not found prefix")
	}
	end := strings.Index(content, suffix)
	if end < 0 {
		return -1, errors.New("Not found suffix")
	}
	dateTime := strings.Split(content[start:end], "上午")
	timeStr := fmt.Sprintf("%s %sAM", dateTime[0], dateTime[1])
	timeStr = strings.Replace(timeStr, ":", ":", -1)

	return utils.ConvertToUnixTime("2006-01-02 3:04PM", timeStr)
}