49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import requests
|
|
|
|
from reeere.ai import clean_reply
|
|
|
|
|
|
def fetch_and_sort_threads(board_name: str):
|
|
url = f"https://a.4cdn.org/{board_name}/threads.json"
|
|
response = requests.get(url)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
all_threads = [thread for page in data for thread in page["threads"]]
|
|
sorted_threads = sorted(all_threads, key=lambda x: x["last_modified"], reverse=True)
|
|
filtered_threads = [thread for thread in sorted_threads if thread["replies"] > 0]
|
|
|
|
return filtered_threads
|
|
else:
|
|
print(f"Error fetching data: {response.status_code}")
|
|
return []
|
|
|
|
|
|
def fetch_thread_by_id(board_name, thread_id):
|
|
url = f"https://a.4cdn.org/{board_name}/thread/{thread_id}.json"
|
|
response = requests.get(url)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
posts_with_images = []
|
|
|
|
for post in data["posts"]:
|
|
if "tim" in post and "ext" in post:
|
|
image_url = f"https://i.4cdn.org/{board_name}/{post['tim']}{post['ext']}"
|
|
post["image_url"] = image_url
|
|
posts_with_images.append(post)
|
|
|
|
return posts_with_images
|
|
else:
|
|
print(f"Error fetching thread: {response.status_code}")
|
|
return None
|
|
|
|
|
|
# def create_thread_context(thread: list):
|
|
# reply_texts = []
|
|
# for reply in thread:
|
|
# if 'com' in reply.keys():
|
|
# reply_texts.append(clean_reply(reply['com']))
|
|
# print(clean_reply(reply['com']))
|
|
# return reply_texts
|