Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get user posts #55

Closed
8ullyMaguire opened this issue Jul 15, 2023 · 8 comments
Closed

Get user posts #55

8ullyMaguire opened this issue Jul 15, 2023 · 8 comments

Comments

@8ullyMaguire
Copy link

8ullyMaguire commented Jul 15, 2023

I would like to know if there could be an option to get all user posts. Something like this:

posts = lemmy.user(user_id).posts
posts = lemmy.get_user_posts(user_id)

I have been running a bot on Lemmy and keeping track of its posts in a database. However, I recently messed up and had to use an older version of the database, which resulted in fewer items being present in the database. I could get all posts for that user into the database. What I need to get from the post is the URL, Title, Body, and Posted Timestamp

I'm going to change the bot to search for the URL before posting to make sure it doesn't duplicate posts but that creates more load to the instance.

@db0
Copy link
Owner

db0 commented Jul 15, 2023

Isn't this what Lemmy.user.get() is doing?

@8ullyMaguire
Copy link
Author

8ullyMaguire commented Jul 15, 2023

Lemmy.user.get() gives me an error

Error encountered while Request.GET: {"error":"no_id_given"}

@db0
Copy link
Owner

db0 commented Jul 15, 2023

I was just pointing to the method, not giving an exact usage. You need to provide the user id

@8ullyMaguire
Copy link
Author

8ullyMaguire commented Jul 15, 2023

I want to the get posts for the current user, I don't know how to get the user id or use the id to get the posts.

lemmy = Lemmy(LEMMY_INSTANCE_URL)
lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD)
user_id = ?
posts = lemmy.user(user_id).get ?

@db0
Copy link
Owner

db0 commented Jul 15, 2023

Please check examples/user.py

username=db0
user = lemmy.user.get(username=username)

@8ullyMaguire
Copy link
Author

8ullyMaguire commented Jul 15, 2023

This only gets 10 posts, is there a way to paginate and get the rest?

@db0
Copy link
Owner

db0 commented Jul 15, 2023

yes, the get accepts a page number

@8ullyMaguire
Copy link
Author

This only gets 10 posts, is there a way to paginate and get the rest?

posts = lemmy.user.get(username=LEMMY_USERNAME)['posts']
{
    "person_view": {
        "person": {
            "id": 993102,
            "name": "issue_tracking_bot",
            "banned": false,
            "published": "2023-07-13T10:45:29.049925",
            "actor_id": "https://lemm.ee/u/issue_tracking_bot",
            "local": true,
            "deleted": false,
            "admin": false,
            "bot_account": true,
            "instance_id": 1
        },
        "counts": {
            "id": 180052,
            "person_id": 997102,
            "post_count": 2379,
            "post_score": 2380,
            "comment_count": 6545,
            "comment_score": 6547
        }
    },
    "comments": [
    ...
    ],
    "posts": [
    ...
    ]
import json
import sqlite3

from config import *
from pythorhead import Lemmy

# Connect to the database
conn = sqlite3.connect('lemmy_github.db')
cursor = conn.cursor()

def import_missing_posts(posts_list):
    for post in posts_list:
        post = post['post']
        try:
            cursor.execute('SELECT * FROM posts WHERE issue_number = ?', (issue_number(post['url']),))
            result = cursor.fetchone()
            if result is None:
                cursor.execute('INSERT INTO posts (issue_number, lemmy_post_id, issue_title, issue_body) VALUES (?, ?, ?, ?)',
                            (issue_number(post['url']), post['id'], post['name'], post['body']))
                conn.commit()
        except sqlite3.Error as e:
            print(f"SQLite error occurred: {e}")
        except KeyError as e:
            print(f"KeyError occurred: {e}. Check if the input dictionary has all the required keys.")
        except Exception as e:
            print(f"An error occurred: {e}")

def issue_number(url) -> int:
    return int(url.split("/")[-1])

def load_json(filename):
    with open(filename) as f:
        return json.load(f)


def process_posts(lemmy, username):
    page = 1
    while True:
        posts = lemmy.user.get(username=username, page=page)['posts']
        if not posts:
            break
        import_missing_posts(posts)
        page += 1

lemmy = Lemmy(LEMMY_INSTANCE_URL)
lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD)
process_posts(lemmy, LEMMY_USERNAME)

# Close the connection
conn.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants