Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from watchdog.observers import Observer
- from watchdog.events import FileSystemEventHandler
- import pygame
- import os
- # Paths to the files
- log_file_path = "D:\\Games\\clients\\pixelmon\\logs\\latest.log"
- audio_file_path = "D:\\12333\\bip-bip.mp3"
- # Directory to monitor
- directory_to_monitor = os.path.dirname(log_file_path)
- # List of strings to search for
- search_strings = ["Легендарный покемон"]
- # Initialize Pygame mixer
- pygame.mixer.init()
- class FileChangeHandler(FileSystemEventHandler):
- def __init__(self):
- self.file_position = 0
- def on_modified(self, event):
- if not event.is_directory and event.src_path.endswith(log_file_path):
- with open(log_file_path, "r", encoding="utf-8") as file:
- # Move to where we left off
- file.seek(self.file_position)
- # Read new lines
- lines = file.readlines()
- # Remember our new position
- self.file_position = file.tell()
- for line in lines:
- if "[Client thread/INFO]: [CHAT]" in line:
- print("Found chat message")
- if any(s in line for s in search_strings):
- print("Found target phrase")
- pygame.mixer.music.load(audio_file_path)
- pygame.mixer.music.play()
- break
- if __name__ == "__main__":
- event_handler = FileChangeHandler()
- observer = Observer()
- observer.schedule(event_handler, path=directory_to_monitor, recursive=False)
- observer.start()
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- observer.stop()
- observer.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement