implement pagination to search subreddit method & remove timeframe attr
In addition, it now searches new posts instead of top
This commit is contained in:
@@ -14,19 +14,34 @@ class RedditAPI:
|
|||||||
self.source_name = "Reddit"
|
self.source_name = "Reddit"
|
||||||
|
|
||||||
# Public Methods #
|
# Public Methods #
|
||||||
def search_subreddit(self, search: str, subreddit: str, limit: int = 10, timeframe: str = "day") -> list[Post]:
|
def search_new_subreddit_posts(self, search: str, subreddit: str, limit: int = 10) -> tuple[list[Post], list[Comment]]:
|
||||||
params = {
|
params = {
|
||||||
'q': search,
|
'q': search,
|
||||||
'limit': limit,
|
'limit': limit,
|
||||||
'restrict_sr': 'on',
|
'restrict_sr': 'on',
|
||||||
'sort': 'top',
|
'sort': 'new'
|
||||||
"t": timeframe
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(f"Searching subreddit '{subreddit}' for '{search}' with limit {limit} and timeframe '{timeframe}'")
|
logger.info(f"Searching subreddit '{subreddit}' for '{search}' with limit {limit}")
|
||||||
url = f"r/{subreddit}/search.json"
|
url = f"r/{subreddit}/search.json"
|
||||||
data = self._fetch_data(url, params)
|
posts = []
|
||||||
return self._parse_posts(data)
|
comments = []
|
||||||
|
|
||||||
|
while len(posts) < limit:
|
||||||
|
batch_limit = min(100, limit - len(posts))
|
||||||
|
params['limit'] = batch_limit
|
||||||
|
|
||||||
|
data = self._fetch_data(url, params)
|
||||||
|
batch_posts, batch_comments = self._parse_posts(data)
|
||||||
|
|
||||||
|
logger.debug(f"Fetched {len(batch_posts)} posts and {len(batch_comments)} comments from search in subreddit {subreddit}")
|
||||||
|
|
||||||
|
if not batch_posts:
|
||||||
|
break
|
||||||
|
|
||||||
|
posts.extend(batch_posts)
|
||||||
|
comments.extend(batch_comments)
|
||||||
|
return posts, comments
|
||||||
|
|
||||||
def get_new_subreddit_posts(self, subreddit: str, limit: int = 10) -> tuple[list[Post], list[Comment]]:
|
def get_new_subreddit_posts(self, subreddit: str, limit: int = 10) -> tuple[list[Post], list[Comment]]:
|
||||||
posts = []
|
posts = []
|
||||||
|
|||||||
Reference in New Issue
Block a user