~nico-izo-ya/+junk/aaron2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/****************************************************************************
 * google.cpp
 *  Copyright © 2011, Vsevolod Velichko.
 *  Licence: GPLv3 or later
 *
 ****************************************************************************
 *                                                                          *
 *   This library is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 3 of the License, or      *
 *   (at your option) any later version.                                    *
 *                                                                          *
 ****************************************************************************/

#include "google.h"
#include <QDebug>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QScopedPointer>
#include <QEventLoop>
#include <QRegExp>
#include <QUrlQuery>

Google::Google(const QString &refer)
{
	setReferer(refer);
}

void Google::setReferer(const QString &refer)
{
	if (refer.isEmpty())
		qDebug() << "Google: referer is empty, service will be unavailable";
	referer = refer;
}

/*QString Google::search(const QString &text)
{
	static QRegExp resultsStart("\"items\":\\s*\\[");
	static QRegExp responseText("\"snippet\":\\s*\"([^\"]+)");
	static QRegExp responseTitle("\"title\":\\s*\"([^\"]+)");
	QUrl request("https://www.googleapis.com/customsearch/v1");
}*/

QString Google::translate(const QString &langpair, const QString &text)
{
	static QRegExp responseCode("\"responseStatus\":\\s*(\\d+)");
	static QRegExp responseText("\"translatedText\":\\s*\"([^\"]+)");
	QUrl request("https://ajax.googleapis.com/ajax/services/language/translate");
	QUrlQuery query;
	query.addQueryItem("v", "1.0");
	query.addQueryItem("q", text);
	query.addQueryItem("langpair", langpair);
	request.setQuery(query);

	QString result = fetchUrl(request);
	qDebug() << result;
	if (result.isEmpty())
		return QString();
	if ((responseCode.indexIn(result) == -1) || (responseCode.cap(1) != "200"))
	{
		return QString();
	}
	if (responseText.indexIn(result) != -1)
		return responseText.cap(1).replace("\\u0026quot;", "\"");
	return QString();
}

QString Google::fetchUrl(const QUrl &url)
{
	qDebug() << url.toString();
	QEventLoop loop;
	QNetworkRequest req(url);
	req.setRawHeader("Referer", referer.toLatin1());
	QScopedPointer<QNetworkReply> reply(qnam.get(req));
	QObject::connect(reply.data(), SIGNAL(finished()), &loop, SLOT(quit()));
	loop.exec();
	if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200)
	{
		qDebug() << "Google: got no reply";
		return QString();
	}
	return QString::fromUtf8(reply->readAll().constData());
}