From a9778f26b2e026f338aa549b7922a4f670d5f319 Mon Sep 17 00:00:00 2001 From: DylanDeFaoite Date: Thu, 6 Nov 2025 14:41:46 +0000 Subject: [PATCH] ADD: Subreddit search --- connectors/reddit_connector.py | 28 ++++++++++++++++++++++++---- dto/post.py | 13 ++++++++++--- main.py | 5 +++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/connectors/reddit_connector.py b/connectors/reddit_connector.py index 5d3bab2..42806ae 100644 --- a/connectors/reddit_connector.py +++ b/connectors/reddit_connector.py @@ -6,6 +6,7 @@ import requests class RedditConnector(BaseConnector): def __init__(self): self.url = "https://www.reddit.com/" + self.source_name = "Reddit" def get_top_posts(self, limit: int = 10, timeframe: str = 'day') -> list[Post]: params = { @@ -16,16 +17,22 @@ class RedditConnector(BaseConnector): data = self._fetch_data(url, params) return self._parse_posts(data) - def search_posts(self, search: str, limit: int = 10, before = None, after = None) -> list[Post]: + def search_posts(self, + search: str, + limit: int = 10, + before = None, + after = None, + timeframe = 'day' + ) -> list[Post]: params = { 'q': search, 'limit': limit, 'before': before, 'after': after, 'sort': 'relevance', - 't': 'day' + 't': timeframe } - url = f"search" + url = f"search.json" data = self._fetch_data(url, params) return self._parse_posts(data) @@ -33,6 +40,18 @@ class RedditConnector(BaseConnector): data = self._fetch_data(f"user/{username}/about.json", {}) return self._parse_user(data) + def search_subreddit(self, search: str, subreddit: str, limit: int = 10, timeframe: str = "day") -> list[Post]: + params = { + 'q': search, + 'limit': limit, + 'restrict_sr': 'on', + 'sort': 'top', + "t": timeframe + } + url = f"r/{subreddit}/search.json" + data = self._fetch_data(url, params) + return self._parse_posts(data) + def _parse_posts(self, data) -> list[Post]: posts = [] for item in data['data']['children']: @@ -42,7 +61,8 @@ class RedditConnector(BaseConnector): title=post_data['title'], content=post_data.get('selftext', ''), url=post_data['url'], - timestamp=post_data['created_utc']) + timestamp=post_data['created_utc'], + source=self.source_name) post.subreddit = post_data['subreddit'] post.upvotes = post_data['ups'] diff --git a/dto/post.py b/dto/post.py index 0e1eeda..c9c5f91 100644 --- a/dto/post.py +++ b/dto/post.py @@ -1,11 +1,18 @@ class Post: - def __init__(self, author, title, content, url, timestamp): + def __init__(self, author: str, + title: str, + content: str, + url: str, + timestamp: float, + source: str): self.author = author self.title = title self.content = content self.url = url self.timestamp = timestamp - + self.source = source + # Optionals self.subreddit = None - self.upvotes = None \ No newline at end of file + self.upvotes = None + self.comments = None \ No newline at end of file diff --git a/main.py b/main.py index 884948c..c487051 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ from connectors.reddit_connector import RedditConnector +from datetime import datetime if __name__ == "__main__": connector = RedditConnector() - search_results = connector.get_top_posts(limit=5, timeframe='week') + search_results = connector.search_subreddit("China", "all", limit=5, timeframe="month") for post in search_results: - print(f"Title: {post.title}\nAuthor: {post.author}\nSubreddit: {post.subreddit}\nUpvotes: {post.upvotes}") + print(f"Title: {post.title}\nAuthor: {post.author}\nSubreddit: {post.subreddit}\nUpvotes: {post.upvotes}\nSource: {post.source}\nTimestamp: {datetime.fromtimestamp(post.timestamp)}") print("---") \ No newline at end of file