From 8978457a6736bfb1b82b31d525760af1388f9b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sat, 14 Mar 2015 19:27:27 +0100 Subject: [PATCH] Initial commit --- .gitignore | 4 +++ LICENSE | 22 ++++++++++++ MANIFEST.in | 1 + README.md | 6 ++++ pylevel2/__init__.py | 1 + pylevel2/api.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ setup.py | 23 +++++++++++++ 7 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 MANIFEST.in create mode 100644 README.md create mode 100644 pylevel2/__init__.py create mode 100644 pylevel2/api.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4faf50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# pylevel2 +*.egg-info +build +dist diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5f89e57 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Raphaël Vinot + +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. + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..bb3ec5f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2587195 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +Python API for Level2 via requests. + +Install +------- + +python setup.py install diff --git a/pylevel2/__init__.py b/pylevel2/__init__.py new file mode 100644 index 0000000..7e72e71 --- /dev/null +++ b/pylevel2/__init__.py @@ -0,0 +1 @@ +from api import PyLevel2 diff --git a/pylevel2/api.py b/pylevel2/api.py new file mode 100644 index 0000000..25d6397 --- /dev/null +++ b/pylevel2/api.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import requests +import urlparse +from datetime import datetime +import os + + +class PyLevel2(object): + + def __init__(self, base_url='https://level2.lu'): + """ + Query the Level 2 API. + """ + self.base_url = base_url + self.session = requests.Session() + self.session.headers.update({'content-type': 'application/json'}) + + def events(self, count=None, year=None, month=None): + """ + Get the events. + """ + path = 'events' + response = None + if count is None and year is None and month is None: + path = os.path.join(path, 'json') + url = urlparse.urljoin(self.base_url, path) + response = self.session.get(url) + elif count is not None: + path = os.path.join(path, '{}.json'.format(count)) + url = urlparse.urljoin(self.base_url, path) + response = self.session.get(url) + elif year is not None and month is not None: + path = os.path.join(path, str(year), '{}.json'.format(month)) + url = urlparse.urljoin(self.base_url, path) + response = self.session.get(url) + else: + # Invalid requests + pass + if response.status_code == 200: + to_return = [] + data = response.json() + if data: + for event in data: + if event.get('start'): + event['start'] = datetime.fromtimestamp(event['start']) + if event.get('end'): + event['end'] = datetime.fromtimestamp(event['end']) + if event.get('date'): + # This value does not contains the year, so it is not really usable + # Get the day from the start time instead + event['date'] = event['start'].date() + to_return.append(event) + else: + # invalid query + pass + return to_return + else: + # Something bad happened + pass + + def spaceapi(self): + """ + Gives information about Level2. + """ + url = urlparse.urljoin(self.base_url, 'spaceapi') + response = self.session.get(url) + if response.status_code == 200: + data = response.json() + if data: + data['state']['lastchange'] = datetime.fromtimestamp(data['state']['lastchange']) + else: + # invalid query + pass + return data + else: + # Something bad happened + pass diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..bb5916e --- /dev/null +++ b/setup.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from setuptools import setup + +setup( + name='pylevel2', + version='1.0', + author='Raphaël Vinot', + author_email='raphael.vinot@gmail.com', + maintainer='Raphaël Vinot', + url='https://github.com/Kaweechelchen/Level2.lu', + description='Python API for Level2.', + long_description=open('README.md').read(), + packages=['pylevel2'], + classifiers=[ + 'License :: OSI Approved :: MIT License', + 'Development Status :: 4 - Beta', + 'Environment :: Console', + 'Programming Language :: Python', + 'Topic :: Internet', + ], + install_requires=['requests'], +)