Using Python, How can I create a bot/script that let’s me re-post a subreddit’s post to Lemmy?

Just wanted to do a simple little project to practice my programming skills.

I just need to know the outline/backbone of things I should know to do this project.

  • CaspianXI@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    This should get you started:

    import requests
    
    # Fetch post from Reddit API
    def fetch_reddit_post():
        reddit_url = "https://www.reddit.com/r/{subreddit}/comments/{post_id}.json"
        subreddit = "python"  # Replace with the desired subreddit
        post_id = "abcdef"  # Replace with the desired post ID
    
        url = reddit_url.format(subreddit=subreddit, post_id=post_id)
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
    
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            post_data = response.json()
            return post_data
        else:
            print("Failed to fetch post from Reddit API")
            return None
    
    # Post to Lemmy API
    def post_to_lemmy(post_data):
        lemmy_url = "https://lemmy.ml/api/{version}/post"
        version = "v3"  # Replace with the desired Lemmy API version
    
        url = lemmy_url.format(version=version)
        headers = {"Content-Type": "application/json"}
    
        # Extract necessary information from the Reddit post
        title = post_data[0]["data"]["children"][0]["data"]["title"]
        content = post_data[0]["data"]["children"][0]["data"]["selftext"]
    
        # Create payload for Lemmy API
        payload = {
            "title": title,
            "body": content,
            "community_id": "abcdef",  # Replace with the desired Lemmy community ID
            "auth": "your_auth_token"  # Replace with your Lemmy API authentication token
        }
    
        response = requests.post(url, json=payload, headers=headers)
        if response.status_code == 200:
            print("Post successfully created on Lemmy")
        else:
            print("Failed to post to Lemmy API")
    
    # Fetch post from Reddit API
    post_data = fetch_reddit_post()
    
    if post_data:
        # Post to Lemmy API
        post_to_lemmy(post_data)
    
  • freamon@feddit.nl
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    PRAW is the Python wrapper for Reddit’s API, and searching github for “lemmy api python” revealed some results, so you’d use one API to download a post, and the other to upload it.

    There’s existing bots that do this though, and they all seem to just fill feeds with indiscriminate, un-replied to, spam

  • Falmarri@lemmy.world
    link
    fedilink
    arrow-up
    2
    arrow-down
    2
    ·
    1 year ago

    Honestly this should be very straight forward. If your coming and asking this without doing any sort of work on your own does not bode well for you

    • minnieo@kbin.social
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      maybe is a bit out of their league, but there is absolutely nothing wrong with asking and getting started that way. you don’t magically know what to do, asking can help you gather the steps required.

      there is never anything wrong with asking and learning

    • Poppa_Mo@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      Some people prefer interacting and learning with others, as opposed to struggling through Google searches and piles upon piles of shit YouTube videos.