egbertbouman / youtube-comment-downloader

Simple script for downloading Youtube comments without using the Youtube API
MIT License
929 stars 228 forks source link

name of output file #69

Open Cyrix126 opened 3 years ago

Cyrix126 commented 3 years ago

Would it be possible to add the video name to the output file. For example with youtube-dl and aria2c -o "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s"

leetfin commented 3 years ago

This would be a great feature, +1 to add.

github-actions[bot] commented 9 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

leetfin commented 9 months ago

I hate stale bot.

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Cyrix126 commented 7 months ago

me too

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Cyrix126 commented 5 months ago

not stale

github-actions[bot] commented 3 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

leetfin commented 3 months ago

not stale. stalebot is lame

biggestsonicfan commented 1 month ago

EDIT: tl;dr: use yt-dlp --write-comments --write-info-json -o "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s" https://www.youtube.com/watch?v=dQw4w9WgXcQ

Would it be possible to add the video name to the output file. For example with youtube-dl and aria2c -o "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s"

The video's actual data is grabbed in downloader.py, then is no longer needed. This data is not passed back to __init__.py, which writes the file to disk.

You would need to map each variable possible to it's equivalent Youtube data within downloader.py and pass that back to __init__.py to achieve what you are looking for. Also, %(ext)s would always be in the json format considering the output this application achieves.

The output argument supports file pathing, so with just a little bit of homework and actually plugging the video url in a web browser

youtube-comment-downloader -u https://www.youtube.com/watch?v=dQw4w9WgXcQ -o dQw4w9WgXcQ.json

can be turned into

youtube-comment-downloader -u https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "RickAstleyYT/2009-10-24 - Rick Astley - Never Gonna Give You Up (Official Music Video).json"

in mere seconds.

If you are using youtube-comment-downloader as a python library, this should be done within your own script with relative ease.

EDIT: Actually, it was very easy with minimal copying and pasting from the source:

import sys
import yt_dlp
from youtube_comment_downloader import *

url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
ydl_opts = {
    'dumpjson': True
}
info_dict = None
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(url, download=False)

comments = YoutubeCommentDownloader()
generator = (
    comments.get_comments_from_url(url, sort_by=SORT_BY_RECENT)
)

uploader = info_dict['uploader']
upload_date = info_dict['upload_date']
title = info_dict['title']

if not os.path.exists(uploader):
    os.makedirs(uploader)

    count = 1
    limit = None
    pretty = None
    with io.open(os.path.join(uploader, f"{upload_date} - {title}.json"), 'w', encoding='utf8') as fp:
        sys.stdout.write("Downloaded %d comment(s)\r" % count)
        sys.stdout.flush()
        start_time = time.time()

        if pretty:
            fp.write('{\n' + ' ' * INDENT + '"comments": [\n')

        comment = next(generator, None)
        while comment:
            comment_str = to_json(comment, indent=INDENT if pretty else None)
            comment = None if limit and count >= limit else next(generator, None)
            comment_str = comment_str + ',' if pretty and comment is not None else comment_str
            print(comment_str.decode('utf-8') if isinstance(comment_str, bytes) else comment_str, file=fp)
            sys.stdout.write("Downloaded %d comment(s)\r" % count)
            sys.stdout.flush()
            count += 1

This is merely an example, as it does not handle proper sanitation of paths and filenames, so there could be illegal characters in those variables depending on your platform.