30
submitted 1 year ago* (last edited 1 year ago) by GodOfThunder@lemm.ee to c/python@programming.dev

I've written a script to repost Github posts to Lemmy but now I'm trying to repost comments too and I don't know how to get the corresponding post_id that I need to do that.

lemmy.comment.create(post_id, comment_body)

Is there a way to associate the Github URL to the post_id when creating it with Pythorhead and then figuring out the Github URL from the comment and getting the post_id that way?

Alternatively, would something like this work? If so how would I limit the search to a specific community?

import requests

# GitHub URL of the post
github_url = "https://github.com/user/repo/issues/123"

# Lemmy API endpoint to search for posts
lemmy_search_url = "https://lemmy.ml/api/v3/search"

# Query parameters for the search endpoint
params = {
    "q": github_url,
    "type": "link",
    "sort": "relevance",
    "limit": 1
}

# Send a GET request to the search endpoint
response = requests.get(lemmy_search_url, params=params)

# Parse the response JSON to get the post_id
post_id = response.json()["posts"][0]["id"]

# Use the post_id to create a comment in Lemmy
lemmy.comment.create(post_id, comment_body)

Are there any other ways?

you are viewing a single comment's thread
view the rest of the comments
[-] jnovinger@programming.dev 1 points 1 year ago

OP, you could even use a local file/sqlite database in the repo and just update and commit it when the script runs.

Simon Willison has a cool approach for this that runs in GitHub Actions and keeps the versioned state in git itself: https://simonwillison.net/2020/Oct/9/git-scraping/

this post was submitted on 13 Jul 2023
30 points (100.0% liked)

Python

6284 readers
65 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS