Поиск текста песни
- ///autor HoldFast
- function ucwords(str) {
- return str.replace(/^(.)|\s(.)/g, function ($1) {
- return $1.toUpperCase();
- });
- }
- function empty(mixed_var) {
- return (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || (is_array(mixed_var) && mixed_var.length === 0));
- }
- var lyrics = {
- text: function (data) {
- var keys = [];
- for (var key in data.query.pages) keys.push(key);
- if ('revisions' in data.query.pages[keys[0]]) {
- try {
- var text = data.query.pages[keys[0]].revisions['0']['*'];
- text = text.split('<lyrics>');
- text = text[1].split('</lyrics>');
- return $.trim(text[0]);
- } catch (err) {
- return '';
- }
- } else return '';
- },
- getartist: function (artist, func) {
- $.getJSON('http://lyrics.wikia.com/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=' + ucwords(artist) + '&callback=?', func);
- },
- redirect: function (data) {
- var keys = [];
- for (var key in data.query.pages) keys.push(key);
- try {
- var text = data.query.pages[keys[0]].revisions['0']['*'];
- text = text.match(/#REDIRECT \[\[(.*)\]\]/)[1];
- } catch (err) {
- text = '';
- }
- return text;
- },
- get: function (artist, title, func) {
- $.getJSON('http://lyrics.wikia.com/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=' + ucwords(artist) + ':' + ucwords(title) + '&callback=?', func);
- },
- gets: function (string, func) {
- $.getJSON('http://lyrics.wikia.com/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=' + string + '&callback=?', func);
- }
- }
- function getTextWikia(artist, title, func) {
- lyrics.get(artist, title, function (data) {
- var redirect = lyrics.redirect(data);
- var text = "";
- if (redirect != "") {
- lyrics.gets(redirect, function (data) {
- text = lyrics.text(data);
- if (!empty(text)) func(text);
- else func('');
- })
- } else {
- text = lyrics.text(data);
- if (empty(text)) {
- lyrics.getartist(artist, function (data) {
- var redirect = lyrics.redirect(data);
- if (redirect != "") {
- lyrics.get(redirect, title, function (data) {
- text = lyrics.text(data);
- if (!empty(text)) func(text);
- else func('');
- });
- } else func('');
- });
- } else func(text);
- }
- });
- }
Для работы необходим jquery.
Пример:
Пример:
- getTextWikia('Ария','Штиль',function(data) {
- console.log(data);
- });