SHARE
TWEET

Untitled

a guest Oct 13th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. import pyowm
  3. import argparse
  4. import subprocess
  5. from datetime import datetime
  6. from datetime import timezone
  7. from datetime import timedelta
  8. from pyowm import timeutils
  9.  
  10. def weater_in_future (weather_details,time_now, hours):
  11.     return weather_details.get_weather_at(
  12.         datetime(
  13.                 time_now.year,
  14.                 time_now.month,
  15.                 time_now.day,
  16.                 time_now.hour + hours,
  17.                 time_now.minute,
  18.                 time_now.second))
  19.  
  20. def process (args):
  21.  
  22.     owm = pyowm.OWM(args.api_key[0])
  23.  
  24.     if args.three_hours_forecast:
  25.         weather_details_now = owm.weather_at_place(args.city[0] + ',' + args.ccode[0])
  26.         weather_values_now = weather_details_now.get_weather()
  27.  
  28.         weather_details = owm.three_hours_forecast(args.city[0]+','+args.ccode[0])
  29.         time_now = datetime.now(timezone.utc)
  30.         weather_values_3 = weater_in_future(weather_details, time_now+timedelta(hours=3),0)
  31.         weather_values_6 = weater_in_future(weather_details, time_now+timedelta(hours=6),0)
  32.         weather_values_9 = weater_in_future(weather_details, time_now+timedelta(hours=9),0)
  33.         weather_values_12 = weater_in_future(weather_details, time_now+timedelta(hours=12),0)
  34.  
  35.         if args.get_temp_c:
  36.             print(round(weather_values_now.get_temperature(unit='celsius')['temp']))
  37.             print(round(weather_values_3.get_temperature(unit='celsius')['temp']))
  38.             print(round(weather_values_6.get_temperature(unit='celsius')['temp']))
  39.             print(round(weather_values_9.get_temperature(unit='celsius')['temp']))
  40.             print(round(weather_values_12.get_temperature(unit='celsius')['temp']))
  41.         if args.get_weather_icon:
  42.             print('PNG/'+weather_values_now.get_weather_icon_name()+'.png')
  43.             print('PNG/'+weather_values_3.get_weather_icon_name()+'.png')
  44.             print('PNG/'+weather_values_6.get_weather_icon_name()+'.png')
  45.             print('PNG/'+weather_values_9.get_weather_icon_name()+'.png')
  46.             print('PNG/'+weather_values_12.get_weather_icon_name()+'.png')
  47.  
  48.     else:
  49.         weather_details = owm.weather_at_place(args.city[0] + ',' + args.ccode[0])
  50.         weather_values = weather_details.get_weather()
  51.         if args.get_temp_c:
  52.             print(weather_values.get_temperature(unit='celsius')['temp'])
  53.         if args.get_temp_f:
  54.             print(weather_values.get_temperature(unit='fahrenheit')['temp'])
  55.         if args.get_weather_icon:
  56.             print('PNG/'+weather_values.get_weather_icon_name()+'.png')
  57.  
  58.  
  59. parser = argparse.ArgumentParser(description='Openweather script.')
  60. parser.add_argument('--api_key',help='OWM API key.',nargs=1,metavar=('[api_key]'), required=True)
  61. parser.add_argument('--city',help='Cityname.',nargs=1,metavar=('[city]'), required=True)
  62. parser.add_argument('--ccode',help='Country code.',nargs=1,metavar=('[code]'), required=True)
  63. parser.add_argument('--get_temp_c',help='Get temperature in Celsius.',action='store_true')
  64. parser.add_argument('--get_temp_f',help='Get temperature in Fahrenheit.',action='store_true')
  65. parser.add_argument('--get_weather_icon',help='Get weekday.',action='store_true')
  66. parser.add_argument('--three_hours_forecast',help='Get three hours forecast.', action='store_true')
  67. args = parser.parse_args()
  68.  
  69. process(args)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top