|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type DockingLocationResponse struct { |
| 12 | + Count int `json:"count"` |
| 13 | + Results []DockingLocation `json:"results"` |
| 14 | +} |
| 15 | + |
| 16 | +type DockingLocation struct { |
| 17 | + ID int `json:"id"` |
| 18 | + Name string `json:"name"` |
| 19 | + SpaceStation SpaceStation `json:"spacestation"` |
| 20 | + Spacecraft *Spacecraft `json:"spacecraft"` |
| 21 | + Payload *Payload `json:"payload"` |
| 22 | +} |
| 23 | + |
| 24 | +type SpaceStation struct { |
| 25 | + ID int `json:"id"` |
| 26 | + Name string `json:"name"` |
| 27 | + URL string `json:"url"` |
| 28 | + Image ImageDetail `json:"image"` |
| 29 | +} |
| 30 | + |
| 31 | +type Spacecraft struct { |
| 32 | + ID int `json:"id"` |
| 33 | + Name string `json:"name"` |
| 34 | + URL string `json:"url"` |
| 35 | +} |
| 36 | + |
| 37 | +type Payload struct { |
| 38 | + ID int `json:"id"` |
| 39 | + Name string `json:"name"` |
| 40 | +} |
| 41 | + |
| 42 | +type ImageDetail struct { |
| 43 | + ID int `json:"id"` |
| 44 | + Name string `json:"name"` |
| 45 | + ImageURL string `json:"image_url"` |
| 46 | + ThumbnailURL string `json:"thumbnail_url"` |
| 47 | + Credit string `json:"credit"` |
| 48 | + License License `json:"license"` |
| 49 | + SingleUse bool `json:"single_use"` |
| 50 | +} |
| 51 | + |
| 52 | +type License struct { |
| 53 | + ID int `json:"id"` |
| 54 | + Name string `json:"name"` |
| 55 | + Priority int `json:"priority"` |
| 56 | + Link string `json:"link"` |
| 57 | +} |
| 58 | + |
| 59 | +func main() { |
| 60 | + fmt.Println("π°οΈ Fetching most recent docking location...") |
| 61 | + |
| 62 | + url := "https://ll.thespacedevs.com/2.3.0/config/docking_locations/?limit=1&format=json" |
| 63 | + client := &http.Client{Timeout: 10 * time.Second} |
| 64 | + |
| 65 | + resp, err := client.Get(url) |
| 66 | + if err != nil { |
| 67 | + fmt.Printf("β Request error: %v\n", err) |
| 68 | + return |
| 69 | + } |
| 70 | + defer resp.Body.Close() |
| 71 | + |
| 72 | + if resp.StatusCode != http.StatusOK { |
| 73 | + fmt.Printf("β Unexpected response code: %d\n", resp.StatusCode) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + body, err := io.ReadAll(resp.Body) |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("β Failed to read body: %v\n", err) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + var data DockingLocationResponse |
| 84 | + if err := json.Unmarshal(body, &data); err != nil { |
| 85 | + fmt.Printf("β JSON error: %v\n", err) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + if len(data.Results) == 0 { |
| 90 | + fmt.Println("β οΈ No docking locations found.") |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + loc := data.Results[0] |
| 95 | + fmt.Println("β
Most Recent Docking Location:") |
| 96 | + fmt.Printf("- Name: %s\n", loc.Name) |
| 97 | + fmt.Printf("- Station: %s\n", loc.SpaceStation.Name) |
| 98 | + fmt.Printf(" β’ Image: %s\n", loc.SpaceStation.Image.ImageURL) |
| 99 | + fmt.Printf(" β’ Credit: %s\n", loc.SpaceStation.Image.Credit) |
| 100 | + fmt.Printf(" β’ License: %s (%s)\n", loc.SpaceStation.Image.License.Name, loc.SpaceStation.Image.License.Link) |
| 101 | + |
| 102 | + if loc.Spacecraft != nil { |
| 103 | + fmt.Printf("- Spacecraft: %s\n", loc.Spacecraft.Name) |
| 104 | + } else { |
| 105 | + fmt.Println("- Spacecraft: None") |
| 106 | + } |
| 107 | + |
| 108 | + if loc.Payload != nil { |
| 109 | + fmt.Printf("- Payload: %s\n", loc.Payload.Name) |
| 110 | + } else { |
| 111 | + fmt.Println("- Payload: None") |
| 112 | + } |
| 113 | +} |
0 commit comments