Salut,
Toujours dans mes bidouillages de diffusion de flux vidéo/audio, j'ai besoin d'afficher le titre de la musique diffusée, un script remplissant cette fonction a été développé en python (à la base pour windows mais bon, il ne devrait pas y avoir de problème sous linux), j'ai tout mis en place (l'interface lua de VLC, le script et quand je l’exécute, il me créé un fichier LectureEnCours.txt, mais dès que je lance la lecture, il me sort:
Traceback (most recent call last):
File "NowPlaying.py", line 110, in <module>
getInfo()
File "NowPlaying.py", line 49, in getInfo
root = ET.fromstring(r.text)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1300, in XML
parser.feed(text)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1640, in feed
self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1543-1544: ordinal not in range(128)
Il n'arrive donc pas à En/Décoder les caractères envoyés par VLC dans le flux XML car supérieurs à 128 ?! Que puis-je y faire ?
Le script en question
#!/usr/bin/env python
#===============================================================================
# title :NowPlaying.py
# description :This script will create a NowPlaying.txt file that contains
# the info for the song that is currently being played via VLC
# author :Tipher88
# contributors :Etuldan
# date :20160220
# version :1.2.0
# usage :python NowPlaying.py
# notes :For this script to work you need to follow the instructions
# in the included README.txt file
# python_version :2.7.8 & 3.2.5
#===============================================================================
import os, time, requests
import xml.etree.ElementTree as ET
# Global variable to keep track of song info being printed and check for changes
currentSongInfo = ''
def getInfo():
# CUSTOM: Separator can be changed to whatever you want
separator = ' | '
nowPlaying = 'UNKNOWN'
songTitle = 'UNKNOWN'
songArtist = 'UNKNOWN'
fileName = ''
s = requests.Session()
# CUSTOM: Username is blank, just provide the password
s.auth = ('', 'mdp')
# Attempt to retrieve song info from the web interface
try:
r = s.get('http://localhost:8080/requests/status.xml', verify=False)
if('401 Client error' in r.text):
print('Web Interface Error: Do the passwords match as described in the README.txt?')
return
except:
print('Web Interface Error: Is VLC running? Did you enable the Web Interface as described in the README.txt?')
return
# Okay, now we know we have a response with our xml data in it
# Save the response data
root = ET.fromstring(r.text)
# Loop through all info nodes to find relevant metadata
for info in root.iter('info'):
# Save the name attribute of the info node
name = info.get('name')
# See if the info node we are looking at is now_playing
if(name == 'now_playing'):
nowPlaying = info.text
else:
# See if the info node we are looking at is for the artist
if(name == 'artist'):
songArtist = info.text
# See if the info node we are looking at is for the title
if(name == 'title'):
songTitle = info.text
# See if the info node we are looking at is for the filename
if(name == 'filename'):
fileName = info.text
fileName = os.path.splitext(fileName)[0]
# END: for info in root.iter('info')
# If the now_playing node exists we should use that and ignore the rest
if(nowPlaying != 'UNKNOWN'):
writeSongInfoToFile('%s%s' % (nowPlaying, separator))
else:
# Make sure a songTitle and songArtist was found in the metadata
if(songTitle != 'UNKNOWN' and
songArtist != 'UNKNOWN'):
# Both songTitle and song Artist have been set so use both
writeSongInfoToFile('%s - %s%s' % (songTitle, songArtist, separator))
elif( songTitle != 'UNKNOWN' ):
# Just use the songTitle
writeSongInfoToFile('%s%s' % (songTitle, separator))
elif( fileName != '' ):
# Use the fileName as a last resort
writeSongInfoToFile('%s%s' % (fileName, separator))
else:
# This should print 'UNKNOWN - UNKNOWN' because no relevant metadata was
# found
writeSongInfoToFile('%s - %s%s' % (songTitle, songArtist, separator))
# END: getInfo()
def writeSongInfoToFile( str ):
global currentSongInfo
if(currentSongInfo != str):
currentSongInfo = str
print(currentSongInfo)
# CUSTOM: The output file name can be changed
textFile = open('NowPlaying.txt', 'w')
textFile.write(currentSongInfo)
textFile.close()
# END: writeSongInfoToFile( str )
if __name__ == '__main__':
while 1:
getInfo()
# CUSTOM: Sleep for a number of seconds before checking again
time.sleep(5)
# END: if __name__ == '__main__'