Implement basic database handling
This commit is contained in:
parent
168c84971d
commit
06af4eaaea
2 changed files with 87 additions and 5 deletions
90
api.py
90
api.py
|
@ -1,5 +1,6 @@
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
from request import Request, urlopen
|
from request import Request, urlopen
|
||||||
|
|
||||||
|
@ -9,9 +10,59 @@ except:
|
||||||
USERNAME, PASSWORD = None, None
|
USERNAME, PASSWORD = None, None
|
||||||
|
|
||||||
API_URL = "https://www.planetromeo.com/api/v4/"
|
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:
|
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:
|
class Location:
|
||||||
def __init__(self, profile, name, country, timestamp=datetime.datetime.now()):
|
def __init__(self, profile, name, country, timestamp=datetime.datetime.now()):
|
||||||
|
@ -20,11 +71,27 @@ class Location:
|
||||||
self.country = country
|
self.country = country
|
||||||
self.timestamp = timestamp
|
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:
|
class Profile:
|
||||||
def __init__(self, id, name):
|
def __init__(self, id, name):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
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:
|
class Message:
|
||||||
def __init__(self, sender, recipient, id, date, text, attachments=[]):
|
def __init__(self, sender, recipient, id, date, text, attachments=[]):
|
||||||
self.sender = sender
|
self.sender = sender
|
||||||
|
@ -34,9 +101,22 @@ class Message:
|
||||||
self.text = text
|
self.text = text
|
||||||
self.attachments = attachments
|
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
|
@classmethod
|
||||||
def from_dict(cls, message):
|
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:
|
class RomeoAPI:
|
||||||
def __init__(self, login=True, username=USERNAME, password=PASSWORD):
|
def __init__(self, login=True, username=USERNAME, password=PASSWORD):
|
||||||
|
@ -60,8 +140,10 @@ class RomeoAPI:
|
||||||
|
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
def messages(self, count=4096):
|
def messages(self, count=16384):
|
||||||
headers = self.build_headers()
|
headers = self.build_headers()
|
||||||
response = urlopen(Request(API_URL + "messages?lang=en&length=%i" % count, headers=headers))
|
response = urlopen(Request(API_URL + "messages?lang=en&length=%i" % count, headers=headers))
|
||||||
data = json.load(response)
|
data = json.load(response)
|
||||||
print(data)
|
|
||||||
|
for message in data["items"]:
|
||||||
|
yield Message.from_dict(message)
|
||||||
|
|
|
@ -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.",
|
"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":{
|
"params":{
|
||||||
"id":"ATTACHMENT_ID",
|
"id":"ATTACHMENT_ID",
|
||||||
"owner_id":"ATTACHMENT_SENDER (usually equals SENDER_ID)",
|
"owner_id":"ATTACHMENT_OWNER (usually equals SENDER_ID)",
|
||||||
"url_token":"URL_TOKEN",
|
"url_token":"URL_TOKEN",
|
||||||
"auth_token":"AUTH_TOKEN (seems irrelevant)"
|
"auth_token":"AUTH_TOKEN (seems irrelevant)"
|
||||||
"width":IMAGE_WIDTH (int),
|
"width":IMAGE_WIDTH (int),
|
||||||
|
|
Loading…
Reference in a new issue