• 9 Posts
  • 13 Comments
Joined 1 year ago
cake
Cake day: July 13th, 2023

help-circle












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