From 64d3114c3eeb933152732e024b81b2a94c8db87f Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sat, 30 Jun 2012 20:06:25 +0000
Subject: [PATCH] Zmiany w Microsoft Translator, tj: - przeniesienie systemu do Azure Datamarket, - zmiany w API, - nowy klucz, tzw. Account Key.
---
modules/80_lang/msapi.php | 44 ++++++++++++++++++++++
INSTALL | 15 ++++---
class/config.php | 2
UPGRADE | 14 +++++++
modules/80_lang/msapi_config.php | 5 ++
modules/80_lang/handler.php | 21 +++++++---
6 files changed, 87 insertions(+), 14 deletions(-)
diff --git a/INSTALL b/INSTALL
index 9be0c36..006c870 100644
--- a/INSTALL
+++ b/INSTALL
@@ -16,17 +16,18 @@
* wykonuj za pomocą crona polecenie `/sciezka/do/bota/data/update.sh` ok.
20 minut po każdej pełnej godzinie
* uruchom /data/update_all.sh (będąc w katalogu ./data) od razu po instalacji
-* pobierz Application ID ze strony http://www.bing.com/developers/appids.aspx
- (wymaga identyfikatora Windows Live ID), a następnie wpisz go w pliku
- ./modules/80_lang/handler.php w miejsu:
+* pobierz Account Key ze strony https://datamarket.azure.com/
+ (wymaga identyfikatora Windows Live ID), uzyskaj subskrybcję Microsoft Translator:
+ https://datamarket.azure.com/dataset/1899a118-d202-492c-aa16-ba21c33c06cb
+ a następnie Account Key w pliku ./modules/80_lang/msapi_config.php w miejscu:
-class bot_lang_module implements BotModule {
- private $APPID = '';
+class msapi_config {
+ protected $accountKey = '';
które po edycji ma wyglądać tak:
-class bot_lang_module implements BotModule {
- private $APPID = 'A123BC9238...ADD';
+class msapi_config {
+ protected $accountKey = 'A123BC9238...ADD';
------------
diff --git a/UPGRADE b/UPGRADE
index eec6e0d..5f0b035 100644
--- a/UPGRADE
+++ b/UPGRADE
@@ -1,4 +1,18 @@
===========================
+ AKTUALIZACJA Z WERSJI 2.2
+===========================
+
+* usuń zawartość katalogu ./modules/80_lang
+* wgraj ./module/80_lang z paczki
+* uzyskaj Azure Datamarket acoount key ze strony:
+ https://datamarket.azure.com/
+ oraz uzyskaj jakąkolwiek subskrypcję Microsoft Translator pod adresem:
+ https://datamarket.azure.com/dataset/1899a118-d202-492c-aa16-ba21c33c06cb
+* Account Key wpisz w przewidzianym miejscu w pliku
+ ./modules/80_lang/msapi_config.php
+* zastąp plik ./class/legacy/main.php
+
+===========================
AKTUALIZACJA Z WERSJI 2.1
===========================
diff --git a/class/config.php b/class/config.php
index c9061f8..bfb7d08 100644
--- a/class/config.php
+++ b/class/config.php
@@ -13,4 +13,4 @@
),
);
}
-?>
\ No newline at end of file
+?>
diff --git a/modules/80_lang/handler.php b/modules/80_lang/handler.php
index 05d6280..15bd3b0 100644
--- a/modules/80_lang/handler.php
+++ b/modules/80_lang/handler.php
@@ -1,7 +1,7 @@
<?php
+require_once(dirname(__FILE__).'/msapi.php');
+
class bot_lang_module implements BotModule {
- private $APPID = '';
-
function handle($msg, $params) {
$args = trim($msg->args);
@@ -23,13 +23,22 @@
$data = jsarray::parse($data);
if(!$data OR count($data)==0 OR count($data[1])==0) {
- $data = file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId='.urlencode($this->APPID).'&text='.urlencode($args).'&from='.$params[0].'&to='.$params[1]);
+ $api = new msapi('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/');
+ $data = $api->execute(array(
+ 'From' => $params[0],
+ 'To' => $params[1],
+ 'Text' => $args,
+ '$skip' => 0,
+ '$top' => 1
+ ));
- if(!$data) {
- return new BotMsg('Błąd podczas pobierania danych ze słownika. Przepraszamy.');
+ if(!$data || !isset($data['d']['results'][0]['Text'])) {
+ return new BotMsg('Błąd podczas pobierania danych z tłumacza. Przepraszamy.');
}
- return new BotMsg('<u>Tłumaczenie (by Microsoft Translator):</u><br />'."\n".strip_tags($data));
+ $data = $data['d']['results'][0]['Text'];
+
+ return new BotMsg('<u>Tłumaczenie (by Microsoft Translator):</u><br />'."\n".htmlspecialchars($data));
}
else
{
diff --git a/modules/80_lang/msapi.php b/modules/80_lang/msapi.php
new file mode 100644
index 0000000..4f4fc16
--- /dev/null
+++ b/modules/80_lang/msapi.php
@@ -0,0 +1,44 @@
+<?php
+require_once(dirname(__FILE__).'/msapi_config.php');
+
+class msapi extends msapi_config {
+ public $url;
+
+ function __construct($url) {
+ $this->url = $url;
+ }
+
+ function execute($params) {
+ if(!is_array($params)) {
+ throw new Exception('Przekazany parametr nie jest tablicą');
+ }
+
+ foreach($params as $name => &$param) {
+ if(substr($name, 0, 1)!='$' && is_string($param)) {
+ $param = '\''.$param.'\'';
+ }
+ }
+ unset($param);
+ $params['$format'] = 'json';
+
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'request_fulluri' => TRUE,
+ 'header' => 'Authorization: Basic '.base64_encode(':'.$this->accountKey)
+ ),
+ ));
+
+ $content = file_get_contents($this->url.'?'.http_build_query($params, '', '&'), FALSE, $context);
+ if(!$content) {
+ return FALSE;
+ }
+
+ $content = json_decode($content, TRUE);
+ if(!$content) {
+ return FALSE;
+ }
+
+ return $content;
+ }
+}
+?>
diff --git a/modules/80_lang/msapi_config.php b/modules/80_lang/msapi_config.php
new file mode 100644
index 0000000..d9fb5e0
--- /dev/null
+++ b/modules/80_lang/msapi_config.php
@@ -0,0 +1,5 @@
+<?php
+class msapi_config {
+ protected $accountKey = '';
+}
+?>
--
Gitblit v1.9.1