diff --git a/api.py b/api.py deleted file mode 100644 index b9a7217..0000000 --- a/api.py +++ /dev/null @@ -1,11 +0,0 @@ -import json -import datetime -import sqlite3 - -from request import Request, urlopen - -try: - from settings import USERNAME, PASSWORD -except: - USERNAME, PASSWORD = None, None - diff --git a/classes/api.py b/classes/api.py index 476282e..b79eb6c 100644 --- a/classes/api.py +++ b/classes/api.py @@ -1,26 +1,39 @@ -from .http import HTTPRequest +from .http import HTTPRequest, urlopen +from .message import Message +from urllib.error import HTTPError + +from const import API_URL, X_API_KEY + +import json class RomeoAPI: - def __init__(self, login=True, username=USERNAME, password=PASSWORD): + def __init__(self, login=True, username=None, password=None): + self.session = None + if login: self.login(username, password) + def login(self, username, password): - payload = {"username": USERNAME, - "password": PASSWORD, + payload = {"username": username, + "password": password, "keep_login": True} - response = urlopen(HTTPRequest(API_URL + "session?lang=en"), + try: + response = urlopen(HTTPRequest(API_URL + "session?lang=en", headers=self.build_headers()), json.dumps(payload).encode("utf-8")) + except HTTPError as e: + print(e.read()) + raise data = json.load(response) self.session = data def build_headers(self, additional={}): headers = { - "X-Api-Key": "vuEp8o93b34CxUCljSMFEdhI70qDWtuk", - "X-Session-Id": self.session["session_id"] + "X-Api-Key": X_API_KEY, + "X-Session-Id": self.session["session_id"] if self.session else "" } for key, value in additional: @@ -35,4 +48,5 @@ class RomeoAPI: data = json.load(response) for message in data["items"]: - yield Message.from_dict(message) + if messageobj := Message.from_dict(message): + yield messageobj diff --git a/classes/attachment.py b/classes/attachment.py new file mode 100644 index 0000000..26af802 --- /dev/null +++ b/classes/attachment.py @@ -0,0 +1,39 @@ +from urllib.request import urlopen + +from .http import HTTPRequest + + +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(HTTPRequest(IMAGE_URL % self.token)).read() + except: + if not exceptions: + return False + raise + + def to_database(self, db=None, exceptions=False): + try: + db = db or Database() + db.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": + try: + return cls(attachment["params"]["id"], attachment["params"]["owner_id"], attachment["params"]["url_token"], message_id) + except Exception as e: + print(f"Error getting attachment: {e}") + return False \ No newline at end of file diff --git a/classes/database.py b/classes/database.py index a092a6c..8cfeb20 100644 --- a/classes/database.py +++ b/classes/database.py @@ -1,6 +1,6 @@ import sqlite3 -from urllib3 import urlopen +from urllib.request import urlopen from .http import HTTPRequest @@ -27,34 +27,3 @@ class Database: 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 index 2245b47..9e9f72a 100644 --- a/classes/http.py +++ b/classes/http.py @@ -1,4 +1,4 @@ -from urllib3 import Request, urlopen +from urllib.request import Request, urlopen from const import USER_AGENT, X_API_KEY diff --git a/classes/location.py b/classes/location.py index 630a316..bad9697 100644 --- a/classes/location.py +++ b/classes/location.py @@ -10,9 +10,10 @@ class Location: self.country = country self.timestamp = timestamp - def to_database(self, exceptions=False): + def to_database(self, db=None, exceptions=False): try: - Database().execute("INSERT INTO location VALUES (?, ?, ?, ?);", + db = db or Database() + db.execute("INSERT INTO location VALUES (?, ?, ?, ?);", (self.profile, self.timestamp, self.name, self.country)) except: if not exceptions: diff --git a/classes/message.py b/classes/message.py index 34e8b71..421e15e 100644 --- a/classes/message.py +++ b/classes/message.py @@ -1,4 +1,5 @@ from .database import Database +from .attachment import Attachment class Message: @@ -10,12 +11,13 @@ class Message: self.text = text self.attachments = attachments - def to_database(self, exceptions=False): + def to_database(self, db=None, exceptions=False): try: - Database().execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);", + db = db or Database() + db.execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);", (self.id, self.sender, self.recipient, self.date, self.text)) for attachment in self.attachments: - attachment.to_database() + attachment.to_database(db) except: if not exceptions: return diff --git a/const.py b/const.py index 73ad407..914abb2 100644 --- a/const.py +++ b/const.py @@ -1,5 +1,5 @@ -API_URL = "https://www.planetromeo.com/api/v4/" -IMAGE_URL = "https://www.planetromeo.com/img/usr/original/0x0/%s.jpg" +API_URL = "https://www.romeo.com/api/v4/" +IMAGE_URL = "https://www.romeo.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 +X_API_KEY = "8JNvPbV3I9KLiNqPSqscNTYeg7uiwvMI" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..81d8a95 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +urllib3 \ No newline at end of file diff --git a/worker.py b/worker.py new file mode 100644 index 0000000..11616ec --- /dev/null +++ b/worker.py @@ -0,0 +1,19 @@ +import json +import datetime +import sqlite3 + +from classes.database import Database +from classes.api import RomeoAPI + +try: + from settings import USERNAME, PASSWORD +except: + USERNAME, PASSWORD = None, None + +api = RomeoAPI(True, USERNAME, PASSWORD) +db = Database() + +db.create_tables() + +for message in api.messages(): + message.to_database() \ No newline at end of file