Advertisement
Snuggledash

4chan Post Randomizer (original concept)

May 27th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import random
  4. # needs python3-requests and python3-html2text packages
  5. import requests, html2text
  6.  
  7. board_list = requests.get('https://a.4cdn.org/boards.json')
  8. if board_list.ok:
  9.     all_boards = [b['board'] for b in board_list.json()['boards']]
  10.     random_board = random.choice(all_boards)
  11.     catalog = requests.get('https://a.4cdn.org/' + random_board + '/catalog.json')
  12.     if catalog.ok:
  13.         catalog_threads = [thread['no'] for page in catalog.json() for thread in page['threads']]
  14.         random_thread = random.choice(catalog_threads)
  15.         posts = requests.get('https://a.4cdn.org/' + random_board + '/thread/' + str(random_thread) + '.json')
  16.         if posts.ok:
  17.             posts_list = posts.json()['posts']
  18.             random_post = random.choice(posts_list)
  19.             post_link = 'https://boards.4chan.org/' + random_board + '/res/' + str(random_thread)
  20.             if random_post['no'] != random_thread:
  21.                 post_link += '#p' + str(random_post['no'])
  22.             print(post_link)
  23.             if 'com' in random_post:
  24.                 plaintext = html2text.html2text(random_post['com'])
  25.                 print(plaintext)
  26.         else:
  27.             print('Error getting posts from /' + random_board + '/' + random_thread + '!')
  28.     else:
  29.         print('Error getting catalog on /' + random_board + '/!')
  30. else:
  31.     print('Error getting board list!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement