From 06af4eaaeabbe5bcdc25bd13e362f473aefdbf4e Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 10 Jan 2021 16:47:39 +0100 Subject: [PATCH] Implement basic database handling --- api.py | 90 +++++++++++++++++++++++++++++++++++++++++++--- apidoc/messages.md | 2 +- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/api.py b/api.py index 69a811c..528cdaf 100644 --- a/api.py +++ b/api.py @@ -1,5 +1,6 @@ import json import datetime +import sqlite3 from request import Request, urlopen @@ -9,9 +10,59 @@ except: USERNAME, PASSWORD = None, None API_URL = "https://www.planetromeo.com/api/v4/" +IMAGE_URL = "https://www.planetromeo.com/img/usr/original/0x0/%s.jpg" + +class Database: + def __init__(self, db="database.db"): + self.conn = sqlite3.connect(db) + self.cur = self.conn.cursor() + + def create_tables(self): + query = """ + CREATE TABLE IF NOT EXISTS `message` (`id` VARCHAR(64), `sender` VARCHAR(64), `recipient` VARCHAR(64), `date` TIMESTAMP, `text` TEXT); + CREATE TABLE IF NOT EXISTS `attachment` (`id` VARCHAR(64) PRIMARY KEY, `owner` VARCHAR(64), `token` VARCHAR(64), `message` VARCHAR(64), `content` BLOB, FOREIGN KEY(`message`) REFERENCES message(`id`)); + CREATE TABLE IF NOT EXISTS `profile` (`id` VARCHAR(64), `name` VARCHAR(256)); + CREATE TABLE IF NOT EXISTS `location` (`profile` VARCHAR(64), `timestamp` TIMESTAMP, `name` VARCHAR(128), `country` VARCHAR(64), FOREIGN KEY(`profile`) REFERENCES profile(`id`)); + """ + + for line in query.strip().split("\n"): + self.cur.execute(line) + + def execute(self, *args, **kwargs): + self.cur.execute(*args, **kwargs) + self.conn.commit() + + def fetchone(self, *args, **kwargs): + return self.cur.fetchone(*args, **kwargs) class Attachment: - pass + def __init__(self, id, owner, token, message, content=None, retrieve=True): + self.id = id + self.owner = owner + self.token = token + self.content = content if content else self.get_content() if retrieve else None + self.message = message + + def get_content(self, exceptions=False): + try: + return urlopen(Request(IMAGE_URL % self.token)).read() + except: + if not exceptions: + return False + raise + + def to_database(self, exceptions=False): + try: + Database().execute("INSERT INTO attachment VALUES (?, ?, ?, ?, ?);", (self.id, self.owner, self.token, self.message, self.content)) + except: + if not exceptions: + return + raise + + @classmethod + def from_dict(cls, attachment, message_id): + if attachment["type"] == "IMAGE": + return cls(attachment["params"]["id"], attachment["params"]["owner_id"], attachment["params"]["url_token"], message_id) class Location: def __init__(self, profile, name, country, timestamp=datetime.datetime.now()): @@ -20,11 +71,27 @@ class Location: self.country = country self.timestamp = timestamp + def to_database(self, exceptions=False): + try: + Database().execute("INSERT INTO location VALUES (?, ?, ?, ?);", (self.profile, self.timestamp, self.name, self.country)) + except: + if not exceptions: + return + raise + class Profile: def __init__(self, id, name): self.id = id self.name = name + def to_database(self, exceptions=False): + try: + Database().execute("INSERT INTO profile VALUES (?, ?);", (self.id, self.name)) + except: + if not exceptions: + return + raise + class Message: def __init__(self, sender, recipient, id, date, text, attachments=[]): self.sender = sender @@ -34,9 +101,22 @@ class Message: self.text = text self.attachments = attachments + def to_database(self, exceptions=False): + try: + Database().execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);", (self.id, self.sender, self.recipient, self.date, self.text)) + for attachment in self.attachments: + attachment.to_database() + except: + if not exceptions: + return + raise + @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"]]) + if not "attachments" in message.keys(): + message["attachments"] = [] + return cls(message["from"], message["to"], message["id"], message["date"], message["text"], [Attachment.from_dict(attachment, message["id"]) for attachment in message["attachments"] if attachment["type"] == "IMAGE"]) + class RomeoAPI: def __init__(self, login=True, username=USERNAME, password=PASSWORD): @@ -60,8 +140,10 @@ class RomeoAPI: return headers - def messages(self, count=4096): + def messages(self, count=16384): headers = self.build_headers() response = urlopen(Request(API_URL + "messages?lang=en&length=%i" % count, headers=headers)) data = json.load(response) - print(data) \ No newline at end of file + + for message in data["items"]: + yield Message.from_dict(message) diff --git a/apidoc/messages.md b/apidoc/messages.md index c09244f..962f051 100644 --- a/apidoc/messages.md +++ b/apidoc/messages.md @@ -22,7 +22,7 @@ Sample response: "error_text":"This App is outdated and cannot support all message features. Please log in to on www.planetromeo.com or update to the most recent App version.", "params":{ "id":"ATTACHMENT_ID", - "owner_id":"ATTACHMENT_SENDER (usually equals SENDER_ID)", + "owner_id":"ATTACHMENT_OWNER (usually equals SENDER_ID)", "url_token":"URL_TOKEN", "auth_token":"AUTH_TOKEN (seems irrelevant)" "width":IMAGE_WIDTH (int),