this post was submitted on 13 Jul 2023
1 points (66.7% liked)

ShareGPT

1 readers
1 users here now

To share AI output.

founded 1 year ago
MODERATORS
 

Use pythorhead (https://github.com/db0/pythorhead) to write a python script to write every github issue from https://github.com/LemmyNet/lemmy and https://github.com/LemmyNet/lemmy-ui to https://lemm.ee/c/issue_tracker

Follow this template for the posts

URL: https://github.com/LemmyNet/lemmy-ui/issues/number

Title: [UI][Feature Request] Title

Body: Use CommonMark markdown to quote the body and add a line saying:

Originally posted by user in #1930

Edit: the fixed script is here

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago (1 children)

To modify the script to keep checking for new issues every couple of hours and avoid creating duplicate posts, you can use the schedule library[1] to run the script periodically and maintain a record of posted issues to prevent duplicates.

First, install the schedule library:

pip install schedule

Then, update the script as follows:

import requests
from pythorhead import Lemmy
import schedule
import time

GITHUB_API_BASE = "https://api.github.com"
LEMMY_URL = "https://lemm.ee"
LEMMY_USERNAME = "your_username"
LEMMY_PASSWORD = "your_password"
REPOSITORIES = [
    "LemmyNet/lemmy",
    "LemmyNet/lemmy-ui"
]

posted_issues = set()

def fetch_github_issues(repo):
    url = f"{GITHUB_API_BASE}/repos/{repo}/issues"
    headers = {"Accept": "application/vnd.github+json"}
    response = requests.get(url, headers=headers)
    return response.json()

def initialize_lemmy_instance():
    lemmy = Lemmy(LEMMY_URL)
    lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD)
    return lemmy

def discover_community(lemmy, community_name):
    return lemmy.discover_community(community_name)

def extract_issue_info(issue, repo):
    issue_url = issue["html_url"]
    issue_title = f"[{repo.split('/')[1]}][{issue['title']}] #{issue['number']}"
    issue_body = issue["body"]
    return issue_url, issue_title, issue_body

def post_issues_to_lemmy(lemmy, community_id, repo):
    issues = fetch_github_issues(repo)
    for issue in issues:
        issue_url, issue_title, issue_body = extract_issue_info(issue, repo)
        if issue_url not in posted_issues:
            lemmy.post.create(community_id, issue_title, url=issue_url, body=issue_body)
            posted_issues.add(issue_url)

def main():
    lemmy = initialize_lemmy_instance()
    community_id = discover_community(lemmy, "issue_tracker")
    for repo in REPOSITORIES:
        post_issues_to_lemmy(lemmy, community_id, repo)

def run_periodically():
    schedule.every(2).hours.do(main)

    while True:
        schedule.run_pending()
        time.sleep(60)

if __name__ == "__main__":
    run_periodically()

This updated script uses the schedule library to run the main() function every 2 hours. It also maintains a posted_issues set to keep track of the posted issues and prevent duplicates. When posting issues, the script checks if the issue URL is not in the posted_issues set before posting it to Lemmy and adding it to the set.

Citations: [1] https://www.geeksforgeeks.org/python-schedule-library/ [2] https://www.trainingint.com/how-to-find-duplicates-in-a-python-list.html [3] https://schedule.readthedocs.io [4] https://stackoverflow.com/questions/1541797/how-do-i-check-if-there-are-duplicates-in-a-flat-list [5] https://medium.com/@timothymugayi/7-ways-to-execute-scheduled-jobs-with-python-47d481d22b91 [6] https://note.nkmk.me/en/python-list-duplicate-check/ [7] https://plainenglish.io/blog/5-ways-to-schedule-jobs-in-python-99de8a80f28e [8] https://www.educative.io/answers/how-to-find-duplicates-from-a-list-in-python [9] https://youtube.com/watch?v=yDPQfj4bZY8 [10] https://thispointer.com/python-3-ways-to-check-if-there-are-duplicates-in-a-list/ [11] https://youtube.com/watch?v=FCPBG6NqMmQ [12] https://blog.finxter.com/5-best-ways-to-check-a-list-for-duplicates-in-python/ [13] https://docs.python.org/3/library/sched.html [14] https://www.pythonforbeginners.com/basics/check-if-a-list-has-duplicate-elements [15] https://www.airplane.dev/blog/using-python-scheduler-to-schedule-asynchronous-tasks [16] https://datagy.io/python-list-duplicates/ [17] https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/

[–] [email protected] 1 points 1 year ago (1 children)

posted issues should be saved to memory so that the program remembers them when restarted

[–] [email protected] 1 points 1 year ago

To save the posted issues to memory so that the program remembers them when restarted, you can use a file to store the posted issue URLs. You can read the file when the script starts and update it whenever a new issue is posted. Here's the updated script:

import requests
from pythorhead import Lemmy
import schedule
import time

GITHUB_API_BASE = "https://api.github.com"
LEMMY_URL = "https://lemm.ee"
LEMMY_USERNAME = "your_username"
LEMMY_PASSWORD = "your_password"
REPOSITORIES = [
    "LemmyNet/lemmy",
    "LemmyNet/lemmy-ui"
]

POSTED_ISSUES_FILE = "posted_issues.txt"

def load_posted_issues():
    try:
        with open(POSTED_ISSUES_FILE, 'r') as file:
            return set(line.strip() for line in file)
    except FileNotFoundError:
        return set()

def save_posted_issue(issue_url):
    with open(POSTED_ISSUES_FILE, 'a') as file:
        file.write(issue_url + '\n')

posted_issues = load_posted_issues()

# ... (rest of the functions remain the same)

def post_issues_to_lemmy(lemmy, community_id, repo):
    issues = fetch_github_issues(repo)
    for issue in issues:
        issue_url, issue_title, issue_body = extract_issue_info(issue, repo)
        if issue_url not in posted_issues:
            lemmy.post.create(community_id, issue_title, url=issue_url, body=issue_body)
            posted_issues.add(issue_url)
            save_posted_issue(issue_url)

# ... (rest of the script remains the same)

This updated script uses the load_posted_issues function to read the posted issue URLs from the POSTED_ISSUES_FILE when the script starts. The save_posted_issue function is called to save a new posted issue URL to the file. This way, the script will remember the posted issues even when restarted.

Citations: [1] https://stackoverflow.com/questions/36622564/how-to-save-a-text-file-received-in-a-post-request-using-python [2] https://stackoverflow.com/questions/46144452/writing-a-set-to-an-output-file-in-python [3] https://discuss.python.org/t/problem-saving-values-to-a-text-file/12350 [4] https://note.nkmk.me/en/python-file-io-open-with/ [5] https://www.geeksforgeeks.org/saving-text-json-and-csv-to-a-file-in-python/ [6] https://www.geeksforgeeks.org/writing-to-file-in-python/ [7] https://docs.djangoproject.com/en/4.2/topics/http/file-uploads/ [8] https://www.geeksforgeeks.org/reading-writing-text-files-python/ [9] https://pynative.com/python-write-list-to-file/ [10] https://stackabuse.com/reading-and-writing-lists-to-a-file-in-python/ [11] https://www.scrapingbee.com/blog/download-image-python/ [12] https://www.freecodecamp.org/news/file-handling-in-python/ [13] https://www.codementor.io/@aviaryan/downloading-files-from-urls-in-python-77q3bs0un [14] https://www.pythonmorsels.com/creating-and-writing-file-python/ [15] https://realpython.com/storing-images-in-python/