-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Signin - Get the user - Start a websocket connection
- Loading branch information
0 parents
commit 4d12b8f
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dist/ | ||
screeps.egg-info/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
requests==2.8.1 | ||
websocket-client==0.32.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '0.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
) |