forked from erewok/noaa-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocean_data.py
executable file
·63 lines (53 loc) · 2.39 KB
/
ocean_data.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
#!/usr/bin/python3 -tt
from ocean_modules.noaa_parser import NoaaParser
from ocean_modules.send_text import _tsend as send_text
from configparser import ConfigParser, ExtendedInterpolation
import os
def read_config():
parser = ConfigParser(interpolation=ExtendedInterpolation())
# script_dir = os.path.dirname(os.path.realpath(__file__))
# script_dir = os.path.dirname(script_dir, 'config', 'config')
# Ucomment the above, comment below when not in testing.
file = os.path.join(os.getcwd(), 'config', 'config')
parser.read(file)
return parser
def ocean_data_all(source, my_loc):
WeatherInfo = NoaaParser()
all_locations = WeatherInfo.parse_results(source)
my_sources = WeatherInfo.get_locations(my_loc, all_locations)
return WeatherInfo.weather_get(my_sources)
def ocean_data_clean(source, my_loc, time_zone):
WeatherInfo = NoaaParser()
all_locations = WeatherInfo.parse_results(source)
my_sources = WeatherInfo.get_locations(my_loc, all_locations)
return WeatherInfo.weather_info_dict(my_sources, time_zone)
def make_message(input_data):
'''Returns a string from input_data after testing whether input is list
or dict. Dict will actually be a nested dict (see noaa_parser.py)'''
messagestring = ''
if isinstance(input_data, list):
messagestring = '\n'.join(input_data)
elif isinstance(input_data, dict):
for key, val in input_data.items():
messagestring += key + '\n'
if isinstance(val, dict):
for newkey, newval in val.items():
messagestring += newkey + ' ' + newval + '\n'
else:
messagestring += val + '\n'
return messagestring
if __name__=='__main__':
configs = read_config()
username = configs['email']['username']
password = configs['email']['password']
recipient = configs['cell settings']['recipient']
region = configs['noaa.gov settings']['region']
location = configs['noaa.gov settings']['location']
time_zone = configs['location settings']['set_time_zone']
all_dat = configs['data requested'].getboolean('all')
if all_dat:
message = ocean_data_all(region, location) # returns a list
else:
message = ocean_data_clean(region, location, time_zone) # returns a dict
weather_msg = location + '\n' + make_message(message)
send_text(username, password, recipient, weather_msg)