Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # main.py
- # Copyright (C) Ilya Koltsov a.k.a Ingwar 2010 <fetfrum@gmail.com>
- #
- # Rhythmbox-helper is free software: you can redistribute it and/or modify it
- # under the terms of the GNU General Public License as published by the
- # Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # Rhythmbox-helper is distributed in the hope that it will be useful, but
- # WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- # See the GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along
- # with this program. If not, see <http://www.gnu.org/licenses/>.
- import dbus
- import string
- import urllib
- import os
- import sys
- import argparse
- scover = "/home/igor/.conky/pix/cover.png"
- defaul_album = "/home/igor/.conky/pix/default_album.png"
- pixdir = "/home/igor/.conky/pix/covers/"
- def ftrack_info(metadata):
- track_info = {}
- track_info['stop']=0
- if metadata["mimetype"] == "application/octet-stream":
- track_info["radio"] = True
- if "rb:stream-song-title" in metadata:
- Stream = metadata["rb:stream-song-title"]
- elif "title" in metadata:
- Stream = metadata["title"]
- else:
- Stream = "Неизвестный поток"
- #Stream="Рок-архив: Kingdom Come - Living Out Of Touch"
- track_info["channel"] = metadata["title"].encode("utf-8")
- if Stream.count(": ")>0:
- titles = Stream.split(": ")
- track_info["program"] = titles[0]
- Stream=titles[1].strip()
- else:
- track_info["program"]=""
- if Stream.count(" - ")>0:
- titles=Stream.split(" - ")
- track_info["artist"] = titles[0].encode("utf-8")
- track_info["title"] = titles[1].encode("utf-8")
- if track_info["title"].count("(")>0:
- titles=track_info["title"].split("(")
- track_info["title"] = titles[0]
- track_info["other"] = titles[1]
- else:
- track_info["other"]=""
- track_info["album"] = get_album(track_info)
- else:
- track_info["title"] = Stream
- track_info["artist"]= ""
- track_info["album"]="" #get_album(track_info)
- else:
- track_info["album"] = metadata["album"]
- track_info["title"] = metadata["title"]
- track_info["artist"] = metadata["artist"]
- track_info["radio"]=False
- track_info["channel"]=""
- track_info["program"]=""
- metadata["location"]=metadata["location"].decode("utf-8").encode("utf-8")
- track_info["location"] = urllib.unquote(metadata["location"])
- if track_info["location"].startswith(r"file://"):
- track_info["location"] = track_info["location"].replace(r"file://","")
- track_info["location"] = string.split(track_info["location"],r"/")
- track_info["location"][-1]=""
- track_info["location"] = string.join(track_info["location"],r"/")
- track_info["other"]=""
- return track_info
- def get_cover(track_info):
- if track_info["radio"]==True :
- track_info["location"]=pixdir+track_info["title"]+"-"+track_info["artist"]+"-"
- url_lastfm="http://www.lastfm.ru/music/"+track_info["artist"]+"/"+track_info["album"]
- url_lastfm=url_lastfm.replace(" ", "+")
- lastfm = urllib.urlopen (url_lastfm)
- for slastfm in lastfm.readlines():
- if slastfm.find("width=\"174\"")>0:
- url=slastfm.split("\"",4)[3]
- if url.find("noimage")>=0:
- location_cover=defaul_album
- break
- location_cover = track_info["location"]+"cover.png"
- cover = urllib.urlopen (url, 'rb')
- fcover=file(location_cover,'wb')
- fcover.write(cover.read())
- break
- else:
- location_cover = track_info["location"]+"cover.png"
- return location_cover
- def get_album(track_info):
- url_lastfm=r"http://www.lastfm.ru/music/"+track_info["artist"].replace(" ", "+")+r"/_/"+track_info["title"].replace(" ", "+")
- lastfm = urllib.urlopen (url_lastfm)
- for slastfm in lastfm.readlines():
- if slastfm.find("featuredAlbum")>=0:
- album=slastfm.split("\"",3)[1]
- album=album.split("/",3)[3]
- album=album.replace("+", " ")
- album=urllib.unquote(album)
- break
- else:
- album="Неизвестный альбом"
- return album
- def print_options(track_info):
- parser = argparse.ArgumentParser(description='Программа выдает некоторые данные из недр Rhythmbox\'а')
- parser.add_argument('-t', '--title', action='store_const', const=True, default=False,
- help='Показать строку с названием песни')
- parser.add_argument('-b', '--artist', action='store_const', const=True, default=False,
- help='Показать строку с именем исполнителя')
- parser.add_argument('-a', '--album', action='store_const', const=True, default=False,
- help='Показать строку с названием альбома')
- parser.add_argument('-p', '--program', action='store_const', const=True, default=False,
- help='Показать строку с названием потока')
- parser.add_argument('-c', '--channel', action='store_const', const=True, default=False,
- help='Показать строку с названием программы')
- parser.add_argument('-o', '--other', action='store_const', const=True, default=False,
- help='Показать строку с названием программы')
- args = parser.parse_args()
- args_dict={ 'title': args.title,
- 'artist': args.artist,
- 'album': args.album,
- 'channel': args.channel,
- 'other': args.other,
- 'program': args.program}
- if len(sys.argv[1:])>0:
- if track_info["stop"]==0:
- for id3tag in args_dict:
- if args_dict[id3tag]:
- print track_info[id3tag]
- else:
- if args_dict["title"]:
- print "Ничего не играет"
- sys.exit (0)
- def main():
- bus = .SessionBus()
- rbox = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
- rboxs = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
- rboxp = bus.get_object( 'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/PlaylistManager')
- player = dbus.Interface(rbox, 'org.gnome.Rhythmbox.Player')
- shell = dbus.Interface(rboxs, 'org.gnome.Rhythmbox.Shell')
- playlist = dbus.Interface(rboxp, 'org.gnome.Rhythmbox.PlaylistManager')
- track_info={}
- fcover=defaul_album
- uri = player.getPlayingUri()
- if len(uri)>0:
- metadata = shell.getSongProperties(uri)
- track_info = ftrack_info(metadata)
- print_options(track_info)
- if track_info["radio"]:
- fcover=pixdir+track_info["title"]+"-"+track_info["artist"]+"-"+"cover.png"
- if os.path.isfile(fcover)==False:
- if track_info["album"]<>"":
- fcover = get_cover(track_info)
- else:
- fcover=track_info["location"]+"cover.png"
- if os.path.isfile(fcover)==False and len(uri):
- fcover = get_cover(track_info)
- else:
- track_info["stop"]=1
- print_options(track_info)
- if os.path.islink(scover):
- if os.readlink(scover)<>fcover and os.path.isfile (fcover)==True:
- os.unlink(scover)
- os.symlink(fcover, scover)
- elif os.path.isfile (fcover)==True:
- os.symlink(fcover, scover)
- else:
- os.symlink(defaul_album, scover)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement