PATCH: Broken URL handling

This commit is contained in:
2025-11-06 14:28:41 +00:00
parent 9f5c6a9647
commit 6533bc2f13
2 changed files with 4 additions and 4 deletions

View File

@@ -12,7 +12,7 @@ class RedditConnector(BaseConnector):
'limit': limit, 'limit': limit,
't': timeframe 't': timeframe
} }
url = f"{self.url}top.json" url = f"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,7 +25,7 @@ class RedditConnector(BaseConnector):
'sort': 'relevance', 'sort': 'relevance',
't': 'day' 't': 'day'
} }
url = f"{self.url}search.json" url = f"search"
data = self._fetch_data(url, params) data = self._fetch_data(url, params)
return self._parse_posts(data) return self._parse_posts(data)
@@ -60,7 +60,7 @@ class RedditConnector(BaseConnector):
def _fetch_data(self, endpoint: str, params: dict) -> dict: def _fetch_data(self, endpoint: str, params: dict) -> dict:
url = f"{self.url}{endpoint}" url = f"{self.url}{endpoint}"
try: try:
response = requests.get(url, headers={'User-agent': 'your bot 0.1'}, params=params) response = requests.get(url, headers={'User-agent': 'python:myredditapp:0.1 (by /u/ThisBirchWood)'}, params=params)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
except requests.RequestException as e: except requests.RequestException as e:

View File

@@ -3,7 +3,7 @@ from connectors.reddit_connector import RedditConnector
if __name__ == "__main__": if __name__ == "__main__":
connector = RedditConnector() connector = RedditConnector()
search_results = connector.search_posts(search="NVIDIA", limit=10) search_results = connector.get_top_posts(limit=5, timeframe='week')
for post in search_results: 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}")
print("---") print("---")