Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ETS2 music player info display program
- # Requires psutil and music-tag external library
- # Tested on Windows 11 x64, Python 3.9.9, psutil==5.9.8, music-tag==0.4.3
- # Code by yclee126
- import psutil
- import os
- import time
- import music_tag
- from pathlib import Path
- # symlink reader
- # https://github.com/python/cpython/blob/master/Tools/scripts/lll.py
- def lll(dirname, sym_dirs):
- for name in os.listdir(dirname):
- if name not in (os.curdir, os.pardir):
- full = os.path.join(dirname, name)
- if os.path.isdir(full) and not os.path.islink(full):
- lll(full, sym_dirs)
- elif os.path.islink(full):
- symlink_dest = os.readlink(full)
- # remove \\?\ prefix in Windows
- if symlink_dest[:4] == '\\\\?\\':
- symlink_dest = symlink_dest[4:]
- sym_dirs.append(symlink_dest)
- def get_pid_by_name(process_name):
- for process in psutil.process_iter(['pid', 'name']):
- if process.info['name'] == process_name:
- return process.info['pid']
- return None
- if __name__ == '__main__':
- # get all music directories including shallow symlinks
- music_path = os.path.expanduser('~/Documents/Euro Truck Simulator 2/music')
- music_path = os.path.normpath(music_path) # normalize between / or \
- music_paths = [music_path]
- lll(music_path, music_paths)
- print('Music dirs:', music_paths)
- # get eurotruck process
- process_name = "eurotrucks2.exe"
- pid = get_pid_by_name(process_name)
- if pid is None:
- print('Process not found')
- exit()
- # monitor eurotruck process
- try:
- process = psutil.Process(pid)
- print(f"The PID of the process '{process_name}' is: {pid}")
- prev_file_path = None
- while True:
- new_file_path = None
- # monitor opened files
- files = process.open_files()
- for file in files:
- for music_path in music_paths:
- if music_path in file.path:
- if file.path != prev_file_path:
- new_file_path = file.path
- prev_file_path = file.path
- break
- # print music info
- if new_file_path is not None:
- tag = music_tag.load_file(new_file_path)
- length = int(tag['#length'])
- minutes = int(length / 60)
- seconds = int(length % 60)
- print((f"\n\n\n\n\n\n"
- f"Title : {tag['title']}\n"
- f"Artist: {tag['artist']}\n"
- f"Album : {tag['album']}\n"
- f"Length: {minutes:02d}:{seconds:02d}\n"
- f"------:\n"
- f"File : {Path(new_file_path).name}\n"))
- time.sleep(1)
- except psutil.NoSuchProcess:
- print('Process terminated')
- exit()
- except:
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement