SHARE
TWEET

Chat Log Searcher

a guest Apr 21st, 2018 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. Club Penguin Wiki chat log searcher.
  5. Use username = None to match any user.
  6. term may be either a string to find or a regular expression.
  7. """
  8.  
  9. __author__ = "Hey.youcp, nagi123"
  10. __version__ = "1.0"
  11. __date__ = "April 1, 2018"
  12.  
  13. import os
  14. import html
  15. import subprocess
  16. import datetime
  17. import re
  18. import requests
  19.  
  20. def daterange(start, end):
  21.     for i in range((end - start).days + 1):
  22.         yield start + datetime.timedelta(i)
  23.  
  24. def search(username, term, date):
  25.     response = requests.get(date.strftime("http://clubpenguin.wikia.com/wiki/Club_Penguin_Wiki:Chat/Logs/%d_%B_%Y?action=raw"))
  26.     if not response.text:
  27.         return None, 0
  28.     lines, line_count = [], 0
  29.     for line in response.iter_lines(decode_unicode=True):
  30.         line = html.unescape(line)
  31.         match = re.match(r"\[?.*?\]? <(.*?)>", line)
  32.         if (username is None or match is not None and match.group(1).lower() == username.lower()) and re.search(term, line, flags=re.IGNORECASE):
  33.                 lines.append(line)
  34.         line_count += 1
  35.     return lines, line_count
  36.  
  37. def search_all(username, term, start, end):
  38.     if username is None:
  39.         print('Searching for "{}"'.format(term))
  40.     else:
  41.         print('Searching for messages by "{}" containing "{}"'.format(username, term))
  42.     pages_found, total_results_count = 0, 0
  43.     with open("output.txt", "w", encoding="utf-8") as output:
  44.         for date in daterange(start, end):
  45.             lines, line_count = search(username, term, date)
  46.             if lines is None:
  47.                 print(date.strftime("%B %d, %Y logs not found."))
  48.                 continue
  49.             results_count = 0
  50.             for line in lines:
  51.                 if results_count == 0:
  52.                     output.write(date.strftime("\n===%B %d, %Y===\n"))
  53.                 output.write(line + "\n")
  54.                 results_count += 1
  55.             print(date.strftime("%B %d, %Y: {} lines, {} results.".format(line_count, results_count)))
  56.             pages_found += 1
  57.             total_results_count += results_count
  58.     print("{} pages were processed and {} total results were found.".format(pages_found, total_results_count))
  59.     return pages_found, total_results_count
  60.  
  61. def display(total_results_count):
  62.     if total_results_count:
  63.         if os.name == "nt":
  64.             os.startfile("output.txt")
  65.         elif os.name == "posix":
  66.             subprocess.call(["xdg-open", "output.txt"])
  67.     else:
  68.         os.remove("output.txt")
  69.  
  70. def main():
  71.     username = None
  72.     term = r"\bHello\b"
  73.     start = datetime.date(2018, 1, 1)
  74.     end = min(datetime.date(2018, 8, 30), datetime.date.today())
  75.     pages_found, total_results_count = search_all(username, term, start, end)
  76.     display(total_results_count)
  77.  
  78. if __name__ == "__main__":
  79.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top