ADD: Subreddit search
This commit is contained in:
@@ -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']
|
||||
|
||||
|
||||
13
dto/post.py
13
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
|
||||
self.upvotes = None
|
||||
self.comments = None
|
||||
5
main.py
5
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("---")
|
||||
Reference in New Issue
Block a user