alexlaverty / python-reddit-youtube-bot

Automated Reddit Youtube Video Bot
https://www.youtube.com/channel/UCzIwW92D_rM5_yvWBsquSbw
75 stars 17 forks source link

Screenshot from template #110

Closed azrafe7 closed 6 months ago

azrafe7 commented 6 months ago

This PR makes possible to create screenshots of reddit comments by filling a template file. No scraping is involved, all straight from PRAW and the Reddit API.

Still working on it (don't merge it yet, please, as I plan to do more tests and add more templates... but please do test it!). For now it supports an "example" template, and an "old_reddit" template,

Can set the template_url in settings.py.

Let me know what you think of it. 😉

Cheers, azrafe7

azrafe7 commented 6 months ago

Ooohh... and credits to SnipCSS 😄 that made the process of mocking the comment html+css way easier!

sebbourgeois commented 6 months ago

I'll give it a try this weekend and give you my feedbacks !

azrafe7 commented 6 months ago

More work on the Dark Reddit Template, please test it. 😉

Blank template: image

Filled template: image

alexlaverty commented 6 months ago

When I pull your branch and run it looks like it's failing to pull the template from your account :

template_url = "https://azrafe7.github.io/python-reddit-youtube-bot.git/comment_templates/old_reddit_mockup/index.html"

image

image

i'm having a play with the code to see if i can get it to read comment template from local path

alexlaverty commented 6 months ago

I've branched off your fork and updated it to read the comment template from a local file instead of remote URL, this is working for me : https://github.com/alexlaverty/python-reddit-youtube-bot/pull/112/files

basically main changes I made were :

        # Get the thread screenshot
        page = context.new_page()

        if settings.use_template:
            template_url = str(Path("comment_templates", settings.template_url))
            # Read the Jinja template from a file
            print(f"Using Comment Template : {template_url}")

            # Create a Jinja environment with UTF-8 encoding
            env = Environment(loader=FileSystemLoader(template_url))
            # Load the template
            template = env.get_template('index.html')
            for _idx, comment in enumerate(
                accepted_comments if settings.screenshot_debug else track(accepted_comments, "Downloading screenshots...")
            ):
                comment_path: Path = Path(f"{video_directory}/comment_{comment.id}.png")

                if comment_path.exists():
                    print(f"Comment Screenshot already downloaded : {comment_path}")
                else:
                    comment_excerpt = get_comment_excerpt(comment)
                    print(f"[{_idx + 1}/{len(accepted_comments)} {comment.id}] {comment.author}: {comment_excerpt}")

                    # Fill template fields and update page
                    values = {
                        'author': comment.author.name if comment.author else '[unknown]',
                        'id': comment.id,
                        'score': number_to_abbreviated_string(comment.score),
                        'avatar': comment.author.icon_img if comment.author else '[unknown]',
                        'date': datetime_to_human_timedelta(dt.datetime.fromtimestamp(comment.created)),
                        'body_text': comment.body,
                        'body_html': comment.body_html,
                    }
                    # Render the template with variables
                    output = template.render(values)

                    # # Save the rendered output to a file
                    # print("Jinja Comment Output :")
                    # print(output)
                    # with open(f"{video_directory}/comment_{comment.id}.html", "w", encoding="utf-8") as output_file:
                    #     output_file.write(output)

                    # Option 1: Pass HTML content
                    page.set_content(output)

                    page.locator('#comment-container').screenshot(path=str(comment_path.resolve()))
                    #browser.close()

I'm also having a look at the PR check trying to figure out why it's failing, rebuilding the docker container fresh

alexlaverty commented 6 months ago

Also a mate sent me this today, which seems very interesting... doesn't look like they've released any source code though : https://humanaigc.github.io/emote-portrait-alive/

alexlaverty commented 6 months ago

Have merged in the PR and rebuilt and updated the docker container : alexlaverty/ttsvibelounge:1.0.8

videos have run via github action workflow ahd are uploading videos again : https://www.youtube.com/watch?v=-a5tkxVhkgc

thanks for all the work you've put in @azrafe7, much appreciated 🎉👏

alexlaverty commented 6 months ago

so looking at the build times, based off the first successful build seems to have halved the amount of time to complete : image

old screenscraping way : image