From e8da850fde659cc9983cb6cfc39c4917ecadb953 Mon Sep 17 00:00:00 2001 From: "Reik Kaps (lulu)" Date: Wed, 5 Feb 2020 22:02:17 +0100 Subject: [PATCH] Dateien dazu --- setup.py | 35 +++++++++++++++ src/SpaceApiCli.py | 109 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 setup.py create mode 100644 src/SpaceApiCli.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d1e4907 --- /dev/null +++ b/setup.py @@ -0,0 +1,35 @@ +from setuptools import setup, find_packages +setup( + name="SpaceApiCli", + version="0.1rc1", + packages=find_packages(), + scripts=["SpaceApiCli.py"], + + # Project uses reStructuredText, so ensure that the docutils get + # installed or upgraded on the target machine + install_requires=["requests>=2.22.0"], + + package_data={ + # If any package contains *.txt or *.rst files, include them: + "": ["*.txt", "*.rst", "*.md"], + # And include any *.msg files found in the "hello" package, too: + # "hello": ["*.msg"], + }, + + # metadata to display on PyPI + author="Reiko Kaps", + author_email="r31k@k4p5.de", + description="A simple Commandline Tool to access the hackspace api", + keywords="spaceapi hackspace cli tool", + url="https://github.com/reikkaps/spaceapicli", # project home page, if any + project_urls={ + "Bug Tracker": "https://github.com/reikkaps/spaceapicli/issues", + "Documentation": "https://github.com/reikkaps/spaceapicli/blob/master/README.md", + "Source Code": "https://github.com/reikkaps/spaceapicli", + }, + classifiers=[ + "License :: OSI Approved :: Python Software Foundation License" + ] + + # could also include long_description, download_url, etc. +) diff --git a/src/SpaceApiCli.py b/src/SpaceApiCli.py new file mode 100644 index 0000000..0e9f717 --- /dev/null +++ b/src/SpaceApiCli.py @@ -0,0 +1,109 @@ +#!/usr/bin/python3 +# +# Reiko Kaps 2015 + +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): + """Download a file to local filesystem + Parameter url: URL + returns string (json) + """ + try: + file = requests.get(url) + except ConnectionError as e: + raise e + + return file.json() + + +def save(json): + status(json) + file = open("./spaceapi.json", 'w') + file.write(str(json)) + file.close() + +def status(json, verbose): + """Ermittelt aus dem Json den Status des Hackerspace""" + if verbose is True: + pass + + if json['state']['open'] is False: + print('🧘 closed'.format(str(json['space']))) + return False + + print('🌞 open'.format(str(json['space']))) + 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): + if args.v: + debug = args.v + else: + debug = False + + try: + json = download(getspaceurl(args.name, debug)) + except ConnectionError as e: + print('not connected') + sys.exit(1) + + if args.w: + 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') + parser.add_argument('-v', action='store_true', help='Show more Infos of Hackerspace') + parser.add_argument('-w', action='store_true', help='get homepage url') + args = parser.parse_args() + + main(args) + + +