This commit is contained in:
zman 2024-03-05 09:21:57 -05:00
parent 431584ae89
commit 91fce540d4
4 changed files with 19 additions and 14 deletions

View File

@ -74,6 +74,7 @@ class PostManager:
api_request_handler (ApiRequestHandler): Handles the API requests for interacting with post data. api_request_handler (ApiRequestHandler): Handles the API requests for interacting with post data.
log_manager (LoggingManager): Manages logging for operations performed by PostManager. log_manager (LoggingManager): Manages logging for operations performed by PostManager.
""" """
def __init__(self, api_request_handler: ApiRequestHandler): def __init__(self, api_request_handler: ApiRequestHandler):
""" """
Initializes the PostManager with an API request handler for making API calls and a logging manager for logging. Initializes the PostManager with an API request handler for making API calls and a logging manager for logging.
@ -166,6 +167,7 @@ class PostAnalyticsManager:
post_manager (PostManager): Manages post retrieval and existence checks. post_manager (PostManager): Manages post retrieval and existence checks.
log_manager (LoggingManager): Manages logging for analytics operations. log_manager (LoggingManager): Manages logging for analytics operations.
""" """
def __init__( def __init__(
self, api_request_handler: ApiRequestHandler, post_manager: PostManager self, api_request_handler: ApiRequestHandler, post_manager: PostManager
): ):
@ -202,8 +204,8 @@ class PostAnalyticsManager:
now = datetime.now(timezone) now = datetime.now(timezone)
# Format datetime objects for the API request # Format datetime objects for the API request
time_begin_str = time_start.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' time_begin_str = time_start.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
time_end_str = now.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z' time_end_str = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
post_id = self.post_manager.get_post_by_reddit_id(reddit_id) post_id = self.post_manager.get_post_by_reddit_id(reddit_id)
post_id = post_id[0]["id"] post_id = post_id[0]["id"]
@ -259,6 +261,7 @@ class RedditMonitor:
subreddit (praw.models.Subreddit): The subreddit object for the specified subreddit. subreddit (praw.models.Subreddit): The subreddit object for the specified subreddit.
log_manager (LoggingManager): Manages logging for Reddit monitoring operations. log_manager (LoggingManager): Manages logging for Reddit monitoring operations.
""" """
def __init__( def __init__(
self, client_id, client_secret, user_agent, username, password, subreddit_name self, client_id, client_secret, user_agent, username, password, subreddit_name
): ):
@ -325,6 +328,7 @@ class SubmissionManager:
webhook_notifier (WebhookNotifier): Handles notifications for new or updated posts. webhook_notifier (WebhookNotifier): Handles notifications for new or updated posts.
log_manager (LoggingManager): Manages logging for submission processing operations. log_manager (LoggingManager): Manages logging for submission processing operations.
""" """
def __init__( def __init__(
self, self,
reddit_monitor: RedditMonitor, reddit_monitor: RedditMonitor,
@ -386,7 +390,9 @@ class SubmissionManager:
if self.post_manager.post_exists(submission.id): if self.post_manager.post_exists(submission.id):
self.log_manager.log("Post exists") self.log_manager.log("Post exists")
self.log_manager.log(f"post id: {submission.id}") self.log_manager.log(f"post id: {submission.id}")
if self.post_analytics_manager.check_update_requirements(submission.id, update_frequency): if self.post_analytics_manager.check_update_requirements(
submission.id, update_frequency
):
self.log_manager.log("Update requirements met") self.log_manager.log("Update requirements met")
post = self.convert_submission_to_post(submission) post = self.convert_submission_to_post(submission)
self.post_analytics_manager.update_post_analytics(post) self.post_analytics_manager.update_post_analytics(post)
@ -415,6 +421,7 @@ class Application:
thread_manager: Manages threading for asynchronous operations. thread_manager: Manages threading for asynchronous operations.
update_frequency (int): The frequency, in seconds, at which post analytics should be updated. update_frequency (int): The frequency, in seconds, at which post analytics should be updated.
""" """
def __init__( def __init__(
self, self,
reddit_monitor, reddit_monitor,
@ -423,7 +430,7 @@ class Application:
post_manager, post_manager,
post_analytics_manager, post_analytics_manager,
submission_manager, submission_manager,
update_frequency update_frequency,
): ):
""" """
Initializes the application with all necessary components. Initializes the application with all necessary components.
@ -465,7 +472,9 @@ class Application:
""" """
Initializes and runs the scheduler for periodic updates. Initializes and runs the scheduler for periodic updates.
""" """
self.scheduler = Scheduler(self.update_frequency, lambda: self.periodic_update(self.update_frequency)) self.scheduler = Scheduler(
self.update_frequency, lambda: self.periodic_update(self.update_frequency)
)
self.scheduler.run() self.scheduler.run()
def run(self): def run(self):
@ -474,9 +483,7 @@ class Application:
and processing submissions. and processing submissions.
""" """
self.log_manager.info("Application started") self.log_manager.info("Application started")
self.thread_manager = ThreadManager( self.thread_manager = ThreadManager(target=self.run_periodic_update, args=())
target=self.run_periodic_update, args=()
)
self.thread_manager.run() self.thread_manager.run()
submissions = self.reddit_monitor.stream_submissions() submissions = self.reddit_monitor.stream_submissions()
self.submission_manager.process_submissions(submissions, self.update_frequency) self.submission_manager.process_submissions(submissions, self.update_frequency)

View File

@ -12,4 +12,4 @@ class Config:
USER_AGENT = "praw:zman.video_repost_bot:v0.1.0 (by u/jzman21)" USER_AGENT = "praw:zman.video_repost_bot:v0.1.0 (by u/jzman21)"
DISABLE_WEBHOOK = False DISABLE_WEBHOOK = False
API_URL = "http://server:8000/api/" API_URL = "http://server:8000/api/"
UPDATE_FREQUENCY = 60 * 15 # 15 minutes UPDATE_FREQUENCY = 60 * 15 # 15 minutes

View File

@ -42,17 +42,14 @@ if __name__ == "__main__":
post_manager, post_manager,
post_analytics_manager, post_analytics_manager,
submission_manager, submission_manager,
update_frequency update_frequency,
) )
app.run() app.run()
""" """
TODO: TODO:
- pull upvote ration into analytics?
- sqlite vs postgres figure out
- basic front end (react) - basic front end (react)
- tests - tests
- logging
- Filter out canadian/uk deals - Filter out canadian/uk deals
- track score and number of comments over time in db - track score and number of comments over time in db
- try to identify product, number of cards, price per card, etc - try to identify product, number of cards, price per card, etc
@ -60,4 +57,5 @@ TODO:
- try to identify platform ie. costco for gift card, tiktok for coupons, etc. - try to identify platform ie. costco for gift card, tiktok for coupons, etc.
- support for craigslist, ebay, etc. - support for craigslist, ebay, etc.
- front end - vizualization, classification, lookup, etc. - front end - vizualization, classification, lookup, etc.
- postgres
""" """

View File

@ -24,4 +24,4 @@ class WebhookNotifier:
try: try:
requests.post(self.webhook_url, data={"content": content}) requests.post(self.webhook_url, data={"content": content})
except Exception as e: except Exception as e:
self.log_manager.error(f"Failed to send notification: {e}") self.log_manager.error(f"Failed to send notification: {e}")