| Class | MusicExtras::MusicSite |
| In: |
lib/musicextras/musicsite.rb
|
| Parent: | Object |
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.
| 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 |
| 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 |
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.
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
Extracts text from an html page and returns it as utf-8 without the html tags.
Downloads a page, given a url. Returns the contents, or nil if there was a problem. Uses a 7 day file cache.
Compares two strings using String.mangle
Returns true or false