38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from .http import HTTPRequest
|
|
|
|
|
|
class RomeoAPI:
|
|
def __init__(self, login=True, username=USERNAME, password=PASSWORD):
|
|
if login:
|
|
self.login(username, password)
|
|
|
|
def login(self, username, password):
|
|
payload = {"username": USERNAME,
|
|
"password": PASSWORD,
|
|
"keep_login": True}
|
|
|
|
response = urlopen(HTTPRequest(API_URL + "session?lang=en"),
|
|
json.dumps(payload).encode("utf-8"))
|
|
|
|
data = json.load(response)
|
|
self.session = data
|
|
|
|
def build_headers(self, additional={}):
|
|
headers = {
|
|
"X-Api-Key": "vuEp8o93b34CxUCljSMFEdhI70qDWtuk",
|
|
"X-Session-Id": self.session["session_id"]
|
|
}
|
|
|
|
for key, value in additional:
|
|
headers[key] = value
|
|
|
|
return headers
|
|
|
|
def messages(self, count=16384):
|
|
headers = self.build_headers()
|
|
response = urlopen(
|
|
HTTPRequest(API_URL + "messages?lang=en&length=%i" % count, headers=headers))
|
|
data = json.load(response)
|
|
|
|
for message in data["items"]:
|
|
yield Message.from_dict(message)
|