def _get_comments(client: Resource, **kwargs) -> List[str]: """Function to obtain comments of the video that has video_id in the order of relevance. ptoken marks where to find the next page of comments. Function returns a list of comment text. """ comments = [] for _ in range(0, MAX_NUMBER_COMMENTS, NUM_RESULTS_PER_REQUEST): try: response = client.commentThreads().list( **kwargs ).execute() except HttpError as e: print(e) return comments if response: for item in response["items"]: comment = item["snippet"]["topLevelComment"] text = comment["snippet"]["textDisplay"] comments.append(text) else: raise datatypes.DataFetchingError(kwargs["videoId"]) # TODO: catch if "nextPageToken" in response: kwargs["pageToken"] = response.get("nextPageToken") else: break return comments
def add_comment(youtube: d.Resource, youtube_id: str, comment_text: str): request = youtube.commentThreads().insert( part='snippet', body={ "snippet": { "topLevelComment": { "snippet": { "textOriginal": comment_text } }, "videoId": youtube_id } } ) response = request.execute() return response['id']
def get_video_comments(youtube: d.Resource, video_id: int, export=False): youtube_id = v.get_youtube_id(video_id=video_id) request = youtube.commentThreads().list( part="snippet", videoId=youtube_id ) response = request.execute() channel_id = channel.get_id(youtube=youtube) if export: with open(f'output/comment_{youtube_id}.json', 'w') as outfile: json.dump(response, outfile) for comment in response['items']: top_comment = comment['snippet']['topLevelComment'] author_id = top_comment['snippet']['authorChannelId']['value'] if author_id == channel_id: return comment return response