romeotools/api.py

67 lines
No EOL
2 KiB
Python

import json
import datetime
from request import Request, urlopen
try:
from settings import USERNAME, PASSWORD
except:
USERNAME, PASSWORD = None, None
API_URL = "https://www.planetromeo.com/api/v4/"
class Attachment:
pass
class Location:
def __init__(self, profile, name, country, timestamp=datetime.datetime.now()):
self.profile = profile
self.name = name
self.country = country
self.timestamp = timestamp
class Profile:
def __init__(self, id, name):
self.id = id
self.name = name
class Message:
def __init__(self, sender, recipient, id, date, text, attachments=[]):
self.sender = sender
self.recipient = recipient
self.id = id
self.date = date
self.text = text
self.attachments = attachments
@classmethod
def from_dict(cls, message):
cls(message["from"], message["to"], message["id"], message["date"], message["text"], [Attachment.from_dict(attachment) for attachment in message["attachments"]])
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(Request(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=4096):
headers = self.build_headers()
response = urlopen(Request(API_URL + "messages?lang=en&length=%i" % count, headers=headers))
data = json.load(response)
print(data)