Class MusicExtras::MusicSite
In: lib/musicextras/musicsite.rb
Parent: Object

Writing musicsite plugins

Adding new site plugins should be fairly straight forward. I’ll use parts of the AllMusic plugin as an example.

  class AllMusic < MusicSite

  NAME = 'AllMusic'
  URL = 'www.allmusic.com'
  DESCRIPTION = 'Lots of information and images for many artists'

  def initialize
    super(NAME, URL)

    Artist::register_plugin(self, :image, '{ARTIST}/{ARTIST}.img')
    Album::register_plugin(self, :cover, '{ARTIST}/covers/{ALBUM}.img')
  end

The first parameter to super should be a meaningful name for the plugin (without spaces). The second parameter should be the base url for the site. NAME should be a meaningful name for the plugin (without spaces). URL should be the base url for the site. DESCRIPTION should be a brief description of what the site provides (and the language for the lyrics, if not in English)

The last two lines register a method with an aggregator. So once the first line is registered, the image method in your plugin will be called when artist.image is called. So your image method should return the binary data for the image. The third line is where the information is cached on the filesystem. See the Artist.cache_values, Album.cache_values, and Song.cache_values for the available {} variables. You may make up any cache path you want, but it should make sense, and stay consistent with the other plugins.

To get your plugin registered with the list of other plugins, make sure to call the class method register() somewhere outside of a method definition. To have your plugin getloaded automatically, add it to musicextras/musicsites/_load_sites.rb

Use the available plugins as an example. There are some helper functions available to you from inheriting this class so check out the code below.

NOTE: Plugins should return text as UTF-8. extract_text() will do the conversion for you, but you must do it yourself if you do not call extract_text(). You may use String#to_utf8 for this.

Methods

activate_plugins   extract_text   fetch_page   match?   new   plugins   register   run_tests   source   test   to_s   update  

Included Modules

Debuggable

Classes and Modules

Class MusicExtras::MusicSite::InvalidPlugin

Constants

USERAGENTS = { 'Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225', 'Opera' => 'Opera/6.0 (Linux 2.4.18 i686; U) [en]', 'Lynx' => 'Lynx/2.8.4dev.16 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6-beta3'   Useragents you can use if your plugin needs it
CACHE_PATH = { 'lyrics' => '{ARTIST}/{TITLE}.lyrics', 'synced_lyrics' => '{ARTIST}/{TITLE}.lrc', 'biography' => '{ARTIST}/biography.txt', 'artist_image' => '{ARTIST}/{ARTIST}.img', 'years_active' => '{ARTIST}/years_active.txt', 'album_cover' => '{ARTIST}/covers/{ALBUM}.img', 'album_review' => '{ARTIST}/reviews/{ALBUM}.txt', 'album_tracks' => '{ARTIST}/tracks/{ALBUM}.txt', 'album_year' => '{ARTIST}/years/{ALBUM}.txt'   Cache paths that plugins should use
NAME = "RedefineNAME"
URL = "RedefineURL"
DESCRIPTION = "RedefineDESCRIPTION"
CACHE_LIFE = 60*60*24*7

Attributes

description  [R]  Store a description for the plugin
name  [R]  Stores the name of the plugin
url  [R]  Stores the url of page for the plugin

Public Class methods

Activates plugins. Optionally, plugins can specify an Array of plugin names to be loaded. This can be retrieved from MusicSite.plugins

Creates a new music site. All plugins should call super so this is ran.

name
A meaningful name for the site (without spaces)
url
The base url of the site (eg: www.allmusic.com)

Returns list of available plugins

Register plugin with list of available plugins

Search config[‘updateurl’] remote site for any plugin updates, saving the updates to the search path if any updates are found Returns the number of plugins updated

Public Instance methods

Extracts text from an html page and returns it as utf-8 without the html tags.

page
A String containing the html
regexp
A Regexp with only one (), that should contain the text Make sure to include /m at the end for multiple lines
encoding
Encoding of text. Defaults to iso-8859-1

Downloads a page, given a url. Returns the contents, or nil if there was a problem. Uses a 7 day file cache.

url
url to download (@url should contain the hostname)
post
post data to send (defaults to nil)
useragent
optional useragent string to pass to server

Compares two strings using String.mangle

+str1+
the first String
+str2+
the second String
remove_pronouns
set to true if pronouns should be removed. defaults to false.

Returns true or false

Returns a string of the form: Source: SiteName [URL]

If plugin passes, returns an array of [true, []] If plugin failes, returns an array of [false, msgs] where msgs is an array of strings describing the failed tests If plugin doesn’t implement tests, don’t override this

Prints in the following format: ‘MusicSite: (url)’

[Validate]