pawamoy / pawamoy.github.io

http://pawamoy.github.io/
2 stars 0 forks source link

posts/migrate-disqus-comments-to-utterances-github/ #24

Closed utterances-bot closed 2 years ago

utterances-bot commented 3 years ago

Migrate Disqus comments to Utterances (in GitHub issues) with Python - pawamoy's website

Findings, thoughts, tutorials, work. Pieces of my mind!

https://pawamoy.github.io/posts/migrate-disqus-comments-to-utterances-github/

markerikson commented 3 years ago

Hey, thanks for the highly detailed post! I just switched my comments at https://blog.isquaredsoftware.com over to Utterances, and used 90% of your code to do the conversion work.

Biggest difference was that the pygithub3 package you linked didn't want to install right, so I ended up using https://pypi.org/project/github3.py/ instead.

Also, I ran into a rate-limiting issue, so I had to update the script to check for dupe comments and slow down comment creation a bit:

    for i, post in enumerate(posts, 1):
        name = post["author"]["name"]
        user = post["author"].get("username")
        mention = " @" + user if user and not user.startswith("disqus_") else ""
        date = post["createdAt"]
        message = post["message"]
        issue = post["thread"]["issue"]
        comments = post["thread"]["comments"]
        body = f"*Original date: {date}*\n\n{message}"
        # don't add original author when it's you
        if user != USERNAME:
            body = f"*Original author:* **{name}{mention}**  \n{body}" 

        try:
          for comment in comments:
            if date in comment.body and name in comment.body:
              raise Error("dupe found")
        except:
          print(f"Skipping comment {i}/{len(posts)}")
          continue
        print(f"Posting {i}/{len(posts)} to issue {issue.number}    \r", end="")
        issue.create_comment(body)
        time.sleep(0.5)

Thanks! Could have figured it out for myself if I'd spent enough time on it, but sure was nice to have an almost ready-made solution to paste in.

pawamoy commented 3 years ago

Great! Glad to hear my script helped you :smile:

I just checked and you're right, I linked the wrong PyGitHub :scream: It should have been https://pypi.org/project/PyGithub/. I'll fix this in the post, thanks!

I will also add a comment about rate limiting, and using a sleep to prevent it, thanks again!

idarek commented 3 years ago

Hello @pawamoy I will be trying to move my comments using your script above. My question before I will start, as would like to use utterances for issues using pathname as a page base instead url and definitely not title.

Assume that your import script is based on pathname? Or am I wrong and it doesn't matter in that stage?

From utterances:

When Utterances loads, the GitHub issue search API is used to find the issue associated with the page based on url, pathname or title.

Thanks in advance.

pawamoy commented 3 years ago

So by default Utterances uses pathname, yes. Note that you have to setup Utterances first on your repo, then serve your blog locally, and create the initial issues by posting "test" comments in each post. Each time your post the first "test" comment, Utterances will create an issue using pathname.

Now my script is also using pathname I think, see this line:

if issue.title == thread["link"].replace(BASE_URL, ""):

We search for issue titles being the URL of the post minus the specified base URL, which results in what we can call the pathname, right?

idarek commented 3 years ago

Wonder if you can help.

I have created "test" in posts that have comments and when run script ending with following error

Traceback (most recent call last):
  File "disqus.py", line 74, in <module>
    disqus_to_github()
  File "disqus.py", line 52, in disqus_to_github
    issue = post["thread"]["issue"]
KeyError: 'issue'

Is that a thread will missing initiated issue by "test" or something else?

idarek commented 3 years ago

Yep, I sorted my own issue. Disqus export in path have / on the end of the link where new environment by default is ignoring it.

Replaced this using Atom and import is in progress :)

Thank you for your script.

ps. when running this script on macOS I need to add additionally import os to main script.

pawamoy commented 3 years ago

Hey @idarek, glad you found the solution! Also, indeed, import os is missing from the script, nice catch, I'll fix it, thanks!