Advertisement
serd2011

234902290

Sep 11th, 2023 (edited)
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. import time
  2. from watchdog.observers import Observer
  3. from watchdog.events import FileSystemEventHandler
  4. import pygame
  5. import os
  6.  
  7. # Paths to the files
  8. log_file_path = "D:\\Games\\clients\\pixelmon\\logs\\latest.log"
  9. audio_file_path = "D:\\12333\\bip-bip.mp3"
  10.  
  11. # Directory to monitor
  12. directory_to_monitor = os.path.dirname(log_file_path)
  13.  
  14. # List of strings to search for
  15. search_strings = ["Легендарный покемон"]
  16.  
  17. # Initialize Pygame mixer
  18. pygame.mixer.init()
  19.  
  20. class FileChangeHandler(FileSystemEventHandler):
  21.     def __init__(self):
  22.         self.file_position = 0
  23.  
  24.     def on_modified(self, event):
  25.         if not event.is_directory and event.src_path.endswith(log_file_path):
  26.             with open(log_file_path, "r", encoding="utf-8") as file:
  27.                 # Move to where we left off
  28.                 file.seek(self.file_position)
  29.                 # Read new lines
  30.                 lines = file.readlines()
  31.                 # Remember our new position
  32.                 self.file_position = file.tell()
  33.                 for line in lines:
  34.                     if "[Client thread/INFO]: [CHAT]" in line:
  35.                         print("Found chat message")
  36.                         if any(s in line for s in search_strings):
  37.                             print("Found target phrase")
  38.                             pygame.mixer.music.load(audio_file_path)
  39.                             pygame.mixer.music.play()
  40.                             break
  41.  
  42. if __name__ == "__main__":
  43.     event_handler = FileChangeHandler()
  44.     observer = Observer()
  45.     observer.schedule(event_handler, path=directory_to_monitor, recursive=False)
  46.     observer.start()
  47.  
  48.     try:
  49.         while True:
  50.             time.sleep(1)
  51.     except KeyboardInterrupt:
  52.         observer.stop()
  53.  
  54.     observer.join()
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement