Skip to content

Commit

Permalink
Output beeflight forecast in JSON or table format
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jun 1, 2020
1 parent fb5e952 commit 3fb5942
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -8,6 +8,7 @@ Development
- Convert to Python3
- Add setup.py, README.rst and CHANGES.rst
- Provide ``extract_sites.sh`` through ``apicast beeflight stations``
- Output beeflight forecast in JSON or table format


2018-04-13 0.1.0
Expand Down
43 changes: 30 additions & 13 deletions apicast/cli.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# (c) 2020 Andreas Motl <andreas@hiveeyes.org>
# (c) 2018-2020 Andreas Motl <andreas@hiveeyes.org>
# License: GNU Affero General Public License, Version 3
import json
import logging
import tabulate
from docopt import docopt, DocoptExit
from tabulate import tabulate

from apicast import __appname__, __version__
from apicast.core import grok_beeflight_forecast, dwd_beeflight_forecast_stations, dwd_beeflight_site_url_by_slug
Expand All @@ -19,14 +19,15 @@ def run():
Usage:
apicast beeflight stations [--site-slugs]
apicast beeflight forecast --url=<url>
apicast beeflight forecast --station=<station>
apicast beeflight forecast --url=<url> [--format=<format>]
apicast beeflight forecast --station=<station> [--format=<format>]
apicast --version
apicast (-h | --help)
Options:
--url=<url> URL to detail page
--station=<station> Station identifier
--format=<format> Output format: "json" or "table". Default: json
--version Show version information
--debug Enable debug messages
-h --help Show this screen
Expand Down Expand Up @@ -72,23 +73,39 @@ def run():
# Fetch and extract forecast information.
elif options.url:
result = grok_beeflight_forecast(options.url)
format_beeflight_forecast(result)
format_beeflight_forecast(result, options.format)

elif options.station:
url = dwd_beeflight_site_url_by_slug(options.station)
result = grok_beeflight_forecast(url)
format_beeflight_forecast(result)
format_beeflight_forecast(result, options.format)


def format_beeflight_forecast(result):
def format_beeflight_forecast(result, format='json'):

data = result['data']
if not data:
raise ValueError('No data found or unable to parse')

# Report about weather station / observation location
print()
print(u'### Prognose des Bienenfluges in {}'.format(result['station']))
print()
format = format or 'json'
format = format.lower()

if format not in ['json', 'table']:
raise ValueError('Unknown output format. Please specify "json" or "table".')

if format == 'json':
result = []
for item in data[1:]:
item = dict(zip(data[0], item))
result.append(item)
print(json.dumps(result, indent=4))

else:

# Report about weather station / observation location
print()
print(u'### Prognose des Bienenfluges in {}'.format(result['station']))
print()

# Output forecast data
print(tabulate(data[1:], headers=data[0], showindex=False, tablefmt='pipe'))
# Output forecast data
print(tabulate.tabulate(data[1:], headers=data[0], showindex=False, tablefmt='pipe'))

0 comments on commit 3fb5942

Please sign in to comment.