Skip to content

Instantly share code, notes, and snippets.

@dsingleton
Created April 17, 2011 21:23
Show Gist options
  • Save dsingleton/924488 to your computer and use it in GitHub Desktop.
Save dsingleton/924488 to your computer and use it in GitHub Desktop.
// javascript:(function(src){document.body.appendChild(document.createElement('script')).setAttribute('src', src);})("https://gist.github.com/raw/924488/twttr.media.types.Lastfm.js");
// Simulate self-executed scope in phoenix.js, Replicate A (ajax/xdm var in compressed version)
(function(A) {
twttr.mediaType("twttr.media.types.Lastfm", {
icon: "song",
domain: "http://www.last.fm",
matchers: {
track: /^#{optional_protocol}?www\.last\.fm\/music\/([^/]+\/[^/]+\/[^/]+)/i,
album: /^#{optional_protocol}?www\.last\.fm\/music\/([^/]+\/[^/]+)/i,
artist: /^#{optional_protocol}?www\.last\.fm\/music\/([^/]+)/i,
},
process: function (B) {
if (twttr.media.types.Lastfm.renderMode == 'iframe') {
this.matchLabel = 'iframe';
}
else {
this.slug = decodeURIComponent(this.slug).replace(/\+/g, " ");
}
this[this.matchLabel](B);
},
render: function (target) {
if (this.data && this.data.name) {
var content = twttr.util.supplant(twttr.media.types.Lastfm.templates[this.matchLabel], this.data);
$(target).append(content);
}
}
}).statics({
renderMode: 'iframe',
API_ROOT: 'http://ws.audioscrobbler.com/2.0/',
API_KEY: 'b25b959554ed76058ac220b7b2e0a026',
templates: {
iframe: '<div class="lastfm"><iframe src="{src}" width="441" height="70"></iframe></div>',
artist: '<div class="lastfm" style="clear: right"> <a href="{link}"> <img src="{image}" style="float: right; margin: 0 0 5px 10px" /> <strong style="color: #D51007; font-size: 14px">{name}</strong> </a> <p class="bio">{bio}</p> </div>',
album: '<div class="lastfm" style="clear: right"> <a href="{link}"> <img src="{image}" style="float: right; margin: 0 0 5px 10px" /> <strong style="color: #D51007; font-size: 14px">{artist} - {name}</strong> </a></div>',
track: '<div class="lastfm" style="clear: right"> <a href="{link}"> <img src="{image}" style="float: right; margin: 0 0 5px 10px" /> <strong style="color: #D51007; font-size: 14px">{artist} - {name}</strong> </a></div>'
}
}).methods({
iframe: function(B) {
this.data.name = 'foo';
this.data.src = 'http://davids.embeds.dev.last.fm/tests/embed?url=' + encodeURIComponent(this.url);
B();
},
artist: function(B) {
this.api(
'artist.getInfo',
{artist: this.slug},
twttr.bind(this, function (response) {
this.data.name = response.artist.name;
this.data.link = response.artist.url;
this.data.image = response.artist.image[3]['#text'];
this.data.bio = response.artist.bio.summary;
B();
})
);
},
album: function(B) {
this.api(
'album.getInfo',
{artist: this.slug.split('/')[0], album: this.slug.split('/')[1]},
twttr.bind(this, function (response) {
console.log(response);
this.data.name = response.album.name;
this.data.artist = response.album.artist;
this.data.link = response.album.url;
this.data.image = response.album.image[2]['#text'];
B();
})
);
},
track: function(B) {
this.api(
'track.getInfo',
{artist: this.slug.split('/')[0], track: this.slug.split('/')[2]},
twttr.bind(this, function (response) {
this.data.name = response.track.name;
this.data.artist = response.track.artist.name;
this.data.link = response.track.url;
if (response.track.album) {
this.data.image = response.track.album.image[2]['#text'];
}
B();
})
);
},
api: function(method, params, success, error) {
A({
url: twttr.media.types.Lastfm.API_ROOT,
dataType: "jsonp",
success: success,
error: error,
data: twttr.merge(params, {
api_key: twttr.media.types.Lastfm.API_KEY,
format: 'json',
method: method
})
});
}
});
})($.ajax);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment