This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
inara_updater/inara.py

73 lines
1.6 KiB
Python
Raw Normal View History

2015-10-26 00:51:48 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Creates connections to inara.cz to retrieve and update player info.
import requests
import sys
URL_BASE = "http://inara.cz/"
URL_LOGIN = URL_BASE
URL_CMDR = URL_BASE + "cmdr/"
class ServerError(Exception):
pass
class CredentialsError(Exception):
pass
class Session(requests.Session):
def __init__(self):
requests.Session.__init__(self)
def inara_login(self, username, password):
if (not username or not password):
raise CredentialsError()
data = {
"loginid": username,
"loginpass": password,
"formact": "ENT_LOGIN",
"location": "intro"
}
self._inara_handled_request(self.post, URL_LOGIN, data=data)
def inara_update_credits(self, credits):
data = {
"location": "cmdr",
"formact": "USER_CREDITS_SET",
"playercredits": credits,
"playercreditsassets": None,
"oass": 48126920,
}
self._inara_handled_request(self.post, URL_CMDR, data=data)
def inara_update_location(self, location):
data = {
'formact': 'USER_LOCATION_SET',
'playercurloc': location
}
self._inara_handled_request(self.post, URL_CMDR, data=data)
def _inara_handled_request(self, func, url, data=None):
r = func(url, data=data)
r.raise_for_status()
def _inara_dump(self, r):
print "Request:"
print 'Headers\t%s' % r.request.headers
print 'Data\t%s' % r.request.body
print ""
print "Response:"
print 'Status\t%s' % r.status_code
print 'URL\t%s' % r.url
print 'Headers\t%s' % r.headers
print 'Logged in? ', "Yes" if "Logout" in r.text else "No"
print ""