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
|
|
|
|
|
|
|
import json
|
|
|
|
import sys, os, argparse
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
except ImportError as e:
|
|
|
|
print('please install requests python module')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
DIR_URL='https://directory.spaceapi.io/'
|
|
|
|
|
|
|
|
def download(url):
|
2020-02-05 23:03:34 +01:00
|
|
|
"""
|
|
|
|
Download json endpoint
|
2020-02-05 22:02:17 +01:00
|
|
|
Parameter url: URL
|
|
|
|
returns string (json)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
file = requests.get(url)
|
|
|
|
except ConnectionError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
return file.json()
|
|
|
|
|
|
|
|
|
2020-02-05 23:03:34 +01:00
|
|
|
def list_spaces():
|
|
|
|
"""
|
|
|
|
list all directory entries
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
directory = download(DIR_URL)
|
|
|
|
except ConnectionError:
|
|
|
|
raise
|
|
|
|
# print all entries
|
|
|
|
for name in directory:
|
|
|
|
print('{}'.format(name))
|
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 23:03:34 +01:00
|
|
|
|
2020-02-05 22:02:17 +01:00
|
|
|
|
|
|
|
def status(json, verbose):
|
|
|
|
"""Ermittelt aus dem Json den Status des Hackerspace"""
|
|
|
|
if verbose is True:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if json['state']['open'] is False:
|
2020-02-09 18:43:13 +01:00
|
|
|
print(' {} is closed'.format(str(json['space'])))
|
2020-02-05 22:02:17 +01:00
|
|
|
return False
|
|
|
|
|
2020-02-09 18:43:13 +01:00
|
|
|
print(' {} is open'.format(str(json['space'])))
|
2020-02-05 22:02:17 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def getHomepage(json, verbose):
|
|
|
|
"""Ermittelt aus dem Json die Homepage des Hackerspace"""
|
|
|
|
if json['url']:
|
|
|
|
print('opening {}'.format(json['url']))
|
|
|
|
subprocess.run(['firefox', json['url']])
|
|
|
|
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def getspaceurl(name, debug=False):
|
|
|
|
try:
|
|
|
|
directory = download(DIR_URL)
|
|
|
|
except ConnectionError:
|
|
|
|
raise
|
|
|
|
if name in directory:
|
|
|
|
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)
|
|
|
|
|
|
|
|
def main(args):
|
2020-02-14 23:20:04 +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:
|
|
|
|
json = download(getspaceurl(args.name, debug))
|
|
|
|
except ConnectionError as e:
|
|
|
|
print('not connected')
|
|
|
|
sys.exit(1)
|
2020-02-14 23:20:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
if args.details:
|
|
|
|
details(json, debug)
|
|
|
|
sys.exit(0)
|
2020-02-05 23:03:34 +01:00
|
|
|
|
|
|
|
if args.web:
|
2020-02-05 22:02:17 +01:00
|
|
|
getHomepage(json, debug)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if status(json, verbose=False):
|
|
|
|
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:20:04 +01:00
|
|
|
parser.add_argument('-d', '--details', help='Shows more details of Hackerspace', default='LeineLab')
|
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()
|
|
|
|
|
|
|
|
main(args)
|
|
|
|
|
|
|
|
|
|
|
|
|