yt comments includes reply threads. Readme updated.

This commit is contained in:
raisindetre 2024-03-17 20:29:56 +13:00
parent e0d2361aab
commit e6df0f93f0
2 changed files with 29 additions and 19 deletions

View File

@ -4,7 +4,7 @@ These are helper tools to work with Fabric. Examples include things like getting
## yt (YouTube) ## yt (YouTube)
`yt` is a command that uses the YouTube API to pull transcripts, get video duration, and other functions. It's primary function is to get a transcript from a video that can then be stitched (piped) into other Fabric Patterns. `yt` is a command that uses the YouTube API to pull transcripts, pull user comments, get video duration, and other functions. It's primary function is to get a transcript from a video that can then be stitched (piped) into other Fabric Patterns.
```bash ```bash
usage: yt [-h] [--duration] [--transcript] [url] usage: yt [-h] [--duration] [--transcript] [url]
@ -15,9 +15,10 @@ positional arguments:
url YouTube video URL url YouTube video URL
options: options:
-h, --help show this help message and exit -h, --help Show this help message and exit
--duration Output only the duration --duration Output only the duration
--transcript Output only the transcript --transcript Output only the transcript
--comments Output only the user comments
``` ```
## ts (Audio transcriptions) ## ts (Audio transcriptions)
@ -49,7 +50,7 @@ positional arguments:
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
````
## save ## save
`save` is a "tee-like" utility to pipeline saving of content, while keeping the output stream intact. Can optionally generate "frontmatter" for PKM utilities like Obsidian via the `save` is a "tee-like" utility to pipeline saving of content, while keeping the output stream intact. Can optionally generate "frontmatter" for PKM utilities like Obsidian via the

View File

@ -18,36 +18,45 @@ def get_video_id(url):
def get_comments(youtube, video_id): def get_comments(youtube, video_id):
# Fetch comments for the video
comments = [] comments = []
try: try:
response = youtube.commentThreads().list( # Fetch top-level comments
part="snippet", request = youtube.commentThreads().list(
part="snippet,replies",
videoId=video_id, videoId=video_id,
textFormat="plainText", textFormat="plainText",
maxResults=100 # Adjust based on needs maxResults=100 # Adjust based on needs
).execute() )
while response: while request:
response = request.execute()
for item in response['items']: for item in response['items']:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay'] # Top-level comment
comments.append(comment) topLevelComment = item['snippet']['topLevelComment']['snippet']['textDisplay']
comments.append(topLevelComment)
# Check if there are replies in the thread
if 'replies' in item:
for reply in item['replies']['comments']:
replyText = reply['snippet']['textDisplay']
# Add incremental spacing and a dash for replies
comments.append(" - " + replyText)
# Prepare the next page of comments, if available
if 'nextPageToken' in response: if 'nextPageToken' in response:
response = youtube.commentThreads().list( request = youtube.commentThreads().list_next(
part="snippet", previous_request=request, previous_response=response)
videoId=video_id,
textFormat="plainText",
pageToken=response['nextPageToken'],
maxResults=100 # Adjust based on needs
).execute()
else: else:
break request = None
except HttpError as e: except HttpError as e:
print(f"Failed to fetch comments: {e}") print(f"Failed to fetch comments: {e}")
return comments return comments
def main_function(url, options): def main_function(url, options):
# Load environment variables from .env file # Load environment variables from .env file
load_dotenv(os.path.expanduser("~/.config/fabric/.env")) load_dotenv(os.path.expanduser("~/.config/fabric/.env"))