-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vatsa287 <i.mnshreevatsa@gmail.com>
- Loading branch information
Showing
15 changed files
with
264 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
cli-weather | ||
Get fast updates on weather-data right on the command line. | ||
""" | ||
|
||
__version__ = "0.1.0" | ||
__author__ = 'Shree Vatsa N' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from __future__ import print_function | ||
import requests | ||
import argparse | ||
|
||
def get_by_city_args(subparsers): | ||
city_parser = subparsers.add_parser('city', | ||
formatter_class=argparse.RawTextHelpFormatter | ||
) | ||
city_parser.add_argument( | ||
"city", | ||
help="get weather by city name" | ||
) | ||
city_parser.add_argument( | ||
"-c", "--country", | ||
help="country of entered area", | ||
default="" | ||
) | ||
city_parser.add_argument( | ||
"-u", "--units", | ||
help="M - [DEFAULT] Metric (Celcius, m/s, mm)\nS - Scientific (Kelvin, m/s, mm)\nI - Fahrenheit (F, mph, in)", | ||
default="M" | ||
) | ||
city_parser.add_argument( | ||
"-d","--detailed", | ||
help="display detailed weather data", | ||
action="store_true" | ||
) | ||
|
||
def city_parse(args): | ||
city = args.city | ||
country = "&" + args.country | ||
units = args.units | ||
API_KEY = "2a7db0585e7541018229c17efb2efa94" | ||
|
||
if args.country == "": | ||
API_URL = "https://api.weatherbit.io/v2.0/current?city="+city+"&key=" | ||
else: | ||
API_URL = "https://api.weatherbit.io/v2.0/current?city="+city+country+"&key=" | ||
|
||
url = API_URL + API_KEY | ||
|
||
querystring = { | ||
"lang":"en", | ||
"units":units | ||
} | ||
|
||
response = requests.request("GET", url, params=querystring) | ||
|
||
try: | ||
main_data = response.json() | ||
# ValueError-unable to decode json, UnboundLocalError-used var before declaring | ||
except (ValueError,UnboundLocalError) as err: | ||
print("Invalid city") | ||
print("Please use format ex: $ cli-weather bengaluru [-c country_name][-u S/M/F][-d]") | ||
return | ||
|
||
data = main_data['data'] | ||
weather = data[0]['weather'] | ||
# defalut metric values | ||
degree = "celcius" | ||
speed = "m/s" | ||
distance = "mm" | ||
|
||
if args.units == "S": | ||
degree = "kelvin" | ||
elif args.units == "F": | ||
degree = "Fahrenheit" | ||
speed = "mph" | ||
distance = "in" | ||
|
||
if args.detailed: | ||
print("Have a nice day!\n") | ||
print("city : {}".format(args.city.capitalize())) | ||
print("temparature : {} {}".format(data[0]['temp'],degree)) | ||
print("summary : {}".format(weather['description'])) | ||
print("source station ID : {}".format(data[0]['station'])) | ||
print("latitude : {} degrees".format(data[0]['lat'])) | ||
print("longitude : {} degrees".format(data[0]['lon'])) | ||
print("timezone : {}".format(data[0]['timezone'])) | ||
print("last observation time : {}".format(data[0]['ob_time'])) | ||
print("sunrise : {}".format(data[0]['sunrise'])) | ||
print("sunset : {}".format(data[0]['sunset'])) | ||
print("pressure : {} mb".format(data[0]['pres'])) | ||
print("sea level pressure : {} mb".format(data[0]['slp'])) | ||
print("wind speed : {} {}".format(data[0]['wind_spd'],speed)) | ||
print("wind direction : {} degrees".format(data[0]['wind_dir'])) | ||
print("visibility : {} KM".format(data[0]['vis'])) | ||
print("relative humidity : {} %".format(data[0]['rh'])) | ||
print("snowfall : {} {}/hr".format(data[0]['snow'],distance)) | ||
print("estimated solar radiation: {} W/m^2".format(data[0]['solar_rad'])) | ||
|
||
elif args.detailed is False: | ||
print("Current temparature in {} is {} {}" .format(city.capitalize(), data[0]['temp'], degree)) | ||
|
93 changes: 93 additions & 0 deletions
93
build/lib.linux-x86_64-2.7/cli_weather/get_by_postalcode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from __future__ import print_function | ||
import requests | ||
import argparse | ||
|
||
def get_by_postalcode_args(subparsers): | ||
postalcode_parser = subparsers.add_parser('postalcode', | ||
formatter_class=argparse.RawTextHelpFormatter | ||
) | ||
postalcode_parser.add_argument( | ||
"postalcode", | ||
help="get weather by postal code" | ||
) | ||
postalcode_parser.add_argument( | ||
"-c", "--country", | ||
help="country of entered area", | ||
default="" | ||
) | ||
postalcode_parser.add_argument( | ||
"-u", "--units", | ||
help="M - [DEFAULT] Metric (Celcius, m/s, mm)\nS - Scientific (Kelvin, m/s, mm)\nI - Fahrenheit (F, mph, in)", | ||
default="M" | ||
) | ||
postalcode_parser.add_argument( | ||
"-d","--detailed", | ||
help="display sdetailed weather data", | ||
action="store_true" | ||
) | ||
|
||
def postalcode_parse(args): | ||
postalcode = args.postalcode | ||
country = "&" + args.country | ||
units = args.units | ||
API_KEY = "2a7db0585e7541018229c17efb2efa94" | ||
|
||
if args.country == "": | ||
API_URL = "https://api.weatherbit.io/v2.0/current?postal_code="+postalcode+"&key=" | ||
else: | ||
API_URL = "https://api.weatherbit.io/v2.0/current?postal_code="+postalcode+country+"&key=" | ||
|
||
url = API_URL + API_KEY | ||
|
||
querystring = { | ||
"lang":"en", | ||
"units":units | ||
} | ||
|
||
response = requests.request("GET", url, params=querystring) | ||
|
||
try: | ||
main_data = response.json() | ||
# ValueError-unable to decode json, UnboundLocalError-used var before declaring | ||
except (ValueError,UnboundLocalError) as err: | ||
print("Invalid postal-code") | ||
print("Please use format ex: $ cli-weather postal-code 560032 [-c country_name][-u S/M/F][-d]") | ||
return | ||
|
||
data = main_data['data'] | ||
weather = data[0]['weather'] | ||
# defalut metric values | ||
degree = "celcius" | ||
speed = "m/s" | ||
distance = "mm" | ||
|
||
if args.units == "S": | ||
degree = "kelvin" | ||
elif args.units == "F": | ||
degree = "Fahrenheit" | ||
speed = "mph" | ||
distance = "in" | ||
|
||
if args.detailed: | ||
print("Have a nice day!\n") | ||
print("city : {}".format(data[0]['city_name'].capitalize())) | ||
print("temparature : {} {}".format(data[0]['temp'],degree)) | ||
print("summary : {}".format(weather['description'])) | ||
print("source station ID : {}".format(data[0]['station'])) | ||
print("latitude : {} degrees".format(data[0]['lat'])) | ||
print("longitude : {} degrees".format(data[0]['lon'])) | ||
print("timezone : {}".format(data[0]['timezone'])) | ||
print("last observation time : {}".format(data[0]['ob_time'])) | ||
print("sunrise : {}".format(data[0]['sunrise'])) | ||
print("sunset : {}".format(data[0]['sunset'])) | ||
print("pressure : {} mb".format(data[0]['pres'])) | ||
print("sea level pressure : {} mb".format(data[0]['slp'])) | ||
print("wind speed : {} {}".format(data[0]['wind_spd'],speed)) | ||
print("wind direction : {} degrees".format(data[0]['wind_dir'])) | ||
print("visibility : {} KM".format(data[0]['vis'])) | ||
print("relative humidity : {} %".format(data[0]['rh'])) | ||
print("snowfall : {} {}/hr".format(data[0]['snow'],distance)) | ||
print("estimated solar radiation: {} W/m^2".format(data[0]['solar_rad'])) | ||
elif args.detailed is False: | ||
print("Current temparature in {} is {} {}" .format(data[0]['city_name'].capitalize(), data[0]['temp'], degree)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import print_function | ||
import argparse | ||
|
||
# handle ModuleNotFoundError python3 execution | ||
try: | ||
from cli_weather.get_by_city import get_by_city_args, city_parse | ||
from cli_weather.get_by_postalcode import get_by_postalcode_args, postalcode_parse | ||
except ModuleNotFoundError: | ||
from get_by_city import get_by_city_args, city_parse | ||
from get_by_postalcode import get_by_postalcode_args, postalcode_parse | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser('cli-weather') | ||
|
||
# dest - name of the attribute under which sub-command name will be stored, defalut=None | ||
subparsers = parser.add_subparsers(dest="selected_subparser") | ||
get_by_city_args(subparsers) | ||
get_by_postalcode_args(subparsers) | ||
args = parser.parse_args() | ||
|
||
# args.selected_subparser holds selectd subparser in <str> | ||
if args.selected_subparser == "city": | ||
city_parse(args) | ||
elif args.selected_subparser == "postalcode": | ||
postalcode_parse(args) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from __future__ import print_function | ||
import requests | ||
import argparse | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters