Difference between revisions of "MediaWiki:Gadget-site.js"

From CaiyunWiki
Jump to: navigation, search
Line 76: Line 76:
 
         }
 
         }
 
         result = result + "</div>";
 
         result = result + "</div>";
         return result;
+
         $('.poetry').html(result);
 
     }
 
     }
 
}
 
}

Revision as of 08:16, 31 August 2018

//  _________________________________________________________________________________________
// |                                                                                         |
// |                    === WARNING: GLOBAL GADGET FILE ===                                  |
// |                  Changes to this page affect many users.                                |
// | Please discuss changes on the talk page or on [[Wikipedia_talk:Gadget]] before editing. |
// |_________________________________________________________________________________________|
//

function translate(texts, func, direction) {
	direction = direction || 'en2zh';

	var url = "https://api.interpreter.caiyunai.com/v1/translator";

	var headers = {
		"content-type": "application/json",
		"X-Authorization": "token o4msd3dw3q6rh3bcdrm1",
	};

	var replaced_config = {
		"per": {"replaced": true, "replaced_before_rnn": false},
		"num": {"replaced": false, "replaced_before_rnn": false},
		"loc": {"replaced": false, "replaced_before_rnn": false},
		"noun": {"replaced": false, "replaced_before_rnn": false},
		"addition": {"replaced": true, "replaced_before_rnn": false}
	};

	var payload = {
		"source": texts,
		"trans_type": direction, "cached": false,
		"replaced_config": replaced_config,
		"request_id": "web-translate",
		"url": 'https://apidocs.caiyunapp.com'
	};

	$.post({
		"url": url,
		"headers": headers,
		"data": JSON.stringify(payload),
		"dataType": "json",
		"success": function (data, textStatus, jqXHR) {
			var translations = data["target"];
			try {
				func(translations);
			} catch (e) {
				console.log(e);
				//throw e; // intentionally re-throw (caught by window.onerror)
			}
		},
	});
}

var skyconword = {
    'RAIN': 'rain',
    'SNOW': 'snow',
    'HAZE': 'fog',
    'WIND': 'wind',
    'CLOUDY': 'cloudy',
    'PARTLY_CLOUDY_DAY': 'partly cloudy',
    'PARTLY_CLOUDY_NIGHT': 'partly cloudy',
    'CLEAR_DAY': 'clear',
    'CLEAR_NIGHT': 'clear',
};

function buildTianqi(placename, skycon, temp) {
    var desc = skyconword[skycon];
    return "<link href='https://fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'><div class='card'><div class='city'>" + placename + "</div><div class='temp'>" + temp + "&#176;C</div><div class='skycon'>" + desc + "</div></div>";
}

function poetryBuilder(lines) {
    return function(translations) {
        var result = "<link href='https://fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'><div class='card'>";
        for(var idx in translations) {
            var o = lines[idx];
            var t = translations[idx];
            result = result + "<div>" + o + "</div>" + "<div>" + t + "</div>"; 
        }
        result = result + "</div>";
        $('.poetry').html(result);
    }
}

var latlng = $('.tianqi').text();
var pair = latlng.split(',');
var lat = pair[0], lng = pair[1];

var url = 'https://api.caiyunapp.com/v2/5AoOwKgLgmqmy=2i/' + lng + ',' + lat + '/weather.jsonp';

$.ajax({
    "url": url,
    "jsonp": "callback",
    "dataType": "jsonp",
    "data": {
        "getplacename": "True",
        "lang": "en"
    },
    "success": function( response ) {
        $('.tianqi').html(buildTianqi(response.placename, response.result.realtime.skycon, response.result.realtime.temperature));
    }
});


var url = 'http://apidocs.caiyunapp.com/poetry';
$.ajax({
    "url": url,
    "dataType": "json",
    "success": function( response ) {
        translate(response, poetryBuilder(response));
    }
});