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)

Rewrite the script following clean code principles (https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29) where each function does only one thing

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

To rewrite the script following clean code principles, we can break down the script into smaller functions, each with a single responsibility. Here's the updated script:

import requests
from pythorhead import Lemmy

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"
]

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)
        lemmy.post.create(community_id, issue_title, url=issue_url, body=issue_body)

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)

if __name__ == "__main__":
    main()

This updated script breaks down the original script into smaller functions, each with a single responsibility. The fetch_github_issues, initialize_lemmy_instance, discover_community, extract_issue_info, and post_issues_to_lemmy functions handle different aspects of the task, making the code easier to read and maintain.

Citations: [1] https://testdriven.io/blog/clean-code-python/ [2] https://workat.tech/machine-coding/tutorial/introduction-clean-code-software-design-principles-nwu4qqc63e09 [3] https://dev.to/alexomeyer/10-must-know-patterns-for-writing-clean-code-with-python-56bf [4] https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29 [5] https://github.com/zedr/clean-code-python [6] https://www.ionos.com/digitalguide/websites/web-development/clean-code-principles-advantages-and-examples/ [7] https://towardsdatascience.com/python-clean-code-6-best-practices-to-make-your-python-functions-more-readable-7ea4c6171d60 [8] https://www.pluralsight.com/blog/software-development/10-steps-to-clean-code [9] https://www.geeksforgeeks.org/best-practices-to-write-clean-python-code/ [10] https://x-team.com/blog/principles-clean-code/ [11] https://jerrynsh.com/how-to-write-clean-code-in-python/ [12] https://betterprogramming.pub/12-conventions-for-writing-clean-code-e16c51e3939a [13] https://youtube.com/watch?v=F14BKKHeAKU [14] https://medium.com/gojekengineering/the-basics-of-writing-clean-code-39ed770b626d [15] https://www.octobot.io/blog/clean-python-code/ [16] https://www.freecodecamp.org/news/how-to-write-clean-code/

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

Make it keep working and checking for new posts every couple hours. Don't create duplicate posts.

[–] [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/