Skip to content

Commit 6fe0974

Browse files
committed
🥣🖥️ ↝ Most of the data (individual units) can be fetched using manually activated go functions
1 parent 9473d57 commit 6fe0974

File tree

9 files changed

+487
-0
lines changed

9 files changed

+487
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
)
9+
10+
type AstronautAPIResponse struct {
11+
Count int `json:"count"`
12+
Next string `json:"next"`
13+
Prev string `json:"previous"`
14+
Results []Astronaut `json:"results"`
15+
}
16+
17+
type Nationality struct {
18+
ID int `json:"id"`
19+
Name string `json:"name"`
20+
Alpha2Code string `json:"alpha_2_code"`
21+
Alpha3Code string `json:"alpha_3_code"`
22+
NationalityName string `json:"nationality_name"`
23+
NationalityNameComposed string `json:"nationality_name_composed"`
24+
}
25+
26+
type AgencyDetail struct {
27+
ResponseMode string `json:"response_mode"`
28+
ID int `json:"id"`
29+
URL string `json:"url"`
30+
Name string `json:"name"`
31+
Abbrev string `json:"abbrev"`
32+
Type NamedObject `json:"type"`
33+
}
34+
35+
type Astronaut struct {
36+
ID int `json:"id"`
37+
URL string `json:"url"`
38+
Name string `json:"name"`
39+
Status NamedObject `json:"status"`
40+
Agency AgencyDetail `json:"agency"`
41+
Type NamedObject `json:"type"`
42+
InSpace bool `json:"in_space"`
43+
TimeInSpace string `json:"time_in_space"`
44+
EvaTime string `json:"eva_time"`
45+
Age *int `json:"age"`
46+
DateOfBirth *string `json:"date_of_birth"`
47+
DateOfDeath *string `json:"date_of_death"`
48+
Nationality []Nationality `json:"nationality"`
49+
Bio string `json:"bio"`
50+
Wiki *string `json:"wiki"`
51+
LastFlight *string `json:"last_flight"`
52+
FirstFlight *string `json:"first_flight"`
53+
FlightsCount int `json:"flights_count"`
54+
LandingsCount int `json:"landings_count"`
55+
SpacewalksCount int `json:"spacewalks_count"`
56+
}
57+
58+
type NamedObject struct {
59+
ID int `json:"id"`
60+
Name string `json:"name"`
61+
}
62+
63+
func main() {
64+
astronauts, err := GetRecentAstronauts()
65+
if err != nil {
66+
fmt.Println("❌ Error:", err)
67+
return
68+
}
69+
70+
fmt.Println("🧑‍🚀 Most Recently Born Astronauts (Newest)")
71+
for _, a := range astronauts {
72+
fmt.Println("═══════════════════════════════════════════")
73+
fmt.Printf("👤 Name: %s\n", a.Name)
74+
75+
// Handle nationality array
76+
nationalityStr := "Unknown"
77+
if len(a.Nationality) > 0 {
78+
nationalities := make([]string, len(a.Nationality))
79+
for i, nat := range a.Nationality {
80+
nationalities[i] = nat.NationalityName
81+
}
82+
nationalityStr = fmt.Sprintf("%v", nationalities)
83+
if len(a.Nationality) == 1 {
84+
nationalityStr = a.Nationality[0].NationalityName
85+
}
86+
}
87+
fmt.Printf("🗺️ Nationality: %s\n", nationalityStr)
88+
89+
// Handle nullable fields
90+
dob := "Unknown"
91+
if a.DateOfBirth != nil {
92+
dob = *a.DateOfBirth
93+
}
94+
fmt.Printf("🎂 DOB: %s\n", dob)
95+
96+
dod := "N/A"
97+
if a.DateOfDeath != nil && *a.DateOfDeath != "" {
98+
dod = *a.DateOfDeath
99+
}
100+
fmt.Printf("☠️ DOD: %s\n", dod)
101+
102+
age := "Unknown"
103+
if a.Age != nil {
104+
age = fmt.Sprintf("%d", *a.Age)
105+
}
106+
fmt.Printf("🎂 Age: %s\n", age)
107+
108+
fmt.Printf("🧠 Status: %s | Type: %s | In Space Now: %t\n", a.Status.Name, a.Type.Name, a.InSpace)
109+
fmt.Printf("🏢 Agency: %s (%s)\n", a.Agency.Name, a.Agency.Abbrev)
110+
fmt.Printf("🚀 Flights: %d | Landings: %d | Spacewalks: %d\n", a.FlightsCount, a.LandingsCount, a.SpacewalksCount)
111+
fmt.Printf("🕒 Time in Space: %s | EVA Time: %s\n", a.TimeInSpace, a.EvaTime)
112+
113+
firstFlight := "N/A"
114+
if a.FirstFlight != nil && *a.FirstFlight != "" {
115+
firstFlight = *a.FirstFlight
116+
}
117+
lastFlight := "N/A"
118+
if a.LastFlight != nil && *a.LastFlight != "" {
119+
lastFlight = *a.LastFlight
120+
}
121+
fmt.Printf("🛰️ First Flight: %s | Last Flight: %s\n", firstFlight, lastFlight)
122+
123+
if a.Bio != "" {
124+
bioPreview := a.Bio
125+
if len(bioPreview) > 300 {
126+
bioPreview = bioPreview[:300] + "..."
127+
}
128+
fmt.Printf("📜 Bio: %s\n", bioPreview)
129+
}
130+
131+
wiki := "N/A"
132+
if a.Wiki != nil && *a.Wiki != "" {
133+
wiki = *a.Wiki
134+
}
135+
fmt.Printf("🌐 Wiki: %s\n", wiki)
136+
}
137+
}
138+
139+
func GetRecentAstronauts() ([]Astronaut, error) {
140+
url := "https://ll.thespacedevs.com/2.3.0/astronauts/?limit=3&ordering=-date_of_birth&mode=detailed&format=json"
141+
142+
resp, err := http.Get(url)
143+
if err != nil {
144+
return nil, fmt.Errorf("failed to fetch: %w", err)
145+
}
146+
defer resp.Body.Close()
147+
148+
if resp.StatusCode != http.StatusOK {
149+
body, _ := io.ReadAll(resp.Body)
150+
return nil, fmt.Errorf("API returned %d: %s", resp.StatusCode, string(body))
151+
}
152+
153+
var parsed AstronautAPIResponse
154+
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
155+
return nil, fmt.Errorf("error decoding: %w", err)
156+
}
157+
158+
return parsed.Results, nil
159+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
)
9+
10+
type ExpeditionAPIResponse struct {
11+
Count int `json:"count"`
12+
Next string `json:"next"`
13+
Prev string `json:"previous"`
14+
Results []Expedition `json:"results"`
15+
}
16+
17+
// Expedition struct (detailed mode)
18+
type Expedition struct {
19+
ID int `json:"id"`
20+
URL string `json:"url"`
21+
Name string `json:"name"`
22+
Start string `json:"start"`
23+
End string `json:"end"`
24+
SpaceStation SpaceStation `json:"spacestation"`
25+
Crew []CrewMember `json:"crew"`
26+
MissionPatches []Patch `json:"mission_patches"`
27+
}
28+
29+
type NamedObject struct {
30+
ID int `json:"id"`
31+
Name string `json:"name"`
32+
}
33+
34+
type SpaceStation struct {
35+
ID int `json:"id"`
36+
URL string `json:"url"`
37+
Name string `json:"name"`
38+
}
39+
40+
type Role struct {
41+
ID int `json:"id"`
42+
Role string `json:"role"`
43+
Priority int `json:"priority"`
44+
}
45+
46+
type CrewMember struct {
47+
ID int `json:"id"`
48+
Role Role `json:"role"`
49+
Astronaut Astronaut `json:"astronaut"`
50+
}
51+
52+
type Nationality struct {
53+
ID int `json:"id"`
54+
Name string `json:"name"`
55+
Alpha2Code string `json:"alpha_2_code"`
56+
Alpha3Code string `json:"alpha_3_code"`
57+
NationalityName string `json:"nationality_name"`
58+
NationalityNameComposed string `json:"nationality_name_composed"`
59+
}
60+
61+
type Status struct {
62+
ID int `json:"id"`
63+
Name string `json:"name"`
64+
}
65+
66+
type Agency struct {
67+
ResponseMode string `json:"response_mode"`
68+
ID int `json:"id"`
69+
URL string `json:"url"`
70+
Name string `json:"name"`
71+
Abbrev string `json:"abbrev"`
72+
Type NamedObject `json:"type"`
73+
}
74+
75+
type Astronaut struct {
76+
ID int `json:"id"`
77+
URL string `json:"url"`
78+
Name string `json:"name"`
79+
Status Status `json:"status"`
80+
Age int `json:"age"`
81+
InSpace bool `json:"in_space"`
82+
TimeInSpace string `json:"time_in_space"`
83+
EVATime string `json:"eva_time"`
84+
DateOfBirth string `json:"date_of_birth"`
85+
DateOfDeath *string `json:"date_of_death"`
86+
Nationality []Nationality `json:"nationality"`
87+
Agency Agency `json:"agency"`
88+
FirstFlight string `json:"first_flight"`
89+
LastFlight string `json:"last_flight"`
90+
Wiki *string `json:"wiki"`
91+
Bio string `json:"bio"`
92+
}
93+
94+
type Patch struct {
95+
ID int `json:"id"`
96+
Name string `json:"name"`
97+
Priority int `json:"priority"`
98+
ImageURL string `json:"image_url"`
99+
}
100+
101+
func main() {
102+
expeditions, err := GetRecentExpeditions()
103+
if err != nil {
104+
fmt.Println("❌ Error fetching expeditions:", err)
105+
return
106+
}
107+
108+
fmt.Println("🧭 Most Recent Space Expeditions:\n")
109+
110+
for _, e := range expeditions {
111+
fmt.Println("════════════════════════════════════════════════════")
112+
fmt.Printf("🚀 Name: %s\n", e.Name)
113+
fmt.Printf("📅 Start: %s\n", e.Start)
114+
fmt.Printf("📅 End: %s\n", e.End)
115+
fmt.Printf("🏢 Space Station: %s\n", e.SpaceStation.Name)
116+
if len(e.MissionPatches) > 0 {
117+
fmt.Printf("🎖️ Patch: %s (%s)\n", e.MissionPatches[0].Name, e.MissionPatches[0].ImageURL)
118+
}
119+
fmt.Println("👩‍🚀 Crew:")
120+
for _, member := range e.Crew {
121+
astro := member.Astronaut
122+
nationalityStr := "Unknown"
123+
if len(astro.Nationality) > 0 {
124+
nationalityStr = astro.Nationality[0].NationalityName
125+
}
126+
fmt.Printf(" - %s (%s, %s, Age %d)\n", astro.Name, member.Role.Role, nationalityStr, astro.Age)
127+
}
128+
}
129+
}
130+
131+
func GetRecentExpeditions() ([]Expedition, error) {
132+
url := "https://ll.thespacedevs.com/2.3.0/expeditions/?limit=5&ordering=-start&mode=detailed&format=json"
133+
134+
resp, err := http.Get(url)
135+
if err != nil {
136+
return nil, fmt.Errorf("Failed to fetch: %w", err)
137+
}
138+
defer resp.Body.Close()
139+
140+
if resp.StatusCode != http.StatusOK {
141+
body, _ := io.ReadAll(resp.Body)
142+
return nil, fmt.Errorf("API Returned %d: %s", resp.StatusCode, string(body))
143+
}
144+
145+
var parsed ExpeditionAPIResponse
146+
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
147+
return nil, fmt.Errorf("Error decoding: %w", err)
148+
}
149+
150+
return parsed.Results, nil
151+
}

0 commit comments

Comments
 (0)