Add logging to reddit api class
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
|
import requests
|
||||||
|
import logging
|
||||||
|
|
||||||
from dto.post import Post
|
from dto.post import Post
|
||||||
from dto.user import User
|
from dto.user import User
|
||||||
import requests
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class RedditAPI:
|
class RedditAPI:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -13,6 +17,8 @@ class RedditAPI:
|
|||||||
'limit': limit,
|
'limit': limit,
|
||||||
't': timeframe
|
't': timeframe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(f"Fetching top posts from subreddit: {subreddit} with limit {limit} and timeframe '{timeframe}'")
|
||||||
url = f"r/{subreddit}/top.json"
|
url = f"r/{subreddit}/top.json"
|
||||||
data = self._fetch_data(url, params)
|
data = self._fetch_data(url, params)
|
||||||
return self._parse_posts(data)
|
return self._parse_posts(data)
|
||||||
@@ -25,16 +31,19 @@ class RedditAPI:
|
|||||||
'sort': 'top',
|
'sort': 'top',
|
||||||
"t": timeframe
|
"t": timeframe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(f"Searching subreddit '{subreddit}' for '{search}' with limit {limit} and timeframe '{timeframe}'")
|
||||||
url = f"r/{subreddit}/search.json"
|
url = f"r/{subreddit}/search.json"
|
||||||
data = self._fetch_data(url, params)
|
data = self._fetch_data(url, params)
|
||||||
return self._parse_posts(data)
|
return self._parse_posts(data)
|
||||||
|
|
||||||
def get_new_subreddit_posts(self, subreddit: str, limit: int = 10) -> list[Post]:
|
def get_new_subreddit_posts(self, subreddit: str, limit: int = 10) -> list[Post]:
|
||||||
|
|
||||||
posts = []
|
posts = []
|
||||||
after = None
|
after = None
|
||||||
url = f"r/{subreddit}/new.json"
|
url = f"r/{subreddit}/new.json"
|
||||||
|
|
||||||
|
logger.info(f"Fetching new posts from subreddit: {subreddit}")
|
||||||
|
|
||||||
while len(posts) < limit:
|
while len(posts) < limit:
|
||||||
batch_limit = min(100, limit - len(posts))
|
batch_limit = min(100, limit - len(posts))
|
||||||
params = {
|
params = {
|
||||||
@@ -45,6 +54,8 @@ class RedditAPI:
|
|||||||
data = self._fetch_data(url, params)
|
data = self._fetch_data(url, params)
|
||||||
batch = self._parse_posts(data)
|
batch = self._parse_posts(data)
|
||||||
|
|
||||||
|
logger.debug(f"Fetched {len(batch)} new posts from subreddit {subreddit}")
|
||||||
|
|
||||||
if not batch:
|
if not batch:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user