Some refactoring to prepare for actual usage

This commit is contained in:
Kumi 2022-01-22 08:12:24 +01:00
parent e8b9e17d06
commit b0a009f882
11 changed files with 180 additions and 147 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@ logs/
__pycache__/
database.db
settings.py
venv/
.vscode/

138
api.py
View file

@ -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)

0
classes/__init__.py Normal file
View file

38
classes/api.py Normal file
View file

@ -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)

60
classes/database.py Normal file
View file

@ -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)

11
classes/http.py Normal file
View file

@ -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"

20
classes/location.py Normal file
View file

@ -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

28
classes/message.py Normal file
View file

@ -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"])

15
classes/profile.py Normal file
View file

@ -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

5
const.py Normal file
View file

@ -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"

View file

@ -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"