Initial commit

pull/1/head
Raphaël Vinot 2015-03-14 19:27:27 +01:00
commit 8978457a67
7 changed files with 136 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# pylevel2
*.egg-info
build
dist

22
LICENSE Normal file
View File

@ -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.

1
MANIFEST.in Normal file
View File

@ -0,0 +1 @@
include README.md

6
README.md Normal file
View File

@ -0,0 +1,6 @@
Python API for Level2 via requests.
Install
-------
python setup.py install

1
pylevel2/__init__.py Normal file
View File

@ -0,0 +1 @@
from api import PyLevel2

79
pylevel2/api.py Normal file
View File

@ -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

23
setup.py Normal file
View File

@ -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'],
)