diff --git a/.gitignore b/.gitignore index a129050..b76ad2b 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,6 @@ resouces/facts.txt secrets.local -resources/facts.txt +facts.txt + +.idea/terminal.xml diff --git a/.idea/terminal.xml b/.idea/terminal.xml index a91d2d0..fbd11ad 100644 --- a/.idea/terminal.xml +++ b/.idea/terminal.xml @@ -4,6 +4,11 @@ + + + + + \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..bcc746e --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +import service \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/common.py b/common.py similarity index 100% rename from util/common.py rename to common.py diff --git a/config.yaml b/config.yaml index c54fc41..efc85f8 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,7 @@ region: us-east-2 function_name: avan_facts_bot handler: service.handler description: Random Avan facts via groupme -runtime: python3.8 +runtime: python3.7 #role: lambda_basic_execution # S3 upload requires appropriate role with s3:PutObject permission @@ -25,7 +25,9 @@ aws_secret_access_key: ${AWS_TOKEN} # Experimental Environment variables environment_variables: BOT_NAME: ${BOT_NAME} + DB_TABLE_NAME: ${DB_TABLE_NAME} GROUPME_KEY: ${GROUPME_KEY} + FACTS_PATH: ${FACTS_PATH} # If `tags` is uncommented then tags will be set at creation or update # time. During an update all other tags will be removed except the tags diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..7bca529 --- /dev/null +++ b/constants.py @@ -0,0 +1,7 @@ +import os + +BOT_NAME = os.environ.get("BOT_NAME") +DB_TABLE_NAME = os.environ.get("DB_TABLE_NAME") +FACTS_PATH = os.environ.get("FACTS_PATH") +GROUPME = os.environ.get("GROUPME_KEY") + diff --git a/model/db_service.py b/db_service.py similarity index 100% rename from model/db_service.py rename to db_service.py diff --git a/dynamo_db.py b/dynamo_db.py new file mode 100644 index 0000000..ab19c9d --- /dev/null +++ b/dynamo_db.py @@ -0,0 +1,34 @@ +import random + +import boto3 + +import constants +from db_service import DbService +import common + + +class DynamoDb(DbService): + + def __init__(self) -> None: + super().__init__() + self.FACTS_LIST = [] + self.dynamodb = boto3.resource('dynamodb') + self.facts_table = self.dynamodb.Table(constants.DB_TABLE_NAME) + print(self.facts_table.creation_date_time) + + def load_facts_from_db(self): + # global facts_table + self.FACTS_LIST.clear() + resp = self.facts_table.scan() + self.FACTS_LIST = [i["fact"] for i in resp["Items"]] + assert self.FACTS_LIST + + def get_fact(self): + if self.FACTS_LIST is None or not self.FACTS_LIST: + self.load_facts_from_db() + return random.choice(self.FACTS_LIST) + + def get_multiple_facts(self, count=2): + if self.FACTS_LIST is None or not self.FACTS_LIST: + self.load_facts_from_db() + return random.sample(self.FACTS_LIST, k=count) diff --git a/app/fact_bot.py b/fact_bot.py similarity index 86% rename from app/fact_bot.py rename to fact_bot.py index 8b39308..cb5687b 100644 --- a/app/fact_bot.py +++ b/fact_bot.py @@ -1,5 +1,6 @@ -from app import fact_service, groupme -from util import constants +import fact_service +import groupme +import constants def post_fact_to_group(): diff --git a/app/fact_service.py b/fact_service.py similarity index 68% rename from app/fact_service.py rename to fact_service.py index da1df68..3319e50 100644 --- a/app/fact_service.py +++ b/fact_service.py @@ -1,9 +1,11 @@ -from model.text_db import TextDb +from dynamo_db import DynamoDb +from text_db import TextDb def get_random_fact(): ret_fact = None - db_service = TextDb() + # db_service = TextDb() + db_service = DynamoDb() ret_fact = db_service.get_fact() return ret_fact diff --git a/resources/facts_sample.txt b/facts_sample.txt similarity index 100% rename from resources/facts_sample.txt rename to facts_sample.txt diff --git a/app/groupme.py b/groupme.py similarity index 97% rename from app/groupme.py rename to groupme.py index 5226c8f..dbf18e6 100644 --- a/app/groupme.py +++ b/groupme.py @@ -1,6 +1,6 @@ from groupy import Client -from util import constants +import constants GROUPME_CLIENT: Client = None diff --git a/model/__init__.py b/model/__init__.py deleted file mode 100644 index b62f6ca..0000000 --- a/model/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from model import db_service - -from model import text_db diff --git a/requirements.txt b/requirements.txt index 129e358..7c23132 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +boto3 GroupyAPI pytest python-lambda \ No newline at end of file diff --git a/service.py b/service.py index 30c412c..b800cf9 100644 --- a/service.py +++ b/service.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from app import fact_bot +import fact_bot def handler(event, context): diff --git a/test_dynamo_db.py b/test_dynamo_db.py new file mode 100644 index 0000000..43fe319 --- /dev/null +++ b/test_dynamo_db.py @@ -0,0 +1,15 @@ +from dynamo_db import DynamoDb + + +class TestDynamoDb: + def test_get_fact(self): + text_db_service = DynamoDb() + random_fact = text_db_service.get_fact() + assert random_fact is not None + assert random_fact != "" + + def test_get_multiple_facts(self): + text_db_service = DynamoDb() + random_facts_list = text_db_service.get_multiple_facts() + assert random_facts_list is not None + assert len(random_facts_list) > 0 diff --git a/tests/test_text_db.py b/test_text_db.py similarity index 89% rename from tests/test_text_db.py rename to test_text_db.py index 52c2b1a..7744be7 100644 --- a/tests/test_text_db.py +++ b/test_text_db.py @@ -1,7 +1,7 @@ -from model.text_db import TextDb +from text_db import TextDb -class TestTestDb: +class TestTextDb: def test_get_fact(self): text_db_service = TextDb() random_fact = text_db_service.get_fact() diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/model/text_db.py b/text_db.py similarity index 79% rename from model/text_db.py rename to text_db.py index 2815796..4c3f1d2 100644 --- a/model/text_db.py +++ b/text_db.py @@ -1,7 +1,8 @@ import random -from model.db_service import DbService -from util import common +import constants +from db_service import DbService +import common class TextDb(DbService): @@ -11,7 +12,7 @@ class TextDb(DbService): self.FACTS_LIST = None def load_facts_from_file(self): - facts_file = open(f"{common.get_project_root()}/resources/facts.txt", mode="r+") + facts_file = open(f"{constants.FACTS_PATH}", mode="r") self.FACTS_LIST = facts_file.readlines() assert self.FACTS_LIST diff --git a/util/__init__.py b/util/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/constants.py b/util/constants.py deleted file mode 100644 index ff19829..0000000 --- a/util/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -import os - -BOT_NAME = os.environ.get("BOT_NAME") -GROUPME = os.environ.get("GROUPME_KEY")