104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
import sqlite3
|
|
from datetime import datetime
|
|
|
|
PREFIX = "smf_"
|
|
GET_BOARDS = """
|
|
SELECT * FROM `{}boards`
|
|
ORDER BY `board_order` ASC
|
|
""".format(PREFIX)
|
|
GET_CATEGORIES = """
|
|
SELECT * FROM `{}categories`
|
|
ORDER BY `cat_order` ASC
|
|
""".format(PREFIX)
|
|
GET_THREADS = """
|
|
SELECT * FROM `{}topics` AS `topics`, `{}messages` AS `messages`
|
|
WHERE `topics`.`id_board`=? AND `messages`.`id_msg`=`topics`.`id_first_msg`
|
|
ORDER BY `id_last_msg` DESC
|
|
LIMIT ? OFFSET ?
|
|
""".format(PREFIX, PREFIX)
|
|
GET_POSTS = """
|
|
SELECT * FROM `{}messages`
|
|
WHERE `id_topic`=?
|
|
ORDER BY `poster_time` ASC
|
|
LIMIT ? OFFSET ?
|
|
""".format(PREFIX)
|
|
|
|
DEFAULT_POSTS_PER_PAGE = 15
|
|
DEFAULT_THREADS_PER_PAGE = 2000
|
|
|
|
def fix_encoding (string):
|
|
return string.encode("latin1", errors="ignore").decode(errors="ignore")
|
|
|
|
class Forum():
|
|
def __init__ (self, db_path):
|
|
self.connection = sqlite3.connect(db_path)
|
|
self.connection.row_factory = sqlite3.Row
|
|
|
|
def get_board_tree (self):
|
|
categories = self.get_categories()
|
|
boards = self.get_boards()
|
|
for category in categories:
|
|
category.children = [child for child in boards if child.category == category.id and child.child_level == 0]
|
|
for board in boards:
|
|
board.children = [child for child in boards if child.parent_board == board.id]
|
|
return categories
|
|
|
|
def get_categories (self):
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_CATEGORIES)
|
|
return [Category(category) for category in cursor.fetchall()]
|
|
|
|
def get_boards (self):
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_BOARDS)
|
|
return [Board(board) for board in cursor.fetchall()]
|
|
|
|
def get_threads_in_board (self, board, page=0, per_page=DEFAULT_THREADS_PER_PAGE):
|
|
try:
|
|
board = board.id
|
|
except ValueError: pass
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_THREADS, (board, per_page, page * per_page))
|
|
return [Thread(thread) for thread in cursor.fetchall()]
|
|
|
|
def get_posts_in_thread (self, thread, page=0, per_page=DEFAULT_POSTS_PER_PAGE):
|
|
try:
|
|
thread = thread.id
|
|
except ValueError: pass
|
|
cursor = self.connection.cursor()
|
|
cursor.execute(GET_POSTS, (thread, per_page, page * per_page))
|
|
return [Post(post) for post in cursor.fetchall()]
|
|
|
|
class Category():
|
|
def __init__ (self, row):
|
|
self.id = row['id_cat']
|
|
self.name = fix_encoding(row['name'])
|
|
self.children = []
|
|
|
|
class Board():
|
|
def __init__ (self, row):
|
|
self.id = row['id_board']
|
|
self.category = row['id_cat']
|
|
self.parent_board = row['id_parent']
|
|
self.child_level = row['child_level']
|
|
self.name = fix_encoding(row['name'])
|
|
self.description = fix_encoding(row['description'])
|
|
self.children = []
|
|
|
|
class Thread():
|
|
def __init__ (self, row):
|
|
self.id = row['id_topic']
|
|
self.parent = row['id_board']
|
|
self.datetime = datetime.fromtimestamp(row['poster_time'])
|
|
self.subject = fix_encoding(row['subject'])
|
|
self.poster_name = fix_encoding(row['poster_name'])
|
|
self.num_replies = row['num_replies']
|
|
|
|
class Post():
|
|
def __init__ (self, row):
|
|
self.id = row['id_msg']
|
|
self.parent = row['id_topic']
|
|
self.datetime = datetime.fromtimestamp(row['poster_time'])
|
|
self.subject = fix_encoding(row['subject'])
|
|
self.body = fix_encoding(row['body'])
|
|
self.poster_name = fix_encoding(row['poster_name']) |