|
1 | 1 | import argparse |
| 2 | +import sys |
2 | 3 |
|
3 | 4 | import requests |
4 | 5 |
|
5 | | -WEATHER_EMOJIS = { |
6 | | - "clear": "☀️", |
7 | | - "sunny": "☀️", |
8 | | - "cloudy": "☁️", |
9 | | - "partly cloudy": "⛅", |
10 | | - "rain": "🌧️", |
11 | | - "light rain": "🌦️", |
12 | | - "heavy rain": "🌊", |
13 | | - "storm": "🌩️", |
14 | | - "snow": "❄️", |
15 | | - "fog": "🌫️", |
16 | | - "mist": "🌫️", |
17 | | - "default": "🌍", |
18 | | -} |
19 | 6 |
|
20 | | -API_QUERY_TEMPLATE = "https://wttr.in/{city}?format=%C+%t" |
| 7 | +def get_breeds_info(): |
| 8 | + response = requests.get("https://api.thecatapi.com/v1/breeds") |
| 9 | + response.raise_for_status() |
| 10 | + return response.json() |
21 | 11 |
|
22 | 12 |
|
23 | | -def get_weather_emoji(condition, weather_emojis=WEATHER_EMOJIS): |
24 | | - condition = condition.lower() |
25 | | - for key, emoji in weather_emojis.items(): |
26 | | - if key in condition: |
27 | | - return emoji |
28 | | - return weather_emojis["default"] |
| 13 | +def find_breed_info(breed_name): |
| 14 | + json_response = get_breeds_info() |
| 15 | + for breed in json_response: |
| 16 | + if breed["name"] == breed_name: |
| 17 | + return breed |
| 18 | + return {} |
29 | 19 |
|
30 | 20 |
|
31 | | -def get_weather(city, api_query_template=API_QUERY_TEMPLATE): |
32 | | - api_query = api_query_template.format(city=city) |
33 | | - try: |
34 | | - response = requests.get(api_query) |
35 | | - response.raise_for_status() |
36 | | - weather_info = response.text.strip().split("+") |
37 | | - if len(weather_info) < 2: |
38 | | - return "Error: Unexpected weather data format." |
39 | | - condition = weather_info[0].strip() |
40 | | - temperature = weather_info[1].strip() |
41 | | - emoji = get_weather_emoji(condition) |
42 | | - return f"{emoji} {condition} -> {temperature}" |
43 | | - except requests.RequestException: |
44 | | - return "Error: Could not retrieve weather data." |
| 21 | +def display_breed_profile(breed): |
| 22 | + print(f"\n{breed['name']:-^30s}") |
| 23 | + print(f"Origin: {breed['origin']}") |
| 24 | + print(f"Temperament: {breed['temperament']}") |
| 25 | + print(f"Life Span: {breed['life_span']} years") |
| 26 | + print(f"Weight: {breed['weight']['imperial']} lbs") |
| 27 | + if breed.get("wikipedia_url"): |
| 28 | + print(f"\nLearn more: {breed['wikipedia_url']}") |
45 | 29 |
|
46 | 30 |
|
47 | | -def parse_cli_args(): |
48 | | - parser = argparse.ArgumentParser( |
49 | | - prog="weather", |
50 | | - description="Weather information for the specified city.", |
51 | | - epilog="Thanks for using %(prog)s! :)", |
52 | | - ) |
53 | | - parser.add_argument( |
54 | | - "city", |
55 | | - nargs="+", |
56 | | - help="Name of the city to get weather information for", |
57 | | - ) |
| 31 | +def parse_args(): |
| 32 | + parser = argparse.ArgumentParser(description="Get information about cat breeds") |
58 | 33 | parser.add_argument( |
59 | | - "--version", |
60 | | - action="version", |
61 | | - version="%(prog)s 0.1.0", |
| 34 | + "breed", |
| 35 | + help="Name of cat breed (e.g., 'Siamese')", |
62 | 36 | ) |
63 | 37 | return parser.parse_args() |
64 | 38 |
|
65 | 39 |
|
66 | 40 | def main(): |
67 | | - args = parse_cli_args() |
68 | | - weather = get_weather(" ".join(args.city)) |
69 | | - print(weather) |
| 41 | + args = parse_args() |
| 42 | + try: |
| 43 | + breed = find_breed_info(args.breed) |
| 44 | + if not breed: |
| 45 | + print("Breed not found. Try another breed name.") |
| 46 | + return 0 |
| 47 | + display_breed_profile(breed) |
| 48 | + except Exception as e: |
| 49 | + print(f"Error: {e}") |
| 50 | + return 1 |
| 51 | + |
| 52 | + return 0 |
70 | 53 |
|
71 | 54 |
|
72 | 55 | if __name__ == "__main__": |
73 | | - main() |
| 56 | + sys.exit(main()) |
0 commit comments