utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import requests
  2. import json
  3. import time
  4. from colorama import Fore, Back, Style
  5. def getThreadList(response):
  6. ''' Return a list of Ints which denote the thread numbers '''
  7. return [ t["no"] for board in response for t in board["threads"] ]
  8. def getThreadObjects(thread):
  9. ''' Return a list of posts for a thread '''
  10. return [ post for post in thread["posts"] ]
  11. def getComments(thread):
  12. ''' Return a list of comments as strings for a thread '''
  13. t = []
  14. for post in getThreadObjects(thread):
  15. if "com" in post:
  16. t.append( post["com"] )
  17. return t
  18. def cleantrash(trash):
  19. ''' Clean up a string based on arbitrary rules '''
  20. trash=trash.replace("<br>","\n")
  21. trash=trash.replace(". ",".\n")
  22. trash=trash.replace("&gt;",">")
  23. trash=trash.replace("<span class=\"quote\">","")
  24. trash=trash.replace("<wbr>","")
  25. trash=trash.replace("<a href=\"","")
  26. trash=trash.replace("</a>","")
  27. trash=trash.replace("</span>","")
  28. trash=trash.replace("class=\"quotelink\"","")
  29. trash=trash.replace("&#039;","\'")
  30. trash=trash.replace("&quot;","\"")
  31. trash=trash.replace("&amp;","&")
  32. clean = trash
  33. return clean