From b0a009f8829e42b468a99fd4b78d3a6ea935e3d9 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 22 Jan 2022 08:12:24 +0100 Subject: [PATCH] Some refactoring to prepare for actual usage --- .gitignore | 4 +- api.py | 138 -------------------------------------------- classes/__init__.py | 0 classes/api.py | 38 ++++++++++++ classes/database.py | 60 +++++++++++++++++++ classes/http.py | 11 ++++ classes/location.py | 20 +++++++ classes/message.py | 28 +++++++++ classes/profile.py | 15 +++++ const.py | 5 ++ request.py | 8 --- 11 files changed, 180 insertions(+), 147 deletions(-) create mode 100644 classes/__init__.py create mode 100644 classes/api.py create mode 100644 classes/database.py create mode 100644 classes/http.py create mode 100644 classes/location.py create mode 100644 classes/message.py create mode 100644 classes/profile.py create mode 100644 const.py delete mode 100644 request.py diff --git a/.gitignore b/.gitignore index 1b663f9..5c6b6f2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ logs/ *.pyc __pycache__/ database.db -settings.py \ No newline at end of file +settings.py +venv/ +.vscode/ \ No newline at end of file diff --git a/api.py b/api.py index b83f60e..b9a7217 100644 --- a/api.py +++ b/api.py @@ -9,141 +9,3 @@ try: 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) PRIMARY KEY, `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) PRIMARY KEY, `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: - 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()): - self.profile = profile - self.name = name - 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 - self.recipient = recipient - self.id = id - self.date = date - 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): - 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): - 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=16384): - headers = self.build_headers() - response = urlopen(Request(API_URL + "messages?lang=en&length=%i" % count, headers=headers)) - data = json.load(response) - - for message in data["items"]: - yield Message.from_dict(message) diff --git a/classes/__init__.py b/classes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/classes/api.py b/classes/api.py new file mode 100644 index 0000000..476282e --- /dev/null +++ b/classes/api.py @@ -0,0 +1,38 @@ +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) diff --git a/classes/database.py b/classes/database.py new file mode 100644 index 0000000..a092a6c --- /dev/null +++ b/classes/database.py @@ -0,0 +1,60 @@ +import sqlite3 + +from urllib3 import urlopen + +from .http import HTTPRequest + + +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) PRIMARY KEY, `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) PRIMARY KEY, `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: + 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) diff --git a/classes/http.py b/classes/http.py new file mode 100644 index 0000000..2245b47 --- /dev/null +++ b/classes/http.py @@ -0,0 +1,11 @@ +from urllib3 import Request, urlopen + +from const import USER_AGENT, X_API_KEY + + +class HTTPRequest(Request): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.headers["User-Agent"] = USER_AGENT + self.headers["X-Api-Key"] = X_API_KEY + self.headers["Content-Type"] = "application/json" \ No newline at end of file diff --git a/classes/location.py b/classes/location.py new file mode 100644 index 0000000..630a316 --- /dev/null +++ b/classes/location.py @@ -0,0 +1,20 @@ +from .database import Database + +import datetime + + +class Location: + def __init__(self, profile, name, country, timestamp=datetime.datetime.now()): + self.profile = profile + self.name = name + 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 diff --git a/classes/message.py b/classes/message.py new file mode 100644 index 0000000..34e8b71 --- /dev/null +++ b/classes/message.py @@ -0,0 +1,28 @@ +from .database import Database + + +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 + + 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): + 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"]) diff --git a/classes/profile.py b/classes/profile.py new file mode 100644 index 0000000..8844eca --- /dev/null +++ b/classes/profile.py @@ -0,0 +1,15 @@ +from .database import Database + + +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 diff --git a/const.py b/const.py new file mode 100644 index 0000000..73ad407 --- /dev/null +++ b/const.py @@ -0,0 +1,5 @@ +API_URL = "https://www.planetromeo.com/api/v4/" +IMAGE_URL = "https://www.planetromeo.com/img/usr/original/0x0/%s.jpg" + +USER_AGENT = "RomeoTools (https://kumig.it/kumitterer/romeotools.git)" +X_API_KEY = "vuEp8o93b34CxUCljSMFEdhI70qDWtuk" \ No newline at end of file diff --git a/request.py b/request.py deleted file mode 100644 index 4a189fc..0000000 --- a/request.py +++ /dev/null @@ -1,8 +0,0 @@ -from urllib.request import Request as UrllibRequest, urlopen - -class Request(UrllibRequest): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.headers["user-agent"] = "romeotools (https://kumig.it/kumitterer/romeotools)" - self.headers["X-Api-Key"] = "vuEp8o93b34CxUCljSMFEdhI70qDWtuk" - self.headers["Content-Type"] = "application/json" \ No newline at end of file