from watchdog.observers import Observer 
from watchdog.events import FileSystemEventHandler 
import os,re 
from threading import Event, Thread 
import time 
import requests 
 
LOGFILE=r"C:\Users\virtVmix-1\Documents\Adobe\Adobe Media Encoder\22.0\AMEEncodingLog.txt" 
LOGERRFILE=r"C:\Users\virtVmix-1\Documents\Adobe\Adobe Media Encoder\22.0\AMEEncodingErrorLog.txt" 
 
TOKEN= ""
chat_id= ""
 
DIR=os.path.dirname(LOGFILE) 
Log_event=Event() 
Err_event=Event() 
Stop_event=Event() 
 
def send_message(m): 
    print(m) 
    requests.get(f'https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={m}') 
    pass 
 
def parcer(event,LOGFILE): 
    SEEK=0 
    with open(LOGFILE,encoding='UTF-16') as file: 
        file.read() 
        SEEK=file.tell() 
    while not Stop_event.is_set(): 
        event.wait() 
        time.sleep(5) 
        with open(LOGFILE,encoding='UTF-16') as file: 
            file.seek(SEEK) 
            for line in file.readlines(): 
                if m:=re.search(r': Queue Started',line): 
                    send_message(line) 
                elif m:=re.search(r'- Output File: ',line): 
                    outfilename=line.split('\\')[-1] 
                    send_message(outfilename) 
                elif m:=re.search(r'\d+/\d+/\d+ \d+:\d+:\d+ [AP]M : ',line): 
                    send_message(line) 
            SEEK=file.tell() 
        event.clear() 
 
class EventHandler(FileSystemEventHandler): 
        def on_modified(self, event): 
            print(time.strftime('%M:%S'),event.event_type,event.src_path) 
            if event.src_path== LOGFILE: 
                Log_event.set() 
            elif event.src_path== LOGERRFILE: 
                Err_event.set() 
        def on_closed(self, event):            print(time.strftime('%M:%S'),event.event_type,event.src_path) 
 
logThread=Thread(target=parcer, args=(Log_event,LOGFILE)) 
errThread=Thread(target=parcer, args=(Err_event,LOGERRFILE)) 
observer = Observer() 
observer.schedule(EventHandler(), DIR) 
try: 
    observer.start() 
    logThread.start() 
    errThread.start() 
    while input != 'stop':pass 
finally: 
    Stop_event.set() 
    Log_event.set() 
    Err_event.set() 
    observer.stop() 
    logThread.join() 
    errThread.join() 
    observer.join() 
    print('stopped')




