main.go - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       main.go (3269B)
       ---
            1 // Parse Enexis disruptions API output.
            2 package main
            3 
            4 import (
            5         "encoding/json"
            6 //        "flag"
            7         "fmt"
            8         "net/http"
            9 //        "os"
           10         "strings"
           11         "time"
           12 )
           13 
           14 type Item struct {
           15         Location        string
           16         StartAndEndDate string
           17         IsResolved      bool
           18         AmountAffected  string
           19         FailedComponent string
           20         Cause           string
           21         Product         string
           22         Address         struct {
           23                 Street     string
           24                 City       string
           25                 PostalCode string
           26                 Province   struct {
           27                         ProvinceName string
           28                 }
           29         }
           30 }
           31 
           32 type Root struct {
           33         Items []Item
           34 }
           35 
           36 var loc *time.Location
           37 
           38 func process(url string) {
           39         var root Root
           40         client := &http.Client{
           41                 Timeout: 30 * time.Second,
           42         }
           43         req, err := http.NewRequest("GET", url, nil)
           44         if err != nil {
           45                 panic(err)
           46         }
           47         // hide User-Agent.
           48         req.Header.Add("User-Agent", "")
           49 
           50         resp, err := client.Do(req)
           51         if resp != nil {
           52                 defer resp.Body.Close() // err != nil && resp != nil can happen on redirect failure.
           53         }
           54         if err != nil {
           55                 panic(err)
           56         }
           57         if resp.StatusCode != 200 {
           58                 panic(fmt.Errorf("statuscode: %d", resp.StatusCode))
           59         }
           60 
           61         // decode
           62         dec := json.NewDecoder(resp.Body)
           63         err = dec.Decode(&root)
           64         if err != nil {
           65                 panic(err)
           66         }
           67 
           68         for _, item := range root.Items {
           69                 var startdate, enddate time.Time
           70                 i := strings.Index(item.StartAndEndDate, "B ")
           71                 if i != -1 && len(item.StartAndEndDate[i+2:]) >= len("2006-01-02 15:04:05") {
           72                         tm, err := time.ParseInLocation("2006-01-02 15:04:05",
           73                                 item.StartAndEndDate[i+2:i+2+len("2006-01-02 15:04:05")], loc)
           74                         if err == nil {
           75                                 startdate = tm
           76                         }
           77                 }
           78                 i = strings.Index(item.StartAndEndDate, "E ")
           79                 if i != -1 && len(item.StartAndEndDate[i+2:]) >= len("2006-01-02 15:04:05") {
           80                         tm, err := time.ParseInLocation("2006-01-02 15:04:05",
           81                                 item.StartAndEndDate[i+2:i+2+len("2006-01-02 15:04:05")], loc)
           82                         if err == nil {
           83                                 enddate = tm
           84                         }
           85                 }
           86 
           87                 fmt.Printf("%s\n", item.Location)
           88                 if item.IsResolved {
           89                         fmt.Printf("\tResolved:         Yes\n")
           90                 } else {
           91                         fmt.Printf("\tResolved:         No\n")
           92                 }
           93                 if !startdate.IsZero() {
           94                         fmt.Printf("\tStartdate:        %s\n", startdate)
           95                 }
           96                 if !enddate.IsZero() {
           97                         fmt.Printf("\tEnddate:          %s\n", enddate)
           98                 }
           99                 fmt.Printf("\tDisruption type:  %s (%s)\n", item.Cause,
          100                         item.Product)
          101                 fmt.Printf("\tFailed component: %s\n", item.FailedComponent)
          102                 fmt.Printf("\tAmount affected:  %s\n", item.AmountAffected)
          103                 fmt.Printf("\tStreet:           %s\n", item.Address.Street)
          104                 fmt.Printf("\tLocation:         %s %s %s\n",
          105                         item.Address.PostalCode,
          106                         item.Address.City,
          107                         item.Address.Province.ProvinceName)
          108         }
          109 }
          110 
          111 func main() {
          112 //        var config_open, config_closed bool
          113         var err error
          114 
          115 //        flag.BoolVar(&config_open, "open", true, "Open incidents")
          116 //        flag.BoolVar(&config_closed, "closed", false, "Closed incidents")
          117 //        flag.Parse()
          118 
          119 //        if !config_open && !config_closed {
          120 //                flag.PrintDefaults()
          121 //                os.Exit(1)
          122 //        }
          123 
          124         loc, err = time.LoadLocation("Europe/Amsterdam")
          125         if err != nil {
          126                 panic(err)
          127         }
          128 
          129         // old "API" url. As of ~2019-10-11 it seems to not support "open" status anymore.
          130 //        url := "https://www.enexis.nl/consument/_layouts/InternetServiceHandler.ashx?s_act=StoringenMX&status="
          131 //        if config_open {
          132 //                process(url + "open")
          133 //        }
          134 //        if config_closed {
          135 //                process(url + "closed")
          136 //        }
          137         url := "https://www.enexis.nl/api/outages/closed"
          138         process(url)
          139 }