diff --git a/Jupyter/API-tests/News/models/News.py b/Jupyter/API-tests/News/models/News.py new file mode 100644 index 0000000..cb404ad --- /dev/null +++ b/Jupyter/API-tests/News/models/News.py @@ -0,0 +1,13 @@ +from dataclasses import asdict, dataclass + + +@dataclass +class News: + id: str + title: str + date: str + text: str + source_url: str + + def dict(self): + return asdict(self) diff --git a/Jupyter/API-tests/News/models/__init__.py b/Jupyter/API-tests/News/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Jupyter/API-tests/News/notebook.ipynb b/Jupyter/API-tests/News/notebook.ipynb index 75481e0..12dc09b 100644 --- a/Jupyter/API-tests/News/notebook.ipynb +++ b/Jupyter/API-tests/News/notebook.ipynb @@ -678,6 +678,99 @@ "custom_search_data = tagesschau.custom_search(\"Haltern am See\")\n", "custom_search_data" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from utils.mongodb.mongo import MongoConnector\n", + "\n", + "connector = MongoConnector(\n", + " hostname=\"localhost\",\n", + " database=\"transparenzregister\",\n", + " username=\"root\",\n", + " password=\"pR0R0v2e2\",\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[News(id='id', title='title', date='date', text='text'),\n", + " News(id='abc', title='Hallo Welt', date='heute', text='what does the fox say')]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from utils.mongodb.mongo import MongoNewsService\n", + "\n", + "service = MongoNewsService(connector)\n", + "service.get_all()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'id': 'abc', 'title': 'Hallo Welt', 'date': 'heute', 'text': 'what does the fox say'}\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from models.News import News\n", + "\n", + "service.insert(\n", + " News(\n", + " \"abc\", \"Hallo Welt\", \"heute\", \"what does the fox say\", \"https://localhost:8080\"\n", + " )\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "News(id='abc', title='Hallo Welt', date='heute', text='what does the fox say')" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "service.get_by_id(\"abc\")" + ] } ], "metadata": { diff --git a/Jupyter/API-tests/News/requirements.txt b/Jupyter/API-tests/News/requirements.txt new file mode 100644 index 0000000..8c7d698 --- /dev/null +++ b/Jupyter/API-tests/News/requirements.txt @@ -0,0 +1 @@ +pymongo \ No newline at end of file diff --git a/Jupyter/API-tests/News/utils/NewsServiceInterface.py b/Jupyter/API-tests/News/utils/NewsServiceInterface.py new file mode 100644 index 0000000..a69318b --- /dev/null +++ b/Jupyter/API-tests/News/utils/NewsServiceInterface.py @@ -0,0 +1,17 @@ +from abc import ABC + +from models.News import News + + +class NewsServiceInterface(ABC): + def get_all(self) -> list[News]: + raise NotImplementedError + + def get_by_id(self, id: str) -> News | None: + raise NotImplementedError + + def insert(self, news: News): + raise NotImplementedError + + def insert_many(self, news: list[News]): + raise NotImplementedError diff --git a/Jupyter/API-tests/News/utils/__init__.py b/Jupyter/API-tests/News/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Jupyter/API-tests/News/utils/mongodb/mongo.py b/Jupyter/API-tests/News/utils/mongodb/mongo.py new file mode 100644 index 0000000..4ebc1d7 --- /dev/null +++ b/Jupyter/API-tests/News/utils/mongodb/mongo.py @@ -0,0 +1,65 @@ +import pymongo +from models.News import News +from utils.NewsServiceInterface import NewsServiceInterface + + +class MongoConnector: + def __init__( + self, + hostname, + database: str, + port: int = 27017, + username: str | None = None, + password: str | None = None, + ): + self.client = self.connect(hostname, port, username, password) + databases = self.client.list_database_names() + if database not in databases: + print(f"Database {database} will be created") + self.database = self.client[database] + + def connect(self, hostname, port, username, password) -> pymongo.MongoClient: + if username is not None and password is not None: + connection_string = f"mongodb://{username}:{password}@{hostname}:{port}" + else: + connection_string = f"mongodb://{hostname}:{port}" + + return pymongo.MongoClient(connection_string) + + +class MongoNewsService(NewsServiceInterface): + def __init__(self, connector: MongoConnector): + self.collection = connector.database["news"] + + def get_all(self) -> list[News]: + result = self.collection.find() + return [MongoEntryTransformer.transform_outgoing(elem) for elem in result] + + def get_by_id(self, id: str) -> News | None: + result = list(self.collection.find({"_id": id})) + if len(result) == 1: + return MongoEntryTransformer.transform_outgoing(list(result)[0]) + return None + + def insert(self, news: News): + return self.collection.insert_one(MongoEntryTransformer.transform_ingoing(news)) + + +class MongoEntryTransformer: + @staticmethod + def transform_ingoing(news: News) -> dict: + transport_object = news.dict() + print(transport_object) + transport_object["_id"] = news.id + del transport_object["id"] + return transport_object + + @staticmethod + def transform_outgoing(data: dict) -> News: + return News( + id=data["_id"], + title=data["title"], + date=data["date"], + text=data["text"], + source_url=data["source_url"], + )