Make it work again - TODO: Update docs
This commit is contained in:
parent
b0a009f882
commit
d9ac93d8b7
10 changed files with 94 additions and 60 deletions
11
api.py
11
api.py
|
@ -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
|
|
||||||
|
|
|
@ -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:
|
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:
|
if login:
|
||||||
self.login(username, password)
|
self.login(username, password)
|
||||||
|
|
||||||
|
|
||||||
def login(self, username, password):
|
def login(self, username, password):
|
||||||
payload = {"username": USERNAME,
|
payload = {"username": username,
|
||||||
"password": PASSWORD,
|
"password": password,
|
||||||
"keep_login": True}
|
"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"))
|
json.dumps(payload).encode("utf-8"))
|
||||||
|
except HTTPError as e:
|
||||||
|
print(e.read())
|
||||||
|
raise
|
||||||
|
|
||||||
data = json.load(response)
|
data = json.load(response)
|
||||||
self.session = data
|
self.session = data
|
||||||
|
|
||||||
def build_headers(self, additional={}):
|
def build_headers(self, additional={}):
|
||||||
headers = {
|
headers = {
|
||||||
"X-Api-Key": "vuEp8o93b34CxUCljSMFEdhI70qDWtuk",
|
"X-Api-Key": X_API_KEY,
|
||||||
"X-Session-Id": self.session["session_id"]
|
"X-Session-Id": self.session["session_id"] if self.session else ""
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value in additional:
|
for key, value in additional:
|
||||||
|
@ -35,4 +48,5 @@ class RomeoAPI:
|
||||||
data = json.load(response)
|
data = json.load(response)
|
||||||
|
|
||||||
for message in data["items"]:
|
for message in data["items"]:
|
||||||
yield Message.from_dict(message)
|
if messageobj := Message.from_dict(message):
|
||||||
|
yield messageobj
|
||||||
|
|
39
classes/attachment.py
Normal file
39
classes/attachment.py
Normal file
|
@ -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
|
|
@ -1,6 +1,6 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from urllib3 import urlopen
|
from urllib.request import urlopen
|
||||||
|
|
||||||
from .http import HTTPRequest
|
from .http import HTTPRequest
|
||||||
|
|
||||||
|
@ -27,34 +27,3 @@ class Database:
|
||||||
|
|
||||||
def fetchone(self, *args, **kwargs):
|
def fetchone(self, *args, **kwargs):
|
||||||
return self.cur.fetchone(*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)
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from urllib3 import Request, urlopen
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
from const import USER_AGENT, X_API_KEY
|
from const import USER_AGENT, X_API_KEY
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,10 @@ class Location:
|
||||||
self.country = country
|
self.country = country
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
|
|
||||||
def to_database(self, exceptions=False):
|
def to_database(self, db=None, exceptions=False):
|
||||||
try:
|
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))
|
(self.profile, self.timestamp, self.name, self.country))
|
||||||
except:
|
except:
|
||||||
if not exceptions:
|
if not exceptions:
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from .database import Database
|
from .database import Database
|
||||||
|
from .attachment import Attachment
|
||||||
|
|
||||||
|
|
||||||
class Message:
|
class Message:
|
||||||
|
@ -10,12 +11,13 @@ class Message:
|
||||||
self.text = text
|
self.text = text
|
||||||
self.attachments = attachments
|
self.attachments = attachments
|
||||||
|
|
||||||
def to_database(self, exceptions=False):
|
def to_database(self, db=None, exceptions=False):
|
||||||
try:
|
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))
|
(self.id, self.sender, self.recipient, self.date, self.text))
|
||||||
for attachment in self.attachments:
|
for attachment in self.attachments:
|
||||||
attachment.to_database()
|
attachment.to_database(db)
|
||||||
except:
|
except:
|
||||||
if not exceptions:
|
if not exceptions:
|
||||||
return
|
return
|
||||||
|
|
6
const.py
6
const.py
|
@ -1,5 +1,5 @@
|
||||||
API_URL = "https://www.planetromeo.com/api/v4/"
|
API_URL = "https://www.romeo.com/api/v4/"
|
||||||
IMAGE_URL = "https://www.planetromeo.com/img/usr/original/0x0/%s.jpg"
|
IMAGE_URL = "https://www.romeo.com/img/usr/original/0x0/%s.jpg"
|
||||||
|
|
||||||
USER_AGENT = "RomeoTools (https://kumig.it/kumitterer/romeotools.git)"
|
USER_AGENT = "RomeoTools (https://kumig.it/kumitterer/romeotools.git)"
|
||||||
X_API_KEY = "vuEp8o93b34CxUCljSMFEdhI70qDWtuk"
|
X_API_KEY = "8JNvPbV3I9KLiNqPSqscNTYeg7uiwvMI"
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
urllib3
|
19
worker.py
Normal file
19
worker.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue