-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (58 loc) · 2.3 KB
/
example.py
File metadata and controls
72 lines (58 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""
Example usage of PyASAN - NASA API Python wrapper.
This script demonstrates how to use the PyASAN library to interact with NASA's
Astronomy Picture of the Day (APOD) API.
"""
from pyasan import APODClient
from pyasan.exceptions import PyASANError
def main():
"""Demonstrate PyASAN functionality."""
print("🚀 PyASAN - NASA API Python Wrapper Example\n")
# Initialize the client (uses DEMO_KEY by default)
# For production use, get your free API key at https://api.nasa.gov/
client = APODClient()
try:
# Get today's APOD
print("📅 Getting today's Astronomy Picture of the Day...")
apod = client.get_apod(hd=True)
print(f"Title: {apod.title}")
print(f"Date: {apod.date}")
print(f"Media Type: {apod.media_type}")
print(f"URL: {apod.url}")
if apod.hdurl:
print(f"HD URL: {apod.hdurl}")
if apod.copyright:
print(f"Copyright: {apod.copyright}")
print(f"Explanation: {apod.explanation[:100]}...")
print()
# Get a random APOD
print("🎲 Getting a random APOD...")
random_apod = client.get_random_apod()
print(f"Random APOD: {random_apod.title} ({random_apod.date})")
print()
# Get multiple random APODs
print("🎲 Getting 3 random APODs...")
random_apods = client.get_random_apod(count=3)
for i, apod in enumerate(random_apods, 1):
print(f"{i}. {apod.title} ({apod.date})")
print()
# Get recent APODs
print("📆 Getting recent APODs (last 5 days)...")
recent = client.get_recent_apods(days=5)
print(f"Found {len(recent)} recent APODs:")
for apod in recent:
print(f" - {apod.date}: {apod.title}")
print()
# Get APOD for a specific date
print("📅 Getting APOD for a specific date (2023-01-01)...")
specific = client.get_apod(date="2023-01-01")
print(f"New Year 2023 APOD: {specific.title}")
print()
print("✅ All examples completed successfully!")
except PyASANError as e:
print(f"❌ Error: {e}")
except Exception as e:
print(f"❌ Unexpected error: {e}")
if __name__ == "__main__":
main()