This repository has been archived on 2023-06-06. You can view files and clone it, but cannot push or open issues or pull requests.
reeere/reeere/board.py

90 lines
2.6 KiB
Python

import json
import time
import requests
from reeere.ai import clean_reply
def get_sub_forums():
while True:
time.sleep(2)
response = requests.get("http://reee.re/api/boards/")
if response.status_code == 429:
time.sleep(5)
continue
return json.loads(response.text)
def get_threads(sub_forum_link):
while True:
time.sleep(2)
response = requests.get(f"http://reee.re/api/{sub_forum_link}/")
if response.status_code == 429:
time.sleep(5)
continue
return json.loads(response.text)['threads']
def find_most_recent_thread(posts, required_posts=True):
most_recent_thread = None
most_recent_reply = None
most_recent_reply_date = 0
if required_posts:
for thread in posts["threads"]:
for reply in thread["replies"]:
if reply["date"] > most_recent_reply_date:
most_recent_reply_date = reply["date"]
most_recent_thread = thread
else:
for thread in posts["threads"]:
if len(thread["replies"]):
for reply in thread["replies"]:
if reply["date"] > most_recent_reply_date:
most_recent_reply_date = reply["date"]
most_recent_thread = thread
most_recent_reply = reply
else:
if thread['date'] > most_recent_reply_date:
most_recent_reply_date = thread['date']
most_recent_thread = thread
most_recent_reply = {'text': thread['text'], 'id': thread['id']}
return most_recent_thread, most_recent_reply
def find_most_recent_post(posts):
most_recent_thread = None
most_recent_reply_date = 0
most_recent_reply = None
for thread in posts["threads"]:
if not thread["replies"]:
continue
latest_reply_date = max(reply["date"] for reply in thread["replies"])
if latest_reply_date > most_recent_reply_date:
most_recent_reply_date = latest_reply_date
most_recent_thread = thread
for reply in thread["replies"]:
if reply['date'] == latest_reply_date:
most_recent_reply = reply
return most_recent_thread, most_recent_reply
def get_thread_texts(thread):
reply_texts = [clean_reply(reply["text"]) for reply in thread["replies"]]
reply_texts.insert(0, thread['text'])
return list(filter(None, reply_texts))
def get_board_info(board_id: str):
for board in get_sub_forums():
if board['link'] == board_id:
return board
return False