Advertisement
Guest User

johnmendel

a guest
Sep 2nd, 2009
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. diff -rupN sonata/artwork.py sonata-patched/artwork.py
  2. --- sonata/artwork.py 2009-09-02 02:23:48.000000000 -0700
  3. +++ sonata-patched/artwork.py 2009-09-02 02:24:02.000000000 -0700
  4. @@ -9,10 +9,7 @@ import img, ui, misc, mpdhelper as mpdh
  5. from library import library_set_data
  6. from library import library_get_data
  7. from consts import consts
  8. -
  9. -AMAZON_KEY = "12DR2PGAQT303YTEWP02"
  10. -AMAZON_NS = "{http://webservices.amazon.com/AWSECommerceService/2005-10-05}"
  11. -AMAZON_URI = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=%s&Operation=ItemSearch&SearchIndex=Music&Artist=%s&ResponseGroup=Images"
  12. +from pluginsystem import pluginsystem
  13.  
  14. class Artwork(object):
  15. def __init__(self, config, find_path, is_lang_rtl, info_imagebox_get_size_request, schedule_gc_collect, target_image_filename, imagelist_append, remotefilelist_append, notebook_get_allocation, allow_art_search, status_is_play_or_pause, album_filename, get_current_song_text):
  16. @@ -529,71 +526,38 @@ class Artwork(object):
  17. return False
  18.  
  19. def artwork_download_img_to_file(self, artist, album, dest_filename, all_images=False):
  20. - # Returns False if no images found
  21. - if not artist and not album:
  22. - self.downloading_image = False
  23. - return False
  24. self.downloading_image = True
  25. - # Amazon currently doesn't support utf8 and suggests latin1 encoding instead:
  26. - artist = urllib.quote(artist.encode('latin1', 'replace'))
  27. - album = urllib.quote(album.encode('latin1', 'replace'))
  28. -
  29. - # Try searching urls from most specific (artist, title) to least specific (artist only)
  30. - urls = [AMAZON_URI % (AMAZON_KEY, artist) + "&Title=" + album,
  31. - AMAZON_URI % (AMAZON_KEY, artist) + "&Keywords=" + album,
  32. - AMAZON_URI % (AMAZON_KEY, artist)]
  33. -
  34. - for url in urls:
  35. - request = urllib2.Request(url)
  36. - opener = urllib2.build_opener()
  37. - try:
  38. - body = opener.open(request).read()
  39. - xml = ElementTree.fromstring(body)
  40. - largeimgs = xml.getiterator(AMAZON_NS + "LargeImage")
  41. - except:
  42. - largeimgs = None
  43. + # Fetch covers from amazon.com etc.
  44. + cover_fetchers = pluginsystem.get('cover_fetching')
  45. + imgfound = False
  46. + for _plugin, cb in cover_fetchers:
  47. + ret = cb(self.download_progress, artist, album, dest_filename, all_images)
  48. + if ret:
  49. + imgfound = True
  50. + break # XXX if all_images, merge results...
  51.  
  52. - if largeimgs:
  53. - break
  54. - elif url == urls[-1]:
  55. - self.downloading_image = False
  56. - return False
  57. -
  58. - imgs = misc.iunique(url.text for img in largeimgs for url in img.getiterator(AMAZON_NS + "URL"))
  59. - # Prevent duplicate images in remote art window:
  60. - imglist = list(set(list(imgs)))
  61. -
  62. - if not all_images:
  63. - urllib.urlretrieve(imglist[0], dest_filename)
  64. - self.downloading_image = False
  65. - return True
  66. - else:
  67. - try:
  68. - imgfound = False
  69. - for i, image in enumerate(imglist):
  70. - dest_filename_curr = dest_filename.replace("<imagenum>", str(i+1))
  71. - urllib.urlretrieve(image, dest_filename_curr)
  72. - # This populates Main.imagelist for the remote image window
  73. - if os.path.exists(dest_filename_curr):
  74. - pix = gtk.gdk.pixbuf_new_from_file(dest_filename_curr)
  75. - pix = pix.scale_simple(148, 148, gtk.gdk.INTERP_HYPER)
  76. - pix = self.artwork_apply_composite_case(pix, 148, 148)
  77. - pix = img.pixbuf_add_border(pix)
  78. - if self.stop_art_update:
  79. - del pix
  80. - self.downloading_image = False
  81. - return imgfound
  82. - self.imagelist_append([i+1, pix])
  83. - del pix
  84. - imgfound = True
  85. - self.remotefilelist_append(dest_filename_curr)
  86. - if i == 0:
  87. - self.allow_art_search()
  88. - ui.change_cursor(None)
  89. - except:
  90. - pass
  91. - self.downloading_image = False
  92. - return imgfound
  93. + self.downloading_image = False
  94. + return imgfound
  95. +
  96. + def download_progress(self, dest_filename_curr, i):
  97. + # This populates Main.imagelist for the remote image window
  98. + if os.path.exists(dest_filename_curr):
  99. + pix = gtk.gdk.pixbuf_new_from_file(dest_filename_curr)
  100. + pix = pix.scale_simple(148, 148, gtk.gdk.INTERP_HYPER)
  101. + pix = self.artwork_apply_composite_case(pix, 148, 148)
  102. + pix = img.pixbuf_add_border(pix)
  103. + if self.stop_art_update:
  104. + del pix
  105. + return False # don't continue to next image
  106. + self.imagelist_append([i+1, pix])
  107. + del pix
  108. + self.remotefilelist_append(dest_filename_curr)
  109. + if i == 0:
  110. + self.allow_art_search()
  111. +
  112. + ui.change_cursor(None) # XXX indented twice more?
  113. +
  114. + return True # continue to next image
  115.  
  116. def fullscreen_cover_art_set_image(self, force_update=False):
  117. if self.fullscreenalbumimage.get_property('visible') or force_update:
  118. diff -rupN sonata/main.py sonata-patched/main.py
  119. --- sonata/main.py 2009-09-02 02:23:48.000000000 -0700
  120. +++ sonata-patched/main.py 2009-09-02 02:24:02.000000000 -0700
  121. @@ -59,6 +59,7 @@ from consts import consts
  122. from pluginsystem import pluginsystem
  123. from preferences import *
  124. from config import Config
  125. +import rhapsodycovers
  126.  
  127. import tagedit, artwork, about, scrobbler, info, library, streams, playlists, current
  128. import dbus_plugin as dbus
  129. @@ -230,6 +231,7 @@ class Base(object):
  130.  
  131. # Artwork
  132. self.artwork = artwork.Artwork(self.config, self.find_path, misc.is_lang_rtl(self.window), lambda:self.info_imagebox.get_size_request(), self.schedule_gc_collect, self.target_image_filename, self.imagelist_append, self.remotefilelist_append, self.notebook.get_allocation, self.set_allow_art_search, self.status_is_play_or_pause, self.find_path('sonata-album.png'), self.get_current_song_text)
  133. + self.rhapsodycovers = rhapsodycovers.RhapsodyCovers()
  134.  
  135. # Popup menus:
  136. actions = (
  137. diff -rupN sonata/pluginsystem.py sonata-patched/pluginsystem.py
  138. --- sonata/pluginsystem.py 2009-09-02 02:23:48.000000000 -0700
  139. +++ sonata-patched/pluginsystem.py 2009-09-02 02:24:02.000000000 -0700
  140. @@ -87,7 +87,22 @@ class Plugin(object):
  141.  
  142. def force_loaded(self):
  143. return bool(self._get_module())
  144. -
  145. +
  146. +class BuiltinPlugin(Plugin):
  147. + def __init__(self, name, longname, description, capabilities, object):
  148. + self.name = name
  149. + self.longname = longname
  150. + self.description = description
  151. + self._capabilities = capabilities
  152. + self._module = object
  153. + self.version_string = "Built-in"
  154. + self.author = self.author_email = self.url = ""
  155. + self.iconurl = None
  156. + self._enabled = True
  157. +
  158. + def _get_module(self):
  159. + return self._module
  160. +
  161. class PluginSystem(object):
  162. def __init__(self):
  163. self.plugin_infos = []
  164. Binary files sonata/pluginsystem.pyc and sonata-patched/pluginsystem.pyc differ
  165. Binary files sonata/preferences.pyc and sonata-patched/preferences.pyc differ
  166. diff -rupN sonata/rhapsodycovers.py sonata-patched/rhapsodycovers.py
  167. --- sonata/rhapsodycovers.py 1969-12-31 16:00:00.000000000 -0800
  168. +++ sonata-patched/rhapsodycovers.py 2009-09-02 02:24:02.000000000 -0700
  169. @@ -0,0 +1,61 @@
  170. +import os
  171. +import urllib
  172. +import urllib2
  173. +from xml.etree import ElementTree
  174. +
  175. +from pluginsystem import pluginsystem, BuiltinPlugin
  176. +
  177. +class RhapsodyCovers(object):
  178. + def __init__(self):
  179. + pluginsystem.plugin_infos.append(BuiltinPlugin(
  180. + 'rhapsodycovers', "Rhapsody Covers",
  181. + "Fetch album covers from Rhapsody.com.",
  182. + {'cover_fetching': 'get_cover'}, self))
  183. +
  184. + def _sanitize_query(self, str):
  185. + return str.replace(" ", "").replace("'", "").replace("&","")
  186. +
  187. + def get_cover(self, progress_callback, artist, album, dest_filename,
  188. + all_images=False):
  189. + return self.artwork_download_img_to_file(progress_callback, artist, album, dest_filename, all_images)
  190. +
  191. + def artwork_download_img_to_file(self, progress_callback, artist, album, dest_filename, all_images=False):
  192. + if not artist and not album:
  193. + return False
  194. +
  195. + rhapsody_uri = "http://feeds.rhapsody.com"
  196. + url = "%s/%s/%s/data.xml" % (rhapsody_uri, artist, album)
  197. + url = self._sanitize_query(url)
  198. + request = urllib2.Request(url)
  199. + opener = urllib2.build_opener()
  200. + try:
  201. + print "in here"
  202. + body = opener.open(request).read()
  203. + xml = ElementTree.fromstring(body)
  204. + imgs = xml.getiterator("img")
  205. + except:
  206. + return False
  207. +
  208. + imglist = [img.attrib['src'] for img in imgs if img.attrib['src']]
  209. + print imglist
  210. + # Couldn't find any images
  211. + if not imglist:
  212. + return False
  213. +
  214. + if not all_images:
  215. + urllib.urlretrieve(imglist[0], dest_filename)
  216. + return True
  217. + else:
  218. + try:
  219. + imgfound = False
  220. + for i, image in enumerate(imglist):
  221. + dest_filename_curr = dest_filename.replace("<imagenum>", str(i+1))
  222. + urllib.urlretrieve(image, dest_filename_curr)
  223. + if not progress_callback(
  224. + dest_filename_curr, i):
  225. + return imgfound # cancelled
  226. + if os.path.exists(dest_filename_curr):
  227. + imgfound = True
  228. + except:
  229. + pass
  230. +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement