Self-made Douban. FM player in Ubuntu

Source: Internet
Author: User
Tags chrome developer chrome developer tools notification center
ArticleDirectory
    • Simple implementation of Douban. FM player
    • Play Music
    • Ubuntu Desktop notification
    • Summary

Douban. FM is a commonly used network music station, but because it is a Web application, it cannot use global hotkey control, and the official version of Linux Desktop is not provided, so I plan to develop a Linux doufan by myself. FM player.

Functions to be implemented:

    1. Global hotkey Control
    2. Desktop notification when playing
    3. Logon and channel selection

It may not be possible to achieve it, but I will take time to break through the difficulties. So far, basic functions have been simply implemented.

 

Simple implementation of Douban. FM player

Since it is a simple implementation, we should try to keep the implementation as simple as possible to improve the function in the future. On the development tool, I chose python to quickly deliver and test my ideas.

Data Source

How to obtain the music to be played and related information is the first problem to be solved. This requires us to study the workflow of Douban. FM. In fact, we can think about how to implement a network radio station? It is impossible to put all the playlists on the client. Yes, a good solution is to first send a request to the playlist from the client, and the server returns the playlist, the client then requests a file based on the URL in the playlist for playback.

Of course, we can find out the specific situation only after actual analysis. We can use web debugging tools (firebug or chrome Developer Tools), or even capture software to analyze data communication between Douban. FM.

After refreshing, we can see all the HTTP requests sent by Douban. fm. Next, we will look for the HTTP Response carrying the playlist:

From the URL, we can clearly see the playlist text and expand the request response. We can see that the format of the response data is JSON. continue to expand and finally find the desired data.

Next, we will study the URL parameters to customize our player. Open the URL in the new tag:

After several attempts, we can probably determine the behavior of these parameters:

    • Type: It indicates a type. The specific function is unknown. type = N is used for all requests sent by Douban. fm. If this parameter is missing, the playback list cannot be obtained normally;
    • Sid: the role is unknown, saving time. prediction is related to the user;
    • PT: the role is unknown, saving time. Speculation is related to the music type;
    • Chanel: Channel ID. It has been tested and found that 0: Public/Private (when Sid is specified), 1: Chinese,-3: red heart;
    • From: client source, saving;
    • R: From the value point of view, it should be the abbreviation of random, which can be saved;

So the simplest URL for getting a playlist is: http://douban.fm/j/mine/playlist? Type = N & channel = 0

Play Music

How can I use python to play music files? We do not need to download the MP3 file first, and implement another player by ourselves. Even using existing modules or libraries is too troublesome. Why are there so many command line-based streaming media players in Linux not used directly? For example, I often use mplayer.

Use mplayer:MplayerBytes. Then, use the subprocess module of python to call it.

Now you can write out the simplest Douban. fm player:

Httpconnection = httplib. httpconnection ( '  Douban. fm  '  ) Httpconnection. Request (  '  Get  ' , '  /J/mine/playlist? Type = N & channel = 0  '  ) Song = JSON. Loads (httpconnection. getresponse (). Read ())[ '  Song  '  ] Subprocess. Call ([  '  Mplayer  ' , Song [0] [ '  URL  ' ])

It is thrilling that only four lines are used to implement this function.Code.

Ubuntu Desktop notification

Unified desktop notifications make sense (think about the chaotic windows pop-up window), fromOS X v10.8 "mountain lion" started, Mac began to unify the notification center, while Linux desktops were also improving their respective notification systems.

Ubuntu unity has its own desktop notification interface. Open ~ /. Bashrc, you can find such a command alias:

Alias Alert = 'your y-send -- urgency = low-I "$ ([$? = 0] & Echo terminal | echo error) "" $ (History | tail-N1 | sed-e '\'s/^ \ s * [0-9] \ + \ s * //; S /[; & |] \ s * alert $ //'\'')"'

Enter the following command in shell:Alert "test", You can see the following results:

Alert is an alias, and the core command is notify-Send. We can view its usage through man notify-Send.

After the notify plug-in is enabled for the player in Ubuntu, a desktop prompt will be sent every time the player is split:

This is a cool function. How can this function be implemented?

It is not difficult to think about it. images can be downloaded by Song [0] ['picture, then construct the command notify-send-I pictrue_path ['title'] ['artlist'] ['albumtitle. It involves downloading and saving image files. You can use httplib + to read and write files. Actually, wget is used.

Python is used for implementation:

Picture = ' Images/  ' + Song [0] [ '  Picture  ' ]. Split ( '  /  ' ) [4 ]  #  Download album cover  If   Not  OS. Path. exists (picture): subprocess. Call ([  '  Wget '  ,  '  -P  '  ,  '  Images  '  , Song [0] [  '  Picture  '  ])  #  Send desktop notification  Subprocess. Call ([ '  Notify-send  '  ,  '  -I  '  , OS. getcwd () + '  /  ' + Picture, song [0] [  '  Title  '  ], Song [0] [  ' Artist  ' ] + '  \ N  ' + Song [0] [ '  Albumtitle  ' ])

When the image name is retrieved, there is a small hack. Song [0] ['Picture']. Split ('/') [4]This sentence is used to get the image name in the URL. I know it is not good and dangerous, but I still do it (scold me ).

Summary

Finally, the preceding playback code is basically complete. Although global hot key control and logon are not implemented, it is ready for use.

Complete code:

1   #  ! /Usr/bin/Python  2   #  Coding: UTF-8  3   4   Import  Httplib  5   Import  JSON  6   Import  OS  7  Import  Sys  8   Import  Subprocess  9   Import  Time  10   11   Reload (sys)  12 SYS. setdefaultencoding ( '  UTF-8  '  )  13  14   While  True:  15       #  Obtain the playlist  16 Httpconnection = httplib. httpconnection ( '  Douban. fm  '  )  17 Httpconnection. Request ( '  Get  ' , ' /J/mine/playlist? Type = N & channel = 0  '  )  18 Song = JSON. Loads (httpconnection. getresponse (). Read ())[ '  Song  '  ]  19   20 Picture = '  Images/  ' + Song [0] [ '  Picture ' ]. Split ( '  /  ' ) [4 ]  21   22       #  Download album cover  23       If   Not  OS. Path. exists (picture ):  24   Subprocess. Call ([  25               ' Wget  '  ,  26               '  -P  '  ,  27               '  Images  '  ,  28 Song [0] [ '  Picture  '  ]) 29   30       #  Send desktop notification  31   Subprocess. Call ([  32           '  Notify-send  '  ,  33           '  -I  '  ,  34 OS. getcwd () +'  /  ' + Picture,  35 Song [0] [ '  Title  '  ],  36 Song [0] [ '  Artist  ' ] + '  \ N  ' + Song [0] ['  Albumtitle  '  ])  37   38       #  Play  39 Player = subprocess. popen ([ '  Mplayer  ' , Song [0] [ '  URL  '  ])  40 Time. Sleep (song [0] [ '  Length  '  ])  41 Player. Kill ()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.