2020-02-05 22:02:17 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
2020-02-05 23:03:34 +01:00
|
|
|
# Reiko Kaps 2015-2020 <r31k@k4p5.de>
|
2020-02-05 22:02:17 +01:00
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
import argparse
|
2020-02-05 22:02:17 +01:00
|
|
|
import json
|
|
|
|
import subprocess
|
2020-02-14 22:43:48 +01:00
|
|
|
import sys
|
2020-02-05 22:02:17 +01:00
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
try:
|
2020-02-05 22:02:17 +01:00
|
|
|
import requests
|
|
|
|
from requests.exceptions import ConnectionError
|
2020-02-14 22:43:48 +01:00
|
|
|
except ImportError:
|
2020-02-05 22:02:17 +01:00
|
|
|
print('please install requests python module')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
DIR_URL = 'https://directory.spaceapi.io/'
|
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
|
|
|
|
def download(url):
|
2020-02-05 23:03:34 +01:00
|
|
|
"""
|
|
|
|
Download json endpoint
|
2020-02-14 22:43:48 +01:00
|
|
|
Parameter url: URL
|
2020-02-05 22:02:17 +01:00
|
|
|
returns string (json)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
file = requests.get(url)
|
|
|
|
except ConnectionError as e:
|
|
|
|
raise e
|
2020-02-14 22:43:48 +01:00
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
return file.json()
|
|
|
|
|
|
|
|
|
2020-02-05 23:03:34 +01:00
|
|
|
def list_spaces():
|
|
|
|
"""
|
2020-02-14 22:43:48 +01:00
|
|
|
list all directory entries
|
2020-02-05 23:03:34 +01:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
directory = download(DIR_URL)
|
|
|
|
except ConnectionError:
|
|
|
|
raise
|
2020-02-14 22:43:48 +01:00
|
|
|
# print all entries
|
|
|
|
for name in directory:
|
2020-02-05 23:03:34 +01:00
|
|
|
print('{}'.format(name))
|
2020-02-05 22:02:17 +01:00
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
|
2020-02-14 23:20:04 +01:00
|
|
|
def details(data, verbose):
|
|
|
|
"""Show all details"""
|
|
|
|
print(json.dumps(data, indent=4, sort_keys=True))
|
2020-02-05 22:02:17 +01:00
|
|
|
|
2020-02-14 23:51:54 +01:00
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
def status(json_data, verbose):
|
|
|
|
"""Ermittelt aus dem JSON den Status des Hackerspace"""
|
2020-02-05 22:02:17 +01:00
|
|
|
if verbose is True:
|
|
|
|
pass
|
2020-02-14 22:43:48 +01:00
|
|
|
|
|
|
|
if json_data['state']['open'] is False:
|
|
|
|
print(' {} is closed'.format(str(json_data['space'])))
|
2020-02-05 22:02:17 +01:00
|
|
|
return False
|
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
print(' {} is open'.format(str(json_data['space'])))
|
2020-02-05 22:02:17 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
def getHomepage(json_data, verbose):
|
2020-02-05 22:02:17 +01:00
|
|
|
"""Ermittelt aus dem Json die Homepage des Hackerspace"""
|
2020-02-14 22:43:48 +01:00
|
|
|
if json_data['url']:
|
|
|
|
print('opening {}'.format(json_data['url']))
|
|
|
|
subprocess.run(['firefox', json_data['url']])
|
2020-02-05 22:02:17 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def getspaceurl(name, debug=False):
|
|
|
|
try:
|
|
|
|
directory = download(DIR_URL)
|
|
|
|
except ConnectionError:
|
|
|
|
raise
|
2020-02-14 22:43:48 +01:00
|
|
|
if name in directory:
|
2020-02-05 22:02:17 +01:00
|
|
|
return directory[name]
|
|
|
|
else:
|
|
|
|
if debug:
|
|
|
|
print('Space {} was not found!'.format(name))
|
|
|
|
for key in directory:
|
|
|
|
if name.lower() in key.lower():
|
|
|
|
if debug:
|
|
|
|
print("Trying simulare spacename '{}' ...".format(key))
|
|
|
|
return directory[key]
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-02-14 22:43:48 +01:00
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
def main(args):
|
2020-02-14 23:51:54 +01:00
|
|
|
|
2020-02-05 23:03:34 +01:00
|
|
|
if args.verbose:
|
2020-02-14 23:20:04 +01:00
|
|
|
debug = True
|
2020-02-05 22:02:17 +01:00
|
|
|
else:
|
|
|
|
debug = False
|
2020-02-05 23:03:34 +01:00
|
|
|
|
|
|
|
# get the full hackspace list and exit
|
|
|
|
if args.list:
|
|
|
|
list_spaces()
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
try:
|
2020-02-14 22:43:48 +01:00
|
|
|
data = download(getspaceurl(args.name, debug))
|
|
|
|
except ConnectionError:
|
2020-02-05 22:02:17 +01:00
|
|
|
print('not connected')
|
|
|
|
sys.exit(1)
|
2020-02-14 22:43:48 +01:00
|
|
|
|
2020-02-14 23:20:04 +01:00
|
|
|
if args.details:
|
2020-02-14 23:51:54 +01:00
|
|
|
details(data, debug)
|
2020-02-14 23:20:04 +01:00
|
|
|
sys.exit(0)
|
2020-02-14 23:51:54 +01:00
|
|
|
|
2020-02-05 23:03:34 +01:00
|
|
|
if args.web:
|
2020-02-14 22:43:48 +01:00
|
|
|
getHomepage(data, debug)
|
2020-02-05 22:02:17 +01:00
|
|
|
sys.exit(0)
|
2020-02-14 22:43:48 +01:00
|
|
|
|
|
|
|
if status(data, verbose=False):
|
2020-02-05 22:02:17 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description='Show Space Status')
|
|
|
|
parser.add_argument('-n', '--name', help='Name of Hackerspace', default='LeineLab')
|
2020-02-14 23:44:14 +01:00
|
|
|
parser.add_argument('-d', '--details', help='Shows more details of Hackerspace')
|
2020-02-05 23:03:34 +01:00
|
|
|
parser.add_argument('-l', '--list', action='store_true', help='List all Hackspaces on Spaceapi')
|
2020-02-14 23:20:04 +01:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true', help='verbose output')
|
2020-02-05 23:03:34 +01:00
|
|
|
parser.add_argument('-w', '--web', action='store_true', help='get homepage url')
|
2020-02-05 22:02:17 +01:00
|
|
|
args = parser.parse_args()
|
2020-02-14 22:43:48 +01:00
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
main(args)
|