refactor(connectors): Youtube & Reddit connectors implement BaseConnector
This commit is contained in:
@@ -15,7 +15,15 @@ class RedditAPI(BaseConnector):
|
|||||||
self.source_name = "Reddit"
|
self.source_name = "Reddit"
|
||||||
|
|
||||||
# Public Methods #
|
# Public Methods #
|
||||||
def search_new_subreddit_posts(self, search: str, subreddit: str, limit: int) -> list[Post]:
|
def get_new_posts_by_search(self,
|
||||||
|
search: str,
|
||||||
|
subreddit: str,
|
||||||
|
limit: int
|
||||||
|
) -> list[Post]:
|
||||||
|
|
||||||
|
if not search:
|
||||||
|
return self._get_new_subreddit_posts(subreddit, limit=limit)
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'q': search,
|
'q': search,
|
||||||
'limit': limit,
|
'limit': limit,
|
||||||
@@ -43,7 +51,7 @@ class RedditAPI(BaseConnector):
|
|||||||
|
|
||||||
return posts
|
return posts
|
||||||
|
|
||||||
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"
|
||||||
|
|||||||
@@ -16,33 +16,13 @@ class YouTubeAPI(BaseConnector):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.youtube = build('youtube', 'v3', developerKey=API_KEY)
|
self.youtube = build('youtube', 'v3', developerKey=API_KEY)
|
||||||
|
|
||||||
def search_videos(self, query, limit):
|
def get_new_posts_by_search(self,
|
||||||
request = self.youtube.search().list(
|
search: str,
|
||||||
q=query,
|
category: str,
|
||||||
part='snippet',
|
post_limit: int,
|
||||||
type='video',
|
comment_limit: int
|
||||||
maxResults=limit
|
) -> list[Post]:
|
||||||
)
|
videos = self.search_videos(search, post_limit)
|
||||||
response = request.execute()
|
|
||||||
return response.get('items', [])
|
|
||||||
|
|
||||||
def get_video_comments(self, video_id, limit):
|
|
||||||
request = self.youtube.commentThreads().list(
|
|
||||||
part='snippet',
|
|
||||||
videoId=video_id,
|
|
||||||
maxResults=limit,
|
|
||||||
textFormat='plainText'
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = request.execute()
|
|
||||||
except HttpError as e:
|
|
||||||
print(f"Error fetching comments for video {video_id}: {e}")
|
|
||||||
return []
|
|
||||||
return response.get('items', [])
|
|
||||||
|
|
||||||
def fetch_videos(self, query, video_limit, comment_limit) -> list[Post]:
|
|
||||||
videos = self.search_videos(query, video_limit)
|
|
||||||
posts = []
|
posts = []
|
||||||
|
|
||||||
for video in videos:
|
for video in videos:
|
||||||
@@ -83,3 +63,28 @@ class YouTubeAPI(BaseConnector):
|
|||||||
posts.append(post)
|
posts.append(post)
|
||||||
|
|
||||||
return posts
|
return posts
|
||||||
|
|
||||||
|
def search_videos(self, query, limit):
|
||||||
|
request = self.youtube.search().list(
|
||||||
|
q=query,
|
||||||
|
part='snippet',
|
||||||
|
type='video',
|
||||||
|
maxResults=limit
|
||||||
|
)
|
||||||
|
response = request.execute()
|
||||||
|
return response.get('items', [])
|
||||||
|
|
||||||
|
def get_video_comments(self, video_id, limit):
|
||||||
|
request = self.youtube.commentThreads().list(
|
||||||
|
part='snippet',
|
||||||
|
videoId=video_id,
|
||||||
|
maxResults=limit,
|
||||||
|
textFormat='plainText'
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = request.execute()
|
||||||
|
except HttpError as e:
|
||||||
|
print(f"Error fetching comments for video {video_id}: {e}")
|
||||||
|
return []
|
||||||
|
return response.get('items', [])
|
||||||
|
|||||||
Reference in New Issue
Block a user