The code and data behind xeiaso.net
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/fly: add flyght tracker API client

Signed-off-by: Xe Iaso <me@xeiaso.net>

Xe Iaso 4fe62fff 5d37f3b5

+89
+89
internal/fly/flyghttracker/flyghttracker.go
··· 1 + package flyghttracker 2 + 3 + import ( 4 + "encoding/json" 5 + "flag" 6 + "fmt" 7 + "net/http" 8 + "strings" 9 + "time" 10 + 11 + "within.website/x/web" 12 + ) 13 + 14 + var ( 15 + flyghttrackerURL = flag.String("flyghttracker-url", "https://flyght-tracker.fly.dev/api/upcoming_events", "Flyghttracker URL") 16 + ) 17 + 18 + // Date represents a date in the format "YYYY-MM-DD" 19 + type Date struct { 20 + time.Time 21 + } 22 + 23 + // UnmarshalJSON parses a JSON string in the format "YYYY-MM-DD" to a Date 24 + func (d *Date) UnmarshalJSON(b []byte) error { 25 + s := strings.Trim(string(b), "\"") 26 + t, err := time.Parse("2006-01-02", s) 27 + if err != nil { 28 + return err 29 + } 30 + d.Time = t 31 + return nil 32 + } 33 + 34 + // MarshalJSON returns a JSON string in the format "YYYY-MM-DD" 35 + func (d Date) MarshalJSON() ([]byte, error) { 36 + return json.Marshal(d.Time.Format("2006-01-02")) 37 + } 38 + 39 + // Event represents an event that members of DevRel will be attending. 40 + type Event struct { 41 + Name string `json:"name"` 42 + URL string `json:"url"` 43 + StartDate Date `json:"start_date"` 44 + EndDate Date `json:"end_date"` 45 + Location string `json:"location"` 46 + People []string `json:"people"` 47 + } 48 + 49 + // Fetch new events from the Flyght Tracker URL. 50 + // 51 + // It returns a list of events that end in the future and that have "Xe" as one of the attendees. 52 + func Fetch() ([]Event, error) { 53 + resp, err := http.Get(*flyghttrackerURL) 54 + if err != nil { 55 + return nil, fmt.Errorf("failed to fetch flyghttracker events: %w", err) 56 + } 57 + defer resp.Body.Close() 58 + 59 + if resp.StatusCode != http.StatusOK { 60 + return nil, web.NewError(http.StatusOK, resp) 61 + } 62 + 63 + var events []Event 64 + if err := json.NewDecoder(resp.Body).Decode(&events); err != nil { 65 + return nil, fmt.Errorf("failed to decode flyghttracker events: %w", err) 66 + } 67 + 68 + var result []Event 69 + 70 + for _, event := range events { 71 + if event.EndDate.Before(time.Now()) { 72 + continue 73 + } 74 + 75 + found := false 76 + for _, person := range event.People { 77 + if person == "Xe" { 78 + found = true 79 + break 80 + } 81 + } 82 + 83 + if found { 84 + result = append(result, event) 85 + } 86 + } 87 + 88 + return result, nil 89 + }