Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
 - Signin
 - Get the user
 - Start a websocket connection
  • Loading branch information
TooAngel committed May 26, 2016
0 parents commit 4d12b8f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
screeps.egg-info/
build/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Screeps library

Library to connect to the screeps API either via REST or websocket.
3 changes: 3 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requests==2.8.1
websocket-client==0.32.0

10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements.txt requirements.in
#

requests==2.8.1
six==1.10.0 # via websocket-client
websocket-client==0.32.0
66 changes: 66 additions & 0 deletions screeps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import requests
import websocket


class Connection(object):
def __init__(self, email, password, ptr=False):
self.email = email
self.password = password
self.url = 'https://screeps.com/api'
self.url_wss = 'wss://screeps.com/socket/websocket'
if ptr:
self.url = 'https://screeps.com/ptr/api'
self.url_wss = 'wss://screeps.com/ptr/socket/websocket'

# REST connection
def signin(self):
url = '{}/auth/signin'.format(self.url)
data = dict(email=self.email, password=self.password)
response = requests.post(url=url, data=data)
self.token = response.json()['token']

def get_me(self):
url = '{}/auth/me'.format(self.url)
headers = {'X-Token': self.token, 'X-Username': self.token}
response = requests.get(url=url, headers=headers)
return response.json()

def console(self, command):
url = '{}/auth/console'.format(self.url)
headers = {'X-Token': self.token, 'X-Username': self.token}
data = {'expression': command}
response = requests.post(url=url, headers=headers, data=data)
return response.json()

# Websocket connection
def on_message(self, ws, message):
if (message.startswith('auth ok')):
ws.send('subscribe user:' + self.user_id + '/console')
return

self.messageCallback(message)

def on_error(self, ws, error):
raise Exception(error)

def on_close(self, ws):
raise Exception("### closed ###")

def on_open(self, ws):
ws.send('auth {}'.format(self.token))

def connect(self):
ws = websocket.WebSocketApp(url=self.url_wss,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open)
ws.run_forever(ping_interval=1)

def startWebSocket(self, messageCallback):
self.messageCallback = messageCallback
self.signin()
me = self.get_me()
self.user_id = me['_id']
self.connect()
1 change: 1 addition & 0 deletions screeps/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.1'
43 changes: 43 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

from distutils.core import setup

from setuptools import find_packages

from screeps._version import __version__


try:
import pypandoc
long_description = pypandoc.convert('README.md', 'rst')
except(IOError, ImportError):
long_description = open('README.md').read()

setup(
name='screeps',
version=__version__,
author='Tobias Wilken',
author_email='[email protected]',
maintainer='Tobias Wilken',
maintainer_email='[email protected]',
url='https://github.com/TooAngel/python-screeps',
description='Library connecting to the screeps api.',
long_description=long_description,
download_url='https://pypi.python.org/pypi/screeps',
license='AGPL-3.0',
platforms=['Any'],
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)', # noqa
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python',
'Topic :: Software Development',
'Topic :: Utilities'
],
packages=find_packages(),
install_requires=['requests>=2.8.1', 'websocket-client>=0.32.0'],
)

0 comments on commit 4d12b8f

Please sign in to comment.