screenlets.plugins.CoverSearch

Package screenlets :: Package plugins :: Module CoverSearch |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.CoverSearch

     1  # This program is free software: you can redistribute it and/or modify
     2  # it under the terms of the GNU General Public License as published by
     3  # the Free Software Foundation, either version 3 of the License, or
     4  # (at your option) any later version.
     5  #
     6  # This program is distributed in the hope that it will be useful,
     7  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     9  # GNU General Public License for more details.
    10  #
    11  # You should have received a copy of the GNU General Public License
    12  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    13
    14
    15  from AmazonCoverArtSearch import AmazonCoverArtSearch
    16  from Loader import Loader
    17  import os
    18
    19  import threading
    20  import time
    21
    22 -class CoverSearch (threading.Thread):
    23          loader = None
    24          Result = False
    25          artist = ""
    26          album = ""
    27          callback_fn = False
    28
    29          AlbumCover = "/tmp/nowplaying-album.jpg"
    30
    31 -        def __init__(self):
    32                  self.loader = Loader()
    33                  self.engine = AmazonCoverArtSearch(self.loader)
    34                  self.Result = False
    35                  threading.Thread.__init__(self)
    36
    37 -        def initData(self, artist, album, fn):
    38                  self.artist = artist
    39                  self.album = album
    40                  self.callback_fn = fn
    41
    42 -        def saveimg(self, data):
    43                  fobj = open(self.AlbumCover,"w")
    44                  fobj.write(data)
    45                  fobj.close()
    46                  self.Result = True
    47
    48 -        def cb(self, itm, artist, album, result, *args):
    49                  data = self.engine.get_best_match_urls(result)
    50                  if data and self.artist == artist and self.album == album:
    51                          #print data[0]
    52                          self.loader.get_url(data[0], self.saveimg)
    53
    54 -        def run(self):
    55                  if os.path.exists(self.AlbumCover): os.remove(self.AlbumCover)
    56                  self.Result = False
    57                  self.engine.search (self.artist, self.album, self.cb)
    58                  while True:
    59                          if self.Result:
    60                                  break
    61                          if not self.engine.search_next ():
    62                                  break
    63
    64                  cover = False
    65                  if os.path.exists(self.AlbumCover):
    66                          cover = self.AlbumCover
    67
    68                  #print threading.currentThread()
    69                  self.callback_fn(cover)
    70                  return None
    71