Исходник Вывод песни которая щас играет (spotify)

1
Пользователь
Статус
Оффлайн
Регистрация
22 Авг 2021
Сообщения
487
Реакции[?]
45
Поинты[?]
8K
переписано с c++, выкладываю потому что не нашел такого на python'e (везде через spotify api)
1692512434396.png
Python:
import win32gui, win32process, win32api
import win32con
import psutil
import ctypes
import time

def is_spotify(entry):
    return entry.name() == "Spotify.exe"

def enum_windows_proc(hwnd, pids):
    _, winid = win32process.GetWindowThreadProcessId(hwnd)
    if not pids:
        return False
    if winid in pids:
        title = win32gui.GetWindowText(hwnd)
        if ("GDI+ Window (Spotify.exe)" in title or
            "Default IME" in title or
            not title or
            "MSCTFIME UI" in title):
            return True
        if len(title) > 1:
            global procID, ttl
            procID = winid
            ttl = title
    return True

def spotify_window():
    pids = []
    for proc in psutil.process_iter(['pid', 'name']):
        if is_spotify(proc):
            pids.append(proc.info['pid'])
    win32gui.EnumWindows(enum_windows_proc, pids)
    return bool(pids)

def display_current_spotify_song():
    global lasttime
    previous_title = None
    while True: 
        if spotify_window():
            ttl_utf8 = ttl.encode('utf-8')
            if b"spotify" in ttl_utf8.lower():
                spotify_title = "Stopped / Paused"
            else:
                spotify_title = ttl 
            if "Spotify Premium" in spotify_title:
                spotify_title = "Stopped / Paused"
            if "Spotify Free" in spotify_title:
                spotify_title = "Stopped / Paused"
        else:
            spotify_title = "Spotify Not Found"
            
        if spotify_title != previous_title:
            print(spotify_title)
            previous_title = spotify_title
        time.sleep(1)

procID = None
ttl = ""
lasttime = 0

display_current_spotify_song()
 
Сверху Снизу