extracted mongo db connections

pull/231/head
Yariv Menachem 2024-12-30 16:45:36 +02:00
parent 9c18c2e936
commit 768b478d66
3 changed files with 65 additions and 23 deletions

View File

@ -1,9 +1,9 @@
import os
from typing import List
from dotenv import load_dotenv
from pymongo import MongoClient, UpdateOne
import pymongo
from dotenv import load_dotenv
from pymongo import UpdateOne
from .monogo_db import MongoDB
from .. import create_logger
from ..jobs import JobPost
@ -11,27 +11,20 @@ load_dotenv()
class JobRepository:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(JobRepository, cls).__new__(cls)
return cls.instance
_instance = None
def __init__(self, database_name: str = None):
def __new__(cls):
if cls._instance is not None:
return cls._instance
self = super().__new__(cls)
cls._instance = self
self.logger = create_logger("JobRepository")
self.mongoUri = os.getenv("MONGO_URI")
if not self.mongoUri:
self.logger.error("MONGO_URI environment variable is not set")
raise ValueError("MONGO_URI environment variable is not set")
self.client = MongoClient(self.mongoUri)
if database_name is None:
database_name = os.getenv("MONGO_DB_NAME")
if not database_name:
self.logger.error("MONGO_DB_NAME environment variable is not set")
raise ValueError(
"MONGO_DB_NAME environment variable is not set")
self.db = self.client[database_name]
self.collection = self.db["jobs"]
mongo_client = MongoDB()
self.collection = mongo_client.db["jobs"]
self.logger.info("Succeed connect to MongoDB")
return cls._instance
def insert_job(self, job: JobPost):
job_dict = job.model_dump(exclude={"date_posted"})
@ -60,7 +53,7 @@ class JobRepository:
# Execute all operations in bulk
result = self.collection.bulk_write(operations)
self.logger.info(f"Matched: {result.matched_count}, Upserts: {
result.upserted_count}, Modified: {result.modified_count}")
result.upserted_count}, Modified: {result.modified_count}")
# Get the newly inserted jobs (those that were upserted)
# The `upserted_count` corresponds to how many new documents were inserted

View File

@ -0,0 +1,32 @@
import os
from pymongo import MongoClient
from pymongo.synchronous.database import Database
from jobspy import create_logger
class MongoDB:
_instance = None
db:Database = None
def __new__(cls):
if cls._instance is not None:
return cls._instance
self = super().__new__(cls)
cls._instance = self
logger = create_logger("Mongo Client")
mongoUri = os.getenv("MONGO_URI")
if not mongoUri:
logger.error("MONGO_URI environment variable is not set")
raise ValueError("MONGO_URI environment variable is not set")
client = MongoClient(mongoUri)
database_name = os.getenv("MONGO_DB_NAME")
if not database_name:
logger.error("MONGO_DB_NAME environment variable is not set")
raise ValueError(
"MONGO_DB_NAME environment variable is not set")
self.db = client[database_name]
return cls._instance

View File

@ -1,5 +1,6 @@
import os
from pymongo import MongoClient
from telegram import Update
from telegram.ext import Application, CommandHandler, CallbackQueryHandler
@ -14,12 +15,28 @@ title_filters: list[str] = ["test", "qa", "Lead", "Full-Stack", "Full Stack", "F
"automation", "BI ", "Principal", "Architect", "Android", "Machine Learning", "Student",
"Data Engineer", "DevSecOps"]
def connect_db():
logger = create_logger("Mongo Client")
mongoUri = os.getenv("MONGO_URI")
if not mongoUri:
logger.error("MONGO_URI environment variable is not set")
raise ValueError("MONGO_URI environment variable is not set")
client = MongoClient(mongoUri)
database_name = os.getenv("MONGO_DB_NAME")
if not database_name:
logger.error("MONGO_DB_NAME environment variable is not set")
raise ValueError(
"MONGO_DB_NAME environment variable is not set")
return client[database_name]
if __name__ == "__main__":
logger.info("Starting initialize ")
_api_token = os.getenv("TELEGRAM_API_TOKEN")
search_term = "software engineer"
locations = ["Tel Aviv, Israel", "Ramat Gan, Israel",
"Central, Israel", "Rehovot ,Israel"]
db = connect_db()
application = Application.builder().token(_api_token).build()
tg_callback_handler = TelegramCallHandler()
tg_handler_all = TelegramDefaultHandler(sites=[Site.LINKEDIN, Site.GLASSDOOR, Site.INDEED, Site.GOOZALI],