Advertisement
Guest User

Untitled

a guest
Sep 15th, 2010
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.18 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # main.py
  4. # Copyright (C) Ilya Koltsov a.k.a Ingwar 2010 <fetfrum@gmail.com>
  5. #
  6. # Rhythmbox-helper is free software: you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the
  8. # Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Rhythmbox-helper is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. # See the GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19.  
  20. import dbus
  21. import string
  22. import urllib
  23. import os
  24. import sys
  25. import argparse
  26.  
  27.  
  28.  
  29. scover       = "/home/igor/.conky/pix/cover.png"
  30. defaul_album = "/home/igor/.conky/pix/default_album.png"
  31. pixdir       = "/home/igor/.conky/pix/covers/"
  32.  
  33.  
  34. def ftrack_info(metadata):
  35.     track_info = {}
  36.     track_info['stop']=0
  37.     if metadata["mimetype"] == "application/octet-stream":
  38.         track_info["radio"] = True
  39.         if "rb:stream-song-title" in metadata:
  40.             Stream = metadata["rb:stream-song-title"]
  41.         elif "title" in metadata:
  42.             Stream = metadata["title"]
  43.         else:
  44.             Stream = "Неизвестный поток"
  45.         #Stream="Рок-архив: Kingdom Come - Living Out Of Touch"
  46.         track_info["channel"] = metadata["title"].encode("utf-8")
  47.         if Stream.count(": ")>0:
  48.             titles = Stream.split(": ")
  49.             track_info["program"] = titles[0]
  50.             Stream=titles[1].strip()
  51.         else:
  52.             track_info["program"]=""
  53.         if Stream.count(" - ")>0:
  54.             titles=Stream.split(" - ")
  55.             track_info["artist"] = titles[0].encode("utf-8")
  56.             track_info["title"] = titles[1].encode("utf-8")
  57.             if track_info["title"].count("(")>0:
  58.                 titles=track_info["title"].split("(")
  59.                 track_info["title"] = titles[0]
  60.                 track_info["other"] = titles[1]
  61.             else:
  62.                 track_info["other"]=""
  63.             track_info["album"] = get_album(track_info)
  64.         else:
  65.             track_info["title"] = Stream
  66.             track_info["artist"]= ""
  67.             track_info["album"]="" #get_album(track_info)
  68.     else:
  69.         track_info["album"] = metadata["album"]
  70.         track_info["title"] = metadata["title"]
  71.         track_info["artist"] = metadata["artist"]
  72.         track_info["radio"]=False
  73.         track_info["channel"]=""
  74.         track_info["program"]=""
  75.         metadata["location"]=metadata["location"].decode("utf-8").encode("utf-8")
  76.         track_info["location"] = urllib.unquote(metadata["location"])
  77.         if track_info["location"].startswith(r"file://"):
  78.             track_info["location"] = track_info["location"].replace(r"file://","")
  79.         track_info["location"] = string.split(track_info["location"],r"/")
  80.         track_info["location"][-1]=""
  81.         track_info["location"] = string.join(track_info["location"],r"/")
  82.         track_info["other"]=""
  83.     return track_info
  84.    
  85.    
  86. def get_cover(track_info):
  87.     if track_info["radio"]==True :
  88.         track_info["location"]=pixdir+track_info["title"]+"-"+track_info["artist"]+"-"
  89.     url_lastfm="http://www.lastfm.ru/music/"+track_info["artist"]+"/"+track_info["album"]
  90.     url_lastfm=url_lastfm.replace(" ", "+")
  91.     lastfm = urllib.urlopen (url_lastfm)
  92.     for slastfm in lastfm.readlines():
  93.         if slastfm.find("width=\"174\"")>0:
  94.             url=slastfm.split("\"",4)[3]
  95.             if url.find("noimage")>=0:
  96.                 location_cover=defaul_album
  97.                 break
  98.             location_cover = track_info["location"]+"cover.png"
  99.             cover = urllib.urlopen (url, 'rb') 
  100.             fcover=file(location_cover,'wb')
  101.             fcover.write(cover.read())
  102.             break
  103.         else:
  104.             location_cover = track_info["location"]+"cover.png"
  105.     return location_cover
  106.    
  107. def get_album(track_info):
  108.    
  109.     url_lastfm=r"http://www.lastfm.ru/music/"+track_info["artist"].replace(" ", "+")+r"/_/"+track_info["title"].replace(" ", "+")
  110.     lastfm = urllib.urlopen (url_lastfm)
  111.     for slastfm in lastfm.readlines():
  112.         if slastfm.find("featuredAlbum")>=0:
  113.             album=slastfm.split("\"",3)[1]
  114.             album=album.split("/",3)[3]
  115.             album=album.replace("+", " ")
  116.             album=urllib.unquote(album)
  117.             break
  118.         else:
  119.             album="Неизвестный альбом"
  120.     return album
  121.  
  122.  
  123. def print_options(track_info):
  124.     parser = argparse.ArgumentParser(description='Программа выдает некоторые данные из недр Rhythmbox\'а')
  125.     parser.add_argument('-t', '--title', action='store_const', const=True, default=False,
  126.                    help='Показать строку с названием песни')
  127.     parser.add_argument('-b', '--artist', action='store_const', const=True, default=False,
  128.                    help='Показать строку с именем исполнителя')
  129.     parser.add_argument('-a', '--album', action='store_const', const=True, default=False,
  130.                    help='Показать строку с названием альбома')
  131.     parser.add_argument('-p', '--program', action='store_const', const=True, default=False,
  132.                    help='Показать строку с названием потока')
  133.     parser.add_argument('-c', '--channel', action='store_const', const=True, default=False,
  134.                    help='Показать строку с названием программы')
  135.     parser.add_argument('-o', '--other', action='store_const', const=True, default=False,
  136.                    help='Показать строку с названием программы')
  137.                  
  138.     args = parser.parse_args()
  139.  
  140.     args_dict={ 'title': args.title,
  141.                 'artist': args.artist,
  142.                 'album': args.album,
  143.                 'channel': args.channel,
  144.                 'other': args.other,
  145.                 'program': args.program}
  146.    
  147.     if len(sys.argv[1:])>0:
  148.         if track_info["stop"]==0:
  149.             for id3tag in args_dict:
  150.                 if args_dict[id3tag]:
  151.                     print track_info[id3tag]
  152.         else:
  153.             if args_dict["title"]:
  154.                 print "Ничего не играет"
  155.         sys.exit (0)   
  156.    
  157.  
  158. def main():
  159.     bus =  .SessionBus()
  160.     rbox = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
  161.     rboxs = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
  162.     rboxp = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/PlaylistManager')
  163.     player = dbus.Interface(rbox, 'org.gnome.Rhythmbox.Player')
  164.     shell = dbus.Interface(rboxs, 'org.gnome.Rhythmbox.Shell')
  165.     playlist = dbus.Interface(rboxp, 'org.gnome.Rhythmbox.PlaylistManager')
  166.     track_info={}
  167.     fcover=defaul_album
  168.     uri = player.getPlayingUri()
  169.     if len(uri)>0:
  170.         metadata = shell.getSongProperties(uri)
  171.         track_info = ftrack_info(metadata)
  172.         print_options(track_info)      
  173.         if track_info["radio"]:
  174.             fcover=pixdir+track_info["title"]+"-"+track_info["artist"]+"-"+"cover.png"
  175.             if os.path.isfile(fcover)==False:
  176.                 if track_info["album"]<>"":
  177.                     fcover = get_cover(track_info)
  178.         else:
  179.             fcover=track_info["location"]+"cover.png"
  180.             if  os.path.isfile(fcover)==False and len(uri):
  181.                 fcover = get_cover(track_info)
  182.        
  183.     else:
  184.         track_info["stop"]=1
  185.         print_options(track_info)
  186.    
  187.     if os.path.islink(scover):
  188.         if os.readlink(scover)<>fcover and os.path.isfile (fcover)==True:
  189.             os.unlink(scover)
  190.             os.symlink(fcover, scover)
  191.     elif os.path.isfile (fcover)==True:
  192.         os.symlink(fcover, scover)
  193.     else:
  194.         os.symlink(defaul_album, scover)
  195.    
  196.  
  197. if __name__ == '__main__':
  198.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement