This started off as a python learning task. Then I found a nice library (libgmail) for accessing data in Gmail accounts. So I decided to take it a step further and write GMusic.
It basically allows you to store your mp3 (or other format audio files) onto your gmail account. After which you can use GMusic to access and play your songs anywhere that has an internet connection.
Due to large capacity of GMail accounts, you should be able to store upto 500 songs. YMMV.
- Spare GMail account.
- Python 2.4+
- OS: I believe it should work on any OS that python support. I have tested it on Linux and Mac OS X.
- All other dependencies are included in the tarball.
- GMusic uses libgmail to communicate with the GMail server, using the account information you provide.
- Once logged in the state is maintain, so you dont have to login every time you access the account.
- The application consists of 4 subsystems:
- Upload: upload.py
- GUI/Frontend: playlist.cgi, player.cgi, refresh.html
- Backend: gmusic.py, cgiserver.py
- Player: mplayer.py
- The communication between the frontend and backend is done using named pipes (mkfifo) and external files.
- Commands to the backend system are sent using the pipes. The backend generates the results as text/html file (e.g. refresh.html and .dbfile).
- These files are used by the frontend to display the results.
- I have included a script that stores the mp3 files in GMail.
- It takes a root directory that contains all the mp3s.
- The data is organized as playlist. So a group of songs is a playlist.
- The playlist are essentially labels in GMail.
- The immediate up-level directory name that contains the mp3s will become the label (playlist name) for those mp3s. The name of the directory that contains the above dir. is not included in the label name.
- All messages are stored as drafts, hence there is no need to send the email.
- The Subject of the drafts are the filename. So there is 1 draft for each song.
- You should split the upload into smaller chunks. Else your account will be locked for 24hrs.
- Also there is not a lot of exception handling in this script so it will better if you do the upload few files at a time (maybe 50 or so).
- For the GUI it uses the browser.
- Basically, running a simple CGI capable server, which is readily provided by python as CGIHTTPServer module.
- The webpage consists of 3 sections:
- playlist.cgi - Displays the information from the text DB file.
- player.cgi - Provides button to issue commands to the backend system.
- refresh.html - HTML status report of the system, generated by the backend system and refreshed using html meta tags.
- It collects information from the GMail account on the songs that are stored there and the associated labels.
- The retrieved information is stored in a text file. It usually takes a long time to get info from GMail, hence its stored for later use and also refresh the DB on user demand. Since the data isnt very large, text file serves the purpose of a DB.
- There is no streaming support. So you have to download a song in its entirety and then play it.
- There is a thread running which downloads a few songs in advance when a song is being played. So normally there is no delay in playing songs except for the first song.
- Another thread keeps track of the status as to what file is being played etc.
- It accepts simple commands to do its works. The command are given by the frontend, based on user input. You can also give commands for testing using echo, e.g.
echo PLAY > /tmp/gmusicCmdIn
Where /tmp/gmusicCmdIn is the named pipe on which GMusic listens.
- ADD - Adds a song or a playlist of songs to the queue.
- DELETE - Removes a song or a playlist from the queue.
- ADD/DELETE songname.mp3 OR
- ADD/DELETE -playlist songs.m3u
- PLAY - Starts playing the songs in the queue.
- STOP - Stops the song playing. The song will start from the beginning after this.
- PAUSE - Pause the song.
- NEXT - Play the next song in the queue.
- PREV - Play the prev song in the queue.
- REFRESH_DB - Retrieve the song information from GMail and update the DB file.
- REFRESH_STATUS - Info. is updated on the webpage.
- DEBUG - some debugging info is printed on stdout.
- The player is Mplayer.
- Again the communication is done using pipes.
- Mplayer has the ability to listen on a pipe for commands.
mplayer -input file=/tmp/mplayerIn song.mp3
- The backend uses a python wrapper to control the mplayer. So, there is no need to run the above command.
- Install mplayer and python if you havent already done so.
- Untar the package into a directory. All the operations occur from this directory, temporary files are also stored here.
- All files are stored in a directory called gmusic.
- Run the upload script on a dir. containing the songs.
- Its better to run the upload script as a background process, since it normally takes a long time to upload 2+ GB of data onto GMail. Especially from a home connection.
- The first level sub-directory names will be used to create labels in GMail.
- Once the songs are uploaded check your GMail account for correctness.
- Create the named pipes using mkfifo.
- Edit gmusic.py to change the username and passwd of your GMail account.
- Run cgiserver.py as a background process
./cgiserver.py >/dev/null 2>&1 &
- Run ./gmusic.py in background
./gmusic.py >/dev/null 2>&1 &
- Goto http://127.0.0.1:9080/. You should be able to see the GUI with Gmusic ready page refreshing every few secs and a drop down list containing the label names.
- Once you have the system up. Pick a playlist from the dropdown list and select the songs you want to add or delete.
- The song queue is updated based on the information provided.
- The Added songs should show up in the right panel.
- You can add or delete songs from multiple playlists by repeating the above steps.
- There is no error or warning if the deleted songs dont exist.
- Hit play to start the playback.
- If the song has already been downloaded it will start playing shortly. Else the status will indicate that the song is being downloaded.
- Changes to the queue can be made while a song is being played.
- Try running the application in foreground.
- cgiserver.py should be listening on port 9080 (check with netstat).
- Make sure you have the pipes created.
- Test the mplayer and the gmusic pipes using echo "cmmand name" > pipename.
Create a text file with some filenames.mp3, name the file myplaylist.m3u.
echo "ADD -playlist myplaylist.m3u" > /tmp/gmusicCmdIn.
The stdout for gmusic.py will show some information as to which song is being downloaded etc.
Check the queue using the DEBUG command. It will output the add and del Queues on the stdout.
echo DEBUG > /tmp/gmusicCmdIn
Once the first song is finished downloading, play the list. You should hear the first song in the list. The stdout will also show the mplayer cmd that is used to play the song. So you can try and run that on the command line to verify in case it doesnt work.
echo PLAY > /tmp/gmusicCmdIn
The above example shows essentially what happens when the GUI is used.
- Add support for multiple GMail accounts.
- Add ID3 tag info.
- Add config file, to read parameters.
- Add socket interface to receive commands (Since mkfifo is not supported for windows.)
Report any bugs to email@example.com.