From d75a3b465013350de748dc3bab8c5133cc238a60 Mon Sep 17 00:00:00 2001 From: Thomas Passer Jensen Date: Thu, 10 Jan 2019 23:49:13 +0100 Subject: [PATCH] First version --- LICENSE | 21 ++++++++++++ README.md | 11 +++++++ rejseplanen/__init__.py | 1 + rejseplanen/methods.py | 72 +++++++++++++++++++++++++++++++++++++++++ setup.cfg | 2 ++ setup.py | 29 +++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 rejseplanen/__init__.py create mode 100644 rejseplanen/methods.py create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..db43bc3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Thomas Passer Jensen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..014ea1c --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +## Installation + +```pip install rejseplanen``` + +## Module usage + +``` +import rejseplanen + +print(rejseplanen.departureBoard(2158)) +``` \ No newline at end of file diff --git a/rejseplanen/__init__.py b/rejseplanen/__init__.py new file mode 100644 index 0000000..e923ba0 --- /dev/null +++ b/rejseplanen/__init__.py @@ -0,0 +1 @@ +from rejseplanen.methods import departureBoard \ No newline at end of file diff --git a/rejseplanen/methods.py b/rejseplanen/methods.py new file mode 100644 index 0000000..deec4f2 --- /dev/null +++ b/rejseplanen/methods.py @@ -0,0 +1,72 @@ +""" +Python implementation of communication with Rejseplanen API. + +More information about the API can be found at +https://help.rejseplanen.dk/hc/en-us/articles/214174465-Rejseplanen-s-API + +""" + +from datetime import datetime +import requests + +_RESOURCE = 'http://xmlopen.rejseplanen.dk/bin/rest.exe/' + +def departureBoard(stop_id, useTrain=None, useBus=None, useMetro=None, dateTime=None, offset=None, timeout=10): + params = {} + + if type(stop_id) is int: + params['id'] = stop_id + else: + raise TypeError("Expected , got {}.".format(type(stop_id))) + + + params['format'] = 'json' + + # API defaults to use all modes of transportation + if useTrain is not None: + if isinstance(useTrain, bool): + params['useTog'] = int(useTrain) + else: + raise TypeError("Expected , got {}".format(type(useTrain))) + if useBus is not None: + if isinstance(useBus, bool): + params['useBus'] = int(useBus) + else: + raise TypeError("Expected , got {}".format(type(useBus))) + if useMetro is not None: + if isinstance(useMetro, bool): + params['useMetro'] = int(useMetro) + else: + raise TypeError("Expected , got {}".format(type(useMetro))) + + if dateTime and offset: + raise ValueError('Cannot specify both time and offset') + + if dateTime: + if isinstance(dateTime, datetime): + params['date'] = dateTime.strftime("%d.%m.%y") + params['time'] = dateTime.strftime("%H:%M") + else: + raise TypeError('Expected datetime.datime, got {}'.format(type(dateTime))) + + if offset: + if isinstance(offset, int): + params['offset'] = offset + else: + raise TypeError("Expected , got {}".format(type(offset))) + + response = requests.get(_RESOURCE+'departureBoard', params, timeout=timeout) + + if response.status_code != 200: + return response + + result = response.json()['DepartureBoard'] + + # This key is present on error + if 'error' in result: + return result + + return result['Departure'] + +if __name__ == "__main__": + pass \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..224a779 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a21acc0 --- /dev/null +++ b/setup.py @@ -0,0 +1,29 @@ +from setuptools import setup +setup( + name = 'rejseplanen', # How you named your package folder (MyLib) + packages = ['rejseplanen'], # Chose the same as "name" + version = '0.1', # Start with a small number and increase it with every change you make + license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository + description = 'Interface with Rejseplanen API', # Give a short description about your library + author = 'Thomas Passer Jensen', # Type in your name + author_email = 'tomatpasser@gmail.com', # Type in your E-Mail + url = 'https://github.com/tomatpasser/python-rejseplanen', # Provide either the link to your github or to your website + download_url = 'https://github.com/tomatpasser/python-rejseplanen/archive/v_01.tar.gz', # I explain this later on + keywords = ['transport', 'rejseplanen', 'timetable', 'journey', 'public transport'], # Keywords that define your package best + install_requires=[ # I get to this in a second + 'requests>=2.9.1', + ], + classifiers=[ + 'Development Status :: 4 - Beta', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package + + 'Intended Audience :: Developers', # Define that your audience are developers + 'Topic :: Software Development :: Build Tools', + + 'License :: OSI Approved :: MIT License', # Again, pick a license + + 'Programming Language :: Python :: 3', #Specify which pyhton versions that you want to support + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + ], +) \ No newline at end of file