Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added optional separator and end arguments to methods of TextFormatter class #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions youtube_transcript_api/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,29 @@ def format_transcripts(self, transcripts, **kwargs):


class TextFormatter(Formatter):
def format_transcript(self, transcript, **kwargs):
def format_transcript(self, transcript, sep="\n", **kwargs):
"""Converts a transcript into plain text with no timestamps.

:param transcript:
:return: all transcript text lines separated by newline breaks.'
:param sep: A separator for each line in the transcript with default set as newline break ('\n').
:type str:
:return: all transcript text lines separated by `sep`.'
:rtype str
"""
return '\n'.join(line['text'] for line in transcript)
return sep.join(line['text'] for line in transcript)

def format_transcripts(self, transcripts, **kwargs):
def format_transcripts(self, transcripts, sep="\n", end="\n\n\n", **kwargs):
"""Converts a list of transcripts into plain text with no timestamps.

:param transcripts:
:return: all transcript text lines separated by newline breaks.'
:param sep: A separator for each line in the transcript with default set as newline break ('\n').
:type str:
:param end: Each individual transcript to be followed by `end` with default set as 3 newline breaks ('\n\n\n').
:type str:
:return: list of all transcript with text lines separated by `sep` and each transcript followed by `end`.
:rtype str
"""
return '\n\n\n'.join([self.format_transcript(transcript, **kwargs) for transcript in transcripts])
return end.join([self.format_transcript(transcript, sep=sep, **kwargs) for transcript in transcripts])

class _TextBasedFormatter(TextFormatter):
def _format_timestamp(self, hours, mins, secs, ms):
Expand Down