Ubuntu Accomplishments: Desktop
June 10, 2012 6 Comments
I have been following the Ubuntu Accomplishments project for a while, but failed to contribute until recently. The first contribution was inspired by a multimedia accomplishment for adding music to your machine. This simple accomplishment will trigger if you add music to your music folder. I keep my music on a NAS so I would never earn that accomplishment. I decided to build a script that would determine if a user had added music to Rhythmbox. This would account for users who keep music in folder other than music as long as they used Rhythmbox as their music player. Here is the code:
#!/usr/bin/python
import commands, sys, os
homeDir = os.getenv("HOME")
path = os.path.join(homeDir,'.local/share/rhythmbox/rhythmdb.xml')
if os.path.exists(path):
#user has used Rhythmbox now we need to test for the existence of music
# test for the existence of <entry type="song"> in the rhythmdb.xml
file = open(path)
# convert file to string
data = file.read()
# we do not need to keep the file open
file.close()
# test for the existence of a song in the file
if "song" in data:
#user has music in Rhythmbox
sys.exit(0)
# print "user has music"
else:
#user does not have music in Rhythmbox
sys.exit(0)
# print "user does not have music"
else:
# user has not used Rhythmbox
sys.exit(1)
As I explored this script I noticed that the original script that inspired me was testing positive even if there was only a text file and no audio files. I then decided to improve on the original script. Here is the code:
#!/usr/bin/python
import commands, sys, os
homeDir = os.getenv("HOME")
path = os.path.join(homeDir,'.config/user-dirs.dirs')
result = open(path, "r")
content = result.readline()
while (content != "" ):
content.replace( "\n", "" )
content = result.readline()
startStr = content[0:13]
if startStr == 'XDG_MUSIC_DIR':
musicDir = content
# recursive function to go through sub-directories
def test_for_music(directory):
dirList = os.listdir(directory)
for fname in dirList:
filepath = os.path.join(directory, fname)
if os.path.isdir(filepath):
test_for_music(filepath)
else:
filetype = commands.getstatusoutput('file -b "' +filepath + '"')
if "audio" in filetype[1]:
# audio file present
sys.exit(0)
# removes extra characters around directory
musicDir = musicDir[21:]
musicDir = musicDir[:-2]
music = os.path.join(homeDir, musicDir)
test_for_music(music)
#user has no music
sys.exit(1)
I hope that the Rhythmbox script can be expanded to include more music players in the future, but I think it is a good start to work with the default music player in Ubuntu. As I worked on both of these items I made several mistakes, but was guided to success by Rafal Cieślak. It was an amazingly easy and rewarding process. This project has finally allowed me to realize my goal of contributing code to the Ubuntu community. If you are looking to get involved in a community project and know a little bit of Python I would recommend Ubuntu Accomplishments.