Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import html
- import requests
- import itertools
- import colorama
- from colorama import Fore
- colorama.init()
- def colored(color, string):
- return color + string + Fore.RESET
- def getDigits(postNumber):
- # Using strings because I'm lazy and it werks
- rev = str(postNumber)[::-1]
- repeating = itertools.takewhile(lambda c: c == rev[0], rev)
- return int("".join(repeating))
- # Remove HTML tags and quotes
- HTML_TAG_REGEX = re.compile(r"<[^>]*>")
- QUOTE_REGEX = re.compile(r">>\d+")
- class Post:
- def __init__(self, number, comment):
- self.number = number
- self.digits = getDigits(number)
- self.comment = comment
- def coloredNumber(self):
- numberStr = str(self.number)
- digitStr = str(self.digits)
- firstLength = len(numberStr) - len(digitStr)
- return numberStr[:firstLength] + colored(Fore.CYAN, digitStr)
- def formatComment(self):
- comment = self.comment
- comment = html.unescape(comment)
- comment = comment.replace("<br>", "\n")
- comment = HTML_TAG_REGEX.sub("", comment)
- comment = QUOTE_REGEX.sub("", comment)
- if len(comment) > 50:
- comment = comment[:45] + "[...]"
- lines = []
- for line in comment.split("\n"):
- if len(line) > 0:
- if line[0] == ">":
- lines.append(colored(Fore.LIGHTGREEN_EX, line))
- else:
- lines.append(line)
- return " ".join(lines)
- if __name__ == "__main__":
- boardId = input("Which board? ").strip("/")
- threadNo = input("Which thread? ")
- try:
- url = f"https://a.4cdn.org/{boardId}/thread/{threadNo}.json"
- thread = requests.get(url).json()
- except:
- print(f"Can't get information from thread '/{boardId}/{threadNo}'")
- print("Make sure the board and the thread both exist and are still alive")
- exit(1)
- posts = []
- for post in thread["posts"]:
- number = post["no"]
- comment = post["com"] if "com" in post else "(no comment)"
- p = Post(number, comment)
- # Only save posts with dubs, trips, etc.
- if len(str(p.digits)) > 1:
- posts.append(Post(number, comment))
- if len(posts) == 0:
- print("Nobody got lucky in this thread")
- else:
- posts.sort(key=lambda p: p.digits, reverse=True)
- for post in posts[:10]:
- print(f"{post.coloredNumber()}: {post.formatComment()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement