-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherifier.py
84 lines (68 loc) · 3.32 KB
/
weatherifier.py
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
73
74
75
76
77
78
79
80
81
82
83
84
import click
import geocoder
import pyperclip
import matplotlib.pyplot as plt
from art import tprint
from weather import get_weather, print_weather_data
from forecast import get_forecast, generate_weather_report, visualize_weather_report, plot_forecast
from audio import text_to_speech
from sentiment import analyze_sentiment
from translation import translate_text
@click.command()
@click.option('--city', '-c', help='Display weather for a specific city.')
@click.option('--forecast', '-f', is_flag=True, help='Display 5-day weather forecast and plot the graph.')
@click.option('--geolocation', '-g', is_flag=True, help='Get weather based on geolocation.')
@click.option('--share', '-s', is_flag=True, help='Share the weather report.')
@click.option('--tts', '-t', is_flag=True, help='Convert weather report to audio.')
@click.option('--comment', '-cm', help='Analyze sentiment of a weather-related comment.')
@click.option('--translate', '-tr', help='Translate weather description to a specific language.')
def weather(city, forecast, geolocation, share, tts, comment, translate):
tprint("Welcome to Weatherifier!!", font="small")
print("How to use: run the command python weatherifier.py -c <city name> /-f /-s /-t /-cm <comment> /-g")
print("For help, run the command python weatherifier.py --help")
click.echo()
if geolocation:
g = geocoder.ip('me')
city = g.city
if city:
try:
weather_data = get_weather(city)
click.echo(f"Weather forecast for {city}:")
click.echo()
print_weather_data(weather_data)
except Exception as err:
print("City was not found in database . pls try another nearby city")
if translate:
description = get_weather(city)
try:
translated_text = translate_text(description, translate,city)
except Exception as err:
click.echo(err);
if forecast:
forecast_data = get_forecast(city)
click.echo(f"Weather forecast for the next 5 days in {city}:")
click.echo()
for data in forecast_data['list']:
click.echo(data['dt_txt'])
weather_report = generate_weather_report(data)
visualize_weather_report(weather_report)
click.echo()
plot_forecast(forecast_data)
if share:
weather_report = generate_weather_report(weather_data)
pyperclip.copy(weather_report)
click.echo("Weather report copied to clipboard. You can now paste it anywhere.")
if tts:
weather_data = get_weather(city)
weather_report = 'The weather forecast for ' + city + ' is ' + weather_data['weather'][0]['description'] + ' with a temperature of ' + str(weather_data['main']['temp']) + ' degree Celsius, a humidity of ' + str(weather_data['main']['humidity']) + ' percent, and a wind speed of ' + str(weather_data['wind']['speed']) + ' meters per second. Thank you.'
# Generate a unique audio file name
audio_file = f'{city}_weather_report.wav'
try:
text_to_speech(weather_report, audio_file)
click.echo('Weather report audio generated successfully.')
except Exception as e:
click.echo(f'Error generating audio: {str(e)}')
if comment:
analyze_sentiment(comment)
if __name__ == '__main__':
weather()