Advertisement
Guest User

gets

a guest
Mar 6th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import re
  2. import html
  3. import requests
  4. import itertools
  5. import colorama
  6. from colorama import Fore
  7.  
  8.  
  9. colorama.init()
  10.  
  11.  
  12. def colored(color, string):
  13.     return color + string + Fore.RESET
  14.  
  15.  
  16. def getDigits(postNumber):
  17.     # Using strings because I'm lazy and it werks
  18.     rev       = str(postNumber)[::-1]
  19.     repeating = itertools.takewhile(lambda c: c == rev[0], rev)
  20.  
  21.     return int("".join(repeating))
  22.  
  23.  
  24. # Remove HTML tags and quotes
  25. HTML_TAG_REGEX = re.compile(r"<[^>]*>")
  26. QUOTE_REGEX    = re.compile(r">>\d+")
  27.  
  28.  
  29. class Post:
  30.     def __init__(self, number, comment):
  31.         self.number  = number
  32.         self.digits  = getDigits(number)
  33.         self.comment = comment
  34.  
  35.  
  36.     def coloredNumber(self):
  37.         numberStr   = str(self.number)
  38.         digitStr    = str(self.digits)
  39.         firstLength = len(numberStr) - len(digitStr)
  40.        
  41.         return numberStr[:firstLength] + colored(Fore.CYAN, digitStr)
  42.  
  43.  
  44.     def formatComment(self):
  45.         comment = self.comment
  46.  
  47.         comment = html.unescape(comment)
  48.         comment = comment.replace("<br>", "\n")
  49.         comment = HTML_TAG_REGEX.sub("", comment)
  50.         comment = QUOTE_REGEX.sub("", comment)
  51.  
  52.         if len(comment) > 50:
  53.             comment = comment[:45] + "[...]"
  54.  
  55.         lines = []
  56.  
  57.         for line in comment.split("\n"):
  58.             if len(line) > 0:
  59.                 if line[0] == ">":
  60.                     lines.append(colored(Fore.LIGHTGREEN_EX, line))
  61.                 else:
  62.                     lines.append(line)
  63.  
  64.         return " ".join(lines)
  65.  
  66.  
  67. if __name__ == "__main__":
  68.     boardId  = input("Which board? ").strip("/")
  69.     threadNo = input("Which thread? ")
  70.  
  71.     try:
  72.         url = f"https://a.4cdn.org/{boardId}/thread/{threadNo}.json"
  73.         thread = requests.get(url).json()
  74.     except:
  75.         print(f"Can't get information from thread '/{boardId}/{threadNo}'")
  76.         print("Make sure the board and the thread both exist and are still alive")
  77.         exit(1)
  78.  
  79.  
  80.     posts = []
  81.  
  82.     for post in thread["posts"]:
  83.         number  = post["no"]
  84.         comment = post["com"] if "com" in post else "(no comment)"
  85.         p       = Post(number, comment)
  86.  
  87.         # Only save posts with dubs, trips, etc.
  88.         if len(str(p.digits)) > 1:
  89.             posts.append(Post(number, comment))
  90.  
  91.  
  92.     if len(posts) == 0:
  93.         print("Nobody got lucky in this thread")
  94.  
  95.     else:
  96.         posts.sort(key=lambda p: p.digits, reverse=True)
  97.  
  98.         for post in posts[:10]:
  99.             print(f"{post.coloredNumber()}: {post.formatComment()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement