mirror of https://github.com/Bunsly/JobSpy
fixed fire call button to update the job in db applied = true
parent
afa18099bb
commit
34a1933ecd
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from pymongo import UpdateOne
|
from pymongo import UpdateOne
|
||||||
|
|
||||||
|
@ -24,7 +26,42 @@ class JobRepository:
|
||||||
self.logger.info("Succeed connect to MongoDB")
|
self.logger.info("Succeed connect to MongoDB")
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
|
def find_by_id(self, job_id: str) -> Optional[JobPost]:
|
||||||
|
"""
|
||||||
|
Finds a job document in the collection by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
job_id: The ID of the job to find.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The job document if found, otherwise None.
|
||||||
|
"""
|
||||||
|
result = self.collection.find_one({"id": job_id})
|
||||||
|
return result
|
||||||
|
|
||||||
|
def update(self, job_data: dict) -> bool:
|
||||||
|
"""
|
||||||
|
Updates a JobPost in the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
job_data: A dictionary representing the JobPost data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if the update was successful, False otherwise.
|
||||||
|
"""
|
||||||
|
result = self.collection.update_one({"id": job_data["id"]}, {"$set": job_data})
|
||||||
|
return result.modified_count > 0
|
||||||
|
|
||||||
def insert_job(self, job: JobPost):
|
def insert_job(self, job: JobPost):
|
||||||
|
"""
|
||||||
|
Inserts a new job posting into the database collection.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
job (JobPost): The JobPost object to be inserted.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: If an error occurs during insertion.
|
||||||
|
"""
|
||||||
job_dict = job.model_dump(exclude={"date_posted"})
|
job_dict = job.model_dump(exclude={"date_posted"})
|
||||||
self.collection.insert_one(job_dict)
|
self.collection.insert_one(job_dict)
|
||||||
self.logger.info(f"Inserted new job with title {job.title}.")
|
self.logger.info(f"Inserted new job with title {job.title}.")
|
||||||
|
|
|
@ -279,6 +279,7 @@ class JobPost(BaseModel):
|
||||||
is_remote: bool | None = None
|
is_remote: bool | None = None
|
||||||
listing_type: str | None = None
|
listing_type: str | None = None
|
||||||
field: str | None = None
|
field: str | None = None
|
||||||
|
applied: bool = False
|
||||||
|
|
||||||
# linkedin specific
|
# linkedin specific
|
||||||
job_level: str | None = None
|
job_level: str | None = None
|
||||||
|
|
|
@ -1,24 +1,54 @@
|
||||||
from telegram import MaybeInaccessibleMessage
|
from telegram import MaybeInaccessibleMessage
|
||||||
from telegram.constants import ReactionEmoji
|
from telegram.constants import ReactionEmoji
|
||||||
|
|
||||||
|
from db.job_repository import JobRepository
|
||||||
|
from jobspy import create_logger
|
||||||
from telegram_bot import TelegramBot
|
from telegram_bot import TelegramBot
|
||||||
from telegram_handler.button_callback.button_strategy import ButtonStrategy
|
from telegram_handler.button_callback.button_strategy import ButtonStrategy
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_job_id(message: str) -> str:
|
||||||
|
"""
|
||||||
|
Extracts the job ID from a job description string.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message: The string containing the job description.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The extracted job ID, or an empty string if not found.
|
||||||
|
"""
|
||||||
|
# Find the starting position of the ID
|
||||||
|
start_pos = message.find("Job ID: ")
|
||||||
|
if start_pos == -1:
|
||||||
|
return "" # Not found
|
||||||
|
|
||||||
|
# Find the ending position of the ID (excluding newline)
|
||||||
|
end_pos = message.find("\n", start_pos + len("Job ID: "))
|
||||||
|
if end_pos == -1:
|
||||||
|
end_pos = len(message) # No newline, use string end
|
||||||
|
|
||||||
|
# Extract the ID substring
|
||||||
|
return message[start_pos + len("Job ID: "):end_pos]
|
||||||
|
|
||||||
|
|
||||||
class FireStrategy(ButtonStrategy):
|
class FireStrategy(ButtonStrategy):
|
||||||
def __init__(self, message: MaybeInaccessibleMessage) -> None:
|
def __init__(self, message: MaybeInaccessibleMessage) -> None:
|
||||||
"""
|
"""
|
||||||
Usually, the Context accepts a strategy through the constructor, but
|
Usually, the Context accepts a strategy through the constructor, but
|
||||||
also provides a setter to change it at runtime.
|
also provides a setter to change it at runtime.
|
||||||
"""
|
"""
|
||||||
self.message = message
|
self._message = message
|
||||||
self._emoji = ReactionEmoji.FIRE
|
self._emoji = ReactionEmoji.FIRE
|
||||||
self.telegram_bot = TelegramBot()
|
self._telegram_bot = TelegramBot()
|
||||||
|
self._job_repository = JobRepository()
|
||||||
|
self._logger = create_logger("FireStrategy")
|
||||||
|
|
||||||
async def execute(self):
|
async def execute(self):
|
||||||
await self.telegram_bot.set_message_reaction(self.message.message_id, self._emoji)
|
job_id = _extract_job_id(self._message.text)
|
||||||
# find the position in DB
|
job = self._job_repository.find_by_id(job_id)
|
||||||
# set applied to True
|
if not job:
|
||||||
# save
|
self._logger.error(f"Job with ID {job_id} not found.")
|
||||||
# set reaction to message
|
return
|
||||||
pass
|
job["applied"] = True
|
||||||
|
self._job_repository.update(job)
|
||||||
|
await self._telegram_bot.set_message_reaction(self._message.message_id, self._emoji)
|
||||||
|
|
Loading…
Reference in New Issue