import time
import json
import psutil
import pypresence

rpc = pypresence.Presence('1150714322104877108')  # Put your Application ID here
data = None

rpc.connect()

def search_file(file_path, word):
    with open(file_path, 'r') as file:
        for line in reversed(file.readlines()):
            if word in line:
                return line
        



while True:
    for proc in psutil.process_iter():
        # This is an example of structural pattern matching added in Python 3.10 | Tutorial: https://www.python.org/dev/peps/pep-0636/
        match proc.name().lower():
            case "ffmpeg.exe":
                speed = search_file('E:/Projects/python/ffmpeg_work_folder/testoutprogress.txt', 'speed=')
                frame = search_file('E:/Projects/python/ffmpeg_work_folder/testoutprogress.txt', 'frame=')
                state_string = speed + "@ " + frame
                d = {"state": state_string, "details": "encoding"}
                with open("E:/Projects/python/ffmpeg_work_folder/testoutprogress.txt", 'r+') as file:
                    file.truncate(0)
                data = d
                break
            case "itunes.exe":
                data = {"state": "XYZ", "details": "Zoom", "large_image": "foo", "large_text": "bar"}
                break
            case _:
                data = None

    # You don't have to update the status like this, you could do it in the for loop or call a different function to do it.
    if data:
        rpc.update(**data)
    else:
        rpc.clear()

    time.sleep(15)