db0 / pythorhead

A python library for interacting with Lemmy
GNU Affero General Public License v3.0
77 stars 25 forks source link

Get user posts #55

Closed 8ullyMaguire closed 1 year ago

8ullyMaguire commented 1 year ago

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 commented 1 year ago

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

8ullyMaguire commented 1 year ago

Lemmy.user.get() gives me an error

Error encountered while Request.GET: {"error":"no_id_given"}
db0 commented 1 year ago

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

8ullyMaguire commented 1 year ago

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 commented 1 year ago

Please check examples/user.py

username=db0
user = lemmy.user.get(username=username)
8ullyMaguire commented 1 year ago

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

db0 commented 1 year ago

yes, the get accepts a page number

8ullyMaguire commented 1 year ago

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()