Skip to content

Instantly share code, notes, and snippets.

@lxndr-rl
Last active March 1, 2023 00:24
Show Gist options
  • Save lxndr-rl/b6b3ebbb2e584c3674137c910897b182 to your computer and use it in GitHub Desktop.
Save lxndr-rl/b6b3ebbb2e584c3674137c910897b182 to your computer and use it in GitHub Desktop.
Obtener datos de lo que se reproduce actualmente en Windows (Nombre, Artista, Álbum, Miniatura)
import asyncio
from winrt.windows.media.control import \
GlobalSystemMediaTransportControlsSessionManager as MediaManager
from winrt.windows.storage.streams import \
DataReader, Buffer, InputStreamOptions
async def get_media_info():
sessions = await MediaManager.request_async()
current_session = sessions.get_current_session()
if current_session:
if current_session.source_app_user_model_id:
info = await current_session.try_get_media_properties_async()
info_dict = {song_attr: info.__getattribute__(
song_attr) for song_attr in dir(info) if song_attr[0] != '_'}
info_dict['genres'] = list(info_dict['genres'])
return info_dict
async def read_stream_into_buffer(stream_ref, buffer):
readable_stream = await stream_ref.open_read_async()
readable_stream.read_async(
buffer, buffer.capacity, InputStreamOptions.READ_AHEAD)
current_media_info = asyncio.run(get_media_info())
thumb_stream_ref = current_media_info['thumbnail']
thumb_read_buffer = Buffer(5000000)
file_title = str(current_media_info["title"]).strip().replace(
' ', '_').replace('/', '_').replace('\\', '_').replace(':', '_').replace('*', '_').replace('?', '_').replace('"', '_').replace('<', '_').replace('>', '_').replace('|', '_')
path = f'assets/{file_title}.jpg'
asyncio.run(read_stream_into_buffer(thumb_stream_ref, thumb_read_buffer))
buffer_reader = DataReader.from_buffer(thumb_read_buffer)
byte_buffer = buffer_reader.read_bytes(thumb_read_buffer.length)
with open(path, 'wb+') as fobj:
fobj.write(bytearray(byte_buffer))
current_media_info['thumbnail'] = {
'thumbstream': thumb_stream_ref, 'path': path}
print(current_media_info)
{
"album_artist": "Eminem",
"album_title": "The Marshall Mathers LP",
"album_track_count": 0,
"artist": "Eminem",
"genres": [],
"playback_type": 1,
"subtitle": "",
"thumbnail": {
"thumbstream": <_winrt_Windows_Storage_Streams.IRandomAccessStreamReference object at 0x00000247C164A090>,
"path": "assets/The_Real_Slim_Shady.jpg"
},
"title": "The Real Slim Shady",
"track_number": 8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment