-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ingest save time tracking by post type * Store subreddit data * Store subreddit data
- Loading branch information
1 parent
58e284c
commit 0fc5a11
Showing
10 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,7 @@ | |
target_metadata = Base.metadata | ||
|
||
def get_conn_string(): | ||
conn_str = r'mysql+pymysql://barry:[email protected]/reddit_dev' | ||
return conn_str | ||
#return f'mysql+pymysql://{bot_config.db_user}:{quote_plus(bot_config.db_password)}@{bot_config.db_host}/{bot_config.db_name}' | ||
|
||
return f'mysql+pymysql://{bot_config.db_user}:{quote_plus(bot_config.db_password)}@{bot_config.db_host}/{bot_config.db_name}' | ||
|
||
# other values from the config, defined by the needs of env.py, | ||
# can be acquired: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import datetime | ||
|
||
import requests | ||
|
||
from redditrepostsleuth.core.celery import celery | ||
from redditrepostsleuth.core.celery.basetasks import SqlAlchemyTask | ||
from redditrepostsleuth.core.db.databasemodels import Subreddit | ||
from redditrepostsleuth.core.exception import UtilApiException | ||
from redditrepostsleuth.core.logging import configure_logger | ||
|
||
log = configure_logger( | ||
name='redditrepostsleuth', | ||
) | ||
|
||
|
||
@celery.task(bind=True, base=SqlAlchemyTask, autoretry_for=(UtilApiException,), retry_kwards={'max_retries': 50, 'countdown': 600}) | ||
def update_subreddit_data(self, subreddit_name) -> None: | ||
try: | ||
with self.uowm.start() as uow: | ||
subreddit = uow.subreddit.get_by_name(subreddit_name) | ||
url_to_fetch = f'{self.config.util_api}/reddit/subreddit?name={subreddit.name}' | ||
res = requests.get(url_to_fetch) | ||
if res.status_code != 200: | ||
log.error('Bad status %s from util API when checking subreddit %s', res.status_code, subreddit.name) | ||
raise UtilApiException(f'Bad status {res.status_code} checking {subreddit_name}') | ||
|
||
subreddit_data = res.json()['data'] | ||
subreddit.subscribers = subreddit_data['subscribers'] or 0 | ||
subreddit.nsfw = subreddit_data['over18'] or False | ||
subreddit.last_checked = datetime.datetime.now(datetime.UTC) | ||
uow.commit() | ||
log.debug('Update subreddit data for %s. NSFW: %s - Subscribers: %s', subreddit.name, subreddit.nsfw, subreddit.subscribers) | ||
except UtilApiException as e: | ||
raise e | ||
except Exception as e: | ||
log.exception('') | ||
|
||
@celery.task(bind=True, base=SqlAlchemyTask, ignore_reseults=True, serializer='pickle') | ||
def save_subreddit(self, subreddit_name: str): | ||
try: | ||
with self.uowm.start() as uow: | ||
existing = uow.subreddit.get_by_name(subreddit_name) | ||
if existing: | ||
log.debug('Subreddit %s already exists', subreddit_name) | ||
return | ||
subreddit = Subreddit(name=subreddit_name) | ||
uow.subreddit.add(subreddit) | ||
uow.commit() | ||
log.debug('Saved Subreddit %s', subreddit_name) | ||
update_subreddit_data.apply_async((subreddit_name,)) | ||
except Exception as e: | ||
log.exception('') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import datetime | ||
|
||
from sqlalchemy import or_ | ||
|
||
from redditrepostsleuth.core.db.databasemodels import Subreddit | ||
|
||
|
||
class SubredditRepo: | ||
def __init__(self, db_session): | ||
self.db_session = db_session | ||
|
||
def add(self, item): | ||
self.db_session.add(item) | ||
|
||
def get_by_name(self, name: str): | ||
return self.db_session.query(Subreddit).filter(Subreddit.name == name).first() | ||
|
||
def get_subreddits_to_update(self, limit: int = None, offset: int = None) -> list[Subreddit]: | ||
delta = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=3) | ||
return self.db_session.query(Subreddit).filter(or_(Subreddit.added_at < delta, Subreddit.last_checked == None)).limit(limit).offset(offset).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters