github.com/sacloud/libsacloud/v2@v2.32.3/helper/newsfeed/functions.go (about)

     1  // Copyright 2016-2022 The Libsacloud Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package newsfeed
    16  
    17  import (
    18  	"encoding/json"
    19  	"io"
    20  	"net/http"
    21  )
    22  
    23  // NewsFeedURL フィード取得URL
    24  var NewsFeedURL = "https://secure.sakura.ad.jp/rss/sakuranews/getfeeds.php?service=cloud&format=json"
    25  
    26  // Get ニュースフィード(障害/メンテナンス情報)を取得
    27  func Get() ([]*FeedItem, error) {
    28  	resp, err := http.Get(NewsFeedURL)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	defer resp.Body.Close()
    33  
    34  	data, err := io.ReadAll(resp.Body)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var items []*FeedItem
    40  	if err := json.Unmarshal(data, &items); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return items, nil
    45  }
    46  
    47  // GetByURL 指定のURLを持つフィードを取得
    48  func GetByURL(url string) (*FeedItem, error) {
    49  	items, err := Get()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	for _, item := range items {
    54  		if item.URL == url {
    55  			return item, nil
    56  		}
    57  	}
    58  	return nil, nil
    59  }