I finally (partially) fixed my comment scoring Python script! Yippee!

in Pythonlast year (edited)

Yesterday, when I wrote that post, I wasn't sure whether I'll ever be able to solve the problems the script had. But with the help of ChatGPT I was able to discuss and solve some of them. (Yes, I managed to work with it long enough without it being clogged up by the billions of lonely people seeking for someone to talk to.)

I'm particularly proud that I managed to solve the last (and the most difficult) problem, that was a missing 'comment' key almost by myself. There wasn't such a key, and I needed to figure out how to run the for-loop without it.

So this time it wasn't LightHive that caused me problems. This time it was my own code.

I also learned so many new things I'm going to promptly forget by Tomorrow.


AI provided me with this image.

Anyhow. I'm already quite tired, as it's 2:50am, and I've been at this for the whole day and night). I'm just going to post the working piece of code for all of you to marvel at, and go to bed.

I am going to create a GitLab repository for my Hive projects, as I've got some ideas. But that'll have to wait until I'm well rested again.

Here's the code:

# Import needed libraries

import datetime
import string
from lighthive.client import Client
from lighthive.helpers.event_listener import EventListener

# Establish connection to Hive

client = Client()
account = client.account("gamer00")

# Ask the user for a post url or path (with the preceding http server address):

while True:
    post_urls = input("Enter post urls or paths, separated by a comma: ").split(",")
    post_urls = [url.strip() for url in post_urls]
    if all(url.startswith("https://") for url in post_urls) and post_urls:
        break
    print("Invalid input. Please enter valid post urls starting with https:// and separated by a comma.")

# Function to split the url into parts, and extract (return) 'author' and 'permlink' from it:

def extract_author_permlink(url):
    parts = url.split("/")
    author = parts[-2][1:]
    permlink = parts[-1]
    return author, permlink

# Create a set of excluded accounts instead of a list, as a set needs no checking for doubles:

exclusionlist = set()
try:
    with open("exclusion.list", "r") as f:
        exclusionlist.update(f.read().splitlines())
except FileNotFoundError:
    pass

# Ask user for excluded authors

excluded_authors = input("Enter the excluded authors (comma-separated): ")
if excluded_authors:
    excluded_authors = excluded_authors.split(",")
    for author in excluded_authors:
        author = author.strip()
        exclusionlist.add(author)
    with open("exclusion.list", "w") as f:
        f.write("\n".join(exclusionlist))

# Loop through each post 'permlink' by 'author' and get the replies to it:

for post_url in post_urls:
    # Get the post data
    author, permlink = extract_author_permlink(post_url)
    post_data = client.get_content_replies(author, permlink)

    # Initialize author comment count dictionary:
    author_comment_count = {}

    # Loop through the comments of the post and calculate a score by 1 point per comment for each author:

    for comment in post_data:
        author = comment['author']
        if author in exclusionlist:
            continue
        if author in author_comment_count:
            author_comment_count[author] += 1
        else:
            author_comment_count[author] = 1

    # Print the scores

    print("Commenter scores:")
    for author, score in author_comment_count.items():
        print(f"{author}: {score}")

Now, when I run it:

 python Comment_scoring-0.1.py
Enter post urls or paths, separated by a comma: https://ecency.com/quiz/@gamer00/which-u-s-president-quiz-a56a71e5e7dac
Enter the excluded authors (comma-separated): gamer00,wine.bot,pizzabot,tipu
Commenter scores:
thedoc07: 1
kaminchan: 1
ninahaskin: 2

I hope you like it. Yes, of course it still needs some work, it's version 0.1 for now, and I've not even done all of the needed error handling yet, and the output frankly looks like it needs prettifying.

Oh, and it still miscounts the comments. That too...

See ya Tomorrow then...

Sort:  

Great work! I'm just curious if our technological advancements will go parabolic with the AI assist to complete work more efficiently

It might happen, but as for now, the assistant brings just about as many errors into the code as it helps to fix. But hey, it's a fun way to learn (sometimes). 😅

I did some initial testing with it to see if it could debug code I purposely broke.

It was interesting..... we will see how well the next iterations of it can do.

Wow! Great! Your codes look like very complicated recipe for cooking a strange dish for alien feast! 🙀😂

Keep up the good work. Python language is my favorite language

I like it too, but the Hive support is lacking. All the API libraries are more than one year old, and the documentation lists dozens of different calls for listing comments, but none of them quite suitable for my use.

It's difficult to learn things when nothing works. 😭

True, is very different to learn things when nothing work. But keep up the good work

$WINE


Congratulations, @theguruasia You Successfully Shared 0.300 WINEX With @gamer00.
You Earned 0.300 WINEX As Curation Reward.
You Utilized 3/4 Successful Calls.

wine_logo


Contact Us : WINEX Token Discord Channel
WINEX Current Market Price : 0.213


Swap Your Hive <=> Swap.Hive With Industry Lowest Fee (0.1%) : Click This Link
Read Latest Updates Or Contact Us