#!/usr/bin/python3
import random
# needs python3-requests and python3-html2text packages
import requests, html2text
board_list = requests.get('https://a.4cdn.org/boards.json')
if board_list.ok:
all_boards = [b['board'] for b in board_list.json()['boards']]
random_board = random.choice(all_boards)
catalog = requests.get('https://a.4cdn.org/' + random_board + '/catalog.json')
if catalog.ok:
catalog_threads = [thread['no'] for page in catalog.json() for thread in page['threads']]
random_thread = random.choice(catalog_threads)
posts = requests.get('https://a.4cdn.org/' + random_board + '/thread/' + str(random_thread) + '.json')
if posts.ok:
posts_list = posts.json()['posts']
random_post = random.choice(posts_list)
post_link = 'https://boards.4chan.org/' + random_board + '/res/' + str(random_thread)
if random_post['no'] != random_thread:
post_link += '#p' + str(random_post['no'])
print(post_link)
if 'com' in random_post:
plaintext = html2text.html2text(random_post['com'])
print(plaintext)
else:
print('Error getting posts from /' + random_board + '/' + random_thread + '!')
else:
print('Error getting catalog on /' + random_board + '/!')
else:
print('Error getting board list!')
Comments