-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a JSON database to add state of when posts were processed.
- Loading branch information
1 parent
a57feca
commit 8f8a551
Showing
5 changed files
with
151 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ coloredlogs | |
selenium==4.4.* | ||
boto3==1.24.* | ||
schedule==1.1.* | ||
twilio==7.13.* | ||
twilio==7.13.* | ||
pysondb==1.6.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Interface for saving curly girl posts and artifacts | ||
""" | ||
|
||
# Standard library | ||
from hashlib import sha256 | ||
from datetime import datetime | ||
import os | ||
|
||
# Third party libraries | ||
import pysondb | ||
|
||
|
||
DATEFORMAT = "%d/%m/%Y-%H:%M:%S" | ||
|
||
|
||
def initialize_database(database_path: str = f"{os.getcwd()}/curlygirl.json") -> pysondb.db.JsonDatabase: | ||
"""Initialize pysondb | ||
Args: | ||
database_path (str, optional): Path to database. Defaults to "CURRENT_DIR/curlygirl.json". | ||
Returns: | ||
pysondb.db: database object. | ||
""" | ||
return pysondb.db.getDb(database_path) | ||
|
||
|
||
def save_post(post_text: str, db: pysondb.db.JsonDatabase): | ||
"""Save post to database | ||
Args: | ||
post_text (str): Text of the post saved. | ||
db (pysondb.db): Database being saved to. | ||
Returns: | ||
id (str): Id of the post. | ||
""" | ||
id_sha256 = compute_post_id(post_text) | ||
|
||
if db.getByQuery({"id_post": id_sha256}): | ||
return id_sha256 | ||
|
||
db.add({"post_text": post_text, "time": datetime.now().strftime( | ||
DATEFORMAT), "id_post": f"{id_sha256}"}) | ||
return id_sha256 | ||
|
||
|
||
def compute_post_id(post_text: str): | ||
return sha256(post_text.encode()).hexdigest() | ||
|
||
|
||
def get_post_timestamp(post_id: str, db: pysondb.db.JsonDatabase): | ||
post = db.getByQuery({"id_post": post_id}) | ||
if len(post) != 1: | ||
assert "Multiple entries with the same id should not be possible" | ||
|
||
return datetime.strptime(post[0]["time"], DATEFORMAT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Standard library | ||
import unittest | ||
import os | ||
|
||
# Module to test | ||
import curly_db | ||
from main import is_post_too_old | ||
|
||
|
||
class TestCurlyDb(unittest.TestCase): | ||
database_path = "/tmp/curly_girl_test_db.json" | ||
|
||
def setUp(self) -> None: | ||
print("Setup of test") | ||
self.db = curly_db.initialize_database(self.database_path) | ||
|
||
def tearDown(self) -> None: | ||
print("Teardown of test") | ||
self.db.deleteAll() | ||
os.system(f"rm -rf {self.db.filename}*") | ||
|
||
def test_inserting_post(self): | ||
# 1 | ||
# Insert random post text and check if it is there afterwards | ||
post_text = "Afbud! Der er kommet en tid på mandag bla bla bla." | ||
post_id = curly_db.save_post(post_text=post_text, db=self.db) | ||
self.assertEqual(post_text, self.db.getByQuery( | ||
{"id_post": post_id})[0]["post_text"]) | ||
|
||
# 2 | ||
# Inserting identical post and only one entry should be there. | ||
post_id = curly_db.save_post(post_text=post_text, db=self.db) | ||
self.assertTrue(len(self.db.getByQuery({"id_post": post_id})) == 1) | ||
|
||
def test_getting_timestamp(self): | ||
# 1 | ||
# Timestamp here and now | ||
post_text = "Afbud! Der er kommet en tid på mandag bla bla bla." | ||
post_id = curly_db.save_post(post_text=post_text, db=self.db) | ||
time_stamp = curly_db.get_post_timestamp(post_id, self.db) | ||
self.assertTrue(not is_post_too_old(10, time_stamp)) | ||
|
||
# 2 | ||
# Make sure it is the current time which is timestamped. | ||
self.assertTrue(is_post_too_old(0, time_stamp)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters