Advertisement
yclee126

Euro Truck Simulator 2 music info display

Feb 25th, 2024
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | Gaming | 0 0
  1. # ETS2 music player info display program
  2. # Requires psutil and music-tag external library
  3. # Tested on Windows 11 x64, Python 3.9.9, psutil==5.9.8, music-tag==0.4.3
  4.  
  5. # Code by yclee126
  6.  
  7.  
  8. import psutil
  9. import os
  10. import time
  11. import music_tag
  12. from pathlib import Path
  13.  
  14.  
  15. # symlink reader
  16. # https://github.com/python/cpython/blob/master/Tools/scripts/lll.py
  17. def lll(dirname, sym_dirs):
  18.     for name in os.listdir(dirname):
  19.         if name not in (os.curdir, os.pardir):
  20.             full = os.path.join(dirname, name)
  21.             if os.path.isdir(full) and not os.path.islink(full):
  22.                 lll(full, sym_dirs)
  23.             elif os.path.islink(full):
  24.                 symlink_dest = os.readlink(full)
  25.                 # remove \\?\ prefix in Windows
  26.                 if symlink_dest[:4] == '\\\\?\\':
  27.                     symlink_dest = symlink_dest[4:]
  28.                 sym_dirs.append(symlink_dest)
  29.  
  30. def get_pid_by_name(process_name):
  31.     for process in psutil.process_iter(['pid', 'name']):
  32.         if process.info['name'] == process_name:
  33.             return process.info['pid']
  34.     return None
  35.  
  36. if __name__ == '__main__':
  37.     # get all music directories including shallow symlinks
  38.     music_path = os.path.expanduser('~/Documents/Euro Truck Simulator 2/music')
  39.     music_path = os.path.normpath(music_path) # normalize between / or \
  40.    
  41.     music_paths = [music_path]
  42.     lll(music_path, music_paths)
  43.  
  44.     print('Music dirs:', music_paths)
  45.  
  46.     # get eurotruck process
  47.     process_name = "eurotrucks2.exe"
  48.     pid = get_pid_by_name(process_name)
  49.     if pid is None:
  50.         print('Process not found')
  51.         exit()
  52.  
  53.     # monitor eurotruck process
  54.     try:
  55.         process = psutil.Process(pid)
  56.         print(f"The PID of the process '{process_name}' is: {pid}")
  57.  
  58.         prev_file_path = None
  59.         while True:
  60.             new_file_path = None
  61.  
  62.             # monitor opened files
  63.             files = process.open_files()
  64.             for file in files:
  65.                 for music_path in music_paths:
  66.                     if music_path in file.path:
  67.                         if file.path != prev_file_path:
  68.                             new_file_path = file.path
  69.                             prev_file_path = file.path
  70.                         break
  71.            
  72.             # print music info
  73.             if new_file_path is not None:
  74.                 tag = music_tag.load_file(new_file_path)
  75.                 length = int(tag['#length'])
  76.                 minutes = int(length / 60)
  77.                 seconds = int(length % 60)
  78.                
  79.                 print((f"\n\n\n\n\n\n"
  80.                        f"Title : {tag['title']}\n"
  81.                        f"Artist: {tag['artist']}\n"
  82.                        f"Album : {tag['album']}\n"
  83.                        f"Length: {minutes:02d}:{seconds:02d}\n"
  84.                        f"------:\n"
  85.                        f"File  : {Path(new_file_path).name}\n"))
  86.            
  87.             time.sleep(1)
  88.  
  89.     except psutil.NoSuchProcess:
  90.         print('Process terminated')
  91.         exit()
  92.  
  93.     except:
  94.         raise
Tags: music Ets2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement