Giter Site home page Giter Site logo

Can't reply replies about scratchattach HOT 4 CLOSED

banddans avatar banddans commented on August 18, 2024
Can't reply replies

from scratchattach.

Comments (4)

TheCommCraft avatar TheCommCraft commented on August 18, 2024

from scratchattach.

banddans avatar banddans commented on August 18, 2024

This still don't work (if you check the comment and wonder why it says "reply from python :)" it's just because i replied the root comment as a firts test)

user.reply_comment("reply from python!",
                   parent_id="340385010", commentee_id="139742995")

from scratchattach.

banddans avatar banddans commented on August 18, 2024

After testing random stuff i managed to make the reply appear in my messages but nowhere else. Now my code looks like this:

from hugchat import hugchat
from hugchat.login import Login
import scratchattach
import requests
import json

PROMPT = "I want you to act as a scratcher, whenever anyone comments on your profile i will write something like username: comment-content and then you are supposed to write a reply to the comment. Do you understand?"

# Log in to huggingface and grant authorization to huggingchat
EMAIL = ""
PASSWD = ""
# NOTE: trailing slash (/) is required to avoid errors
cookie_path_dir = "./cookies/"
sign = Login(EMAIL, PASSWD)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

chatbot = hugchat.ChatBot(cookies=cookies.get_dict())


def chat(input: str):
    message_result = chatbot.chat(input)
    message_str: str = message_result.wait_until_done()
    return message_str


chat(PROMPT)

session = scratchattach.login("banddans-chatbot", "")
user = session.get_linked_user()
last_messages = []
with open("seen_messages.json", "r") as seen_messages_file:
    last_messages = json.load(seen_messages_file)
    seen_messages_file.close()

while True:
    messages = session.messages()
    for message in messages:
        if message not in last_messages:
            print(message)
            if message["type"] == "addcomment" and message not in last_messages:
                chat_input: str = message["actor_username"] + \
                    ": " + message["comment_fragment"]
                to_reply: str = chat(chat_input).split(": ")[-1]
                print(to_reply)

                print(user.reply_comment(
                    to_reply, parent_id=message["comment_id"], commentee_id=message["actor_id"]))

    last_messages = messages
    with open("seen_messages.json", "w") as seen_messages_file:
        json.dump(last_messages, seen_messages_file)
        seen_messages_file.close()

from scratchattach.

banddans avatar banddans commented on August 18, 2024

Finally found that comment_id wasn't the same as parent_id. I made this function if anyone ever gets the same issue:

from bs4 import BeautifulSoup
import requests

def find_parent_id(comment_id):
    try:
        i = 0
        while True:
            data = requests.get(
                "https://scratch.mit.edu/site-api/comments/user/banddans-chatbot/?page=" + str(i)).text
            soup = BeautifulSoup(data, features="html.parser")
            comment = soup.find(id="comments-" + str(comment_id))
            if comment != None:
                return comment.parent.parent.parent.find(class_="comment")["data-comment-id"]
                break
            i += 1
    except AttributeError:
        return comment_id

from scratchattach.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.