Commit fab4fa06 authored by Karl Hasselbring's avatar Karl Hasselbring
Browse files

WIP routing for quicktip requests

parent c9c603c9
......@@ -51,222 +51,6 @@ class MetaGerSearch extends Controller
->with('r', $redirect);
}
public function quicktips(Request $request, MetaGer $metager)
{
$q = $request->input('q', '');
$mquicktips = [];
$quicktips = [];
$spruch = "";
# DuckDuckGo-!bangs
try {
$placeholder = "0X0plchldr0X0";
$searchWords = explode(" ", $q);
$dummyQuery = "";
$realQuery = "";
foreach ($searchWords as $index => $word) {
if ($word[0] === "!") {
$dummyQuery .= $word . " ";
} else {
$realQuery .= $word . " ";
}
}
$realQuery = rtrim($realQuery);
if ($dummyQuery !== "") {
$dummyQuery .= $placeholder;
$url = "https://api.duckduckgo.com/?format=json&no_redirect=1&t=MetaGerDE&q=" . urlencode($dummyQuery);
$result = json_decode($this->get($url), true);
if (isset($result["Redirect"])) {
$bang = [];
$bang["title"] = trans('metaGerSearch.quicktips.bang.title');
$bang["URL"] = str_replace($placeholder, urlencode(trim($realQuery)), $result["Redirect"]);
$bang["summary"] = '<a href="' . $bang["URL"] . '" target=_blank class="btn btn-primary" style="margin-top:5px;color: #fff">' . trans('metaGerSearch.quicktips.bang.buttonlabel') . " " . parse_url($bang["URL"], PHP_URL_HOST) . '&hellip;</a>';
$bang["gefVon"] = trans('metaGerSearch.quicktips.bang.from') . " <a href = \"https://api.duckduckgo.com/\" target=\"_blank\" rel=\"noopener\">DuckDuckGo</a>";
$mquicktips[] = $bang;
}
}
} catch (\ErrorException $e) {
}
if (APP::getLocale() === "de") {
# Spruch
$spruecheFile = storage_path() . "/app/public/sprueche.txt";
if (file_exists($spruecheFile) && $request->has('sprueche')) {
$sprueche = file($spruecheFile);
$spruch = $sprueche[array_rand($sprueche)];
} else {
$spruch = "";
}
# manuelle Quicktips:
$file = storage_path() . "/app/public/qtdata.csv";
if (file_exists($file) && $q !== '') {
$file = fopen($file, 'r');
while (($line = fgetcsv($file)) !== false) {
$words = array_slice($line, 3);
$isIn = false;
foreach ($words as $word) {
$word = strtolower($word);
if (strpos($q, $word) !== false) {
$isIn = true;
break;
}
}
if ($isIn === true) {
$quicktip = array('QT_Type' => "MQT");
$quicktip["URL"] = $line[0];
$quicktip["title"] = $line[1];
$quicktip["summary"] = $line[2];
$mquicktips[] = $quicktip;
}
}
fclose($file);
}
# Wetter
/* Derzeit Deaktiviert, da bei unseren Abfragezahlen nicht kostenlos */
/*
try {
$url = "http://api.openweathermap.org/data/2.5/weather?type=accurate&units=metric&lang=" . APP::getLocale() . "&q=" . urlencode($q) . "&APPID=" . getenv("openweathermap");
$result = json_decode($this->get($url), true);
$searchWords = explode(' ', $q);
$within = false;
foreach ($searchWords as $word) {
if (stripos($result["name"], $word) !== false) {
$within = true;
}
}
if ($within) {
$weather = [];
$weather["title"] = "Wetter in " . $result["name"];
$weather["URL"] = "http://openweathermap.org/city/" . $result["id"];
$summary = '<b class="detail-short">' . $result["main"]["temp"] . " °C, " . $result["weather"][0]["description"] . "</b>";
$details = '<table class="table table-condensed"><tr><td>Temperatur</td><td>' . $result["main"]["temp_min"] . " bis " . $result["main"]["temp_max"] . " °C</td></tr>";
$details .= "<tr><td>Druck</td><td>" . $result["main"]["pressure"] . " hPa</td></tr>";
$details .= "<tr><td>Luftfeuchtigkeit</td><td>" . $result["main"]["humidity"] . " %</td></tr>";
$details .= "<tr><td>Wind</td><td>" . $result["wind"]["speed"] . " m/s, " . $result["wind"]["deg"] . "°</td></tr>";
$details .= "<tr><td>Bewölkung</td><td>" . $result["clouds"]["all"] . " %</td></tr>";
if (isset($result->rain)) {
$details .= " | Regen letzte drei Stunden: " . $result["rain"]["3h"] . " h";
}
$details .= "</table>";
$weather["summary"] = $summary;
$weather["details"] = $details;
$weather["gefVon"] = "von <a href = \"https://openweathermap.org\" target=\"_blank\" rel=\"noopener\">Openweathermap</a>";
$requestData = [];
$requestData["url"] = "http://openweathermap.org/img/w/";
$weather["image"] = action('Pictureproxy@get', $requestData) . $result["weather"][0]["icon"] . ".png";
$weather["image-alt"] = $result["weather"][0]["main"];
$mquicktips[] = $weather;
}
} catch (\ErrorException $e) {
}
*/
}
# Wikipedia Quicktip
$url = "https://" . APP::getLocale() . ".wikipedia.org/w/api.php?action=opensearch&search=" . urlencode($q) . "&limit=10&namespace=0&format=json&redirects=resolve";
try{
$content = $this->get($url);
}catch(\ErrorException $e){
$content = "";
}
$decodedResponse = json_decode($content, true);
if (isset($decodedResponse[1][0]) && isset($decodedResponse[2][0]) && isset($decodedResponse[3][0])) {
$quicktip = [];
$firstSummary = $decodedResponse[2][0];
// Wenn es mehr als ein Ergebnis gibt
if (isset($decodedResponse[1][1])) {
// Solange noch zusätzliche Seiten vorhanden sind, füge sie der Tabelle hinzu
$details = '<table class=table table-condensed>';
for ($i = 1;isset($decodedResponse[1][$i]) && isset($decodedResponse[2][$i]) && isset($decodedResponse[3][$i]); $i++) {
$details .= '<tr><td><a href="' . $decodedResponse[3][$i] . '" target="_blank" rel="noopener">' . $decodedResponse[1][$i] . '</a></td></tr>';
}
$details .= '</table>';
$quicktip["title"] = $decodedResponse[1][0];
$quicktip["URL"] = $decodedResponse[3][0];
$quicktip["summary"] = $decodedResponse[2][0];
$quicktip["details"] = $details;
$quicktip['gefVon'] = trans('metaGerSearch.quicktips.wikipedia.adress');
} else {
$quicktip["title"] = $decodedResponse[1][0];
$quicktip["URL"] = $decodedResponse[3][0];
$quicktip["summary"] = $decodedResponse[2][0];
$quicktip['gefVon'] = trans('metaGerSearch.quicktips.wikipedia.adress');
}
$quicktips[] = $quicktip;
}
$mquicktips = array_merge($mquicktips, $quicktips);
if (APP::getLocale() === "de") {
# Dict.cc Quicktip
if (count(explode(' ', $q)) < 3) {
$url = "http://www.dict.cc/metager.php?s=" . urlencode($q);
$decodedResponse = json_decode($this->get($url), true);
if ($decodedResponse["headline"] != "" && $decodedResponse["link"] != "") {
$quicktip = [];
$quicktip["title"] = $decodedResponse["headline"];
$quicktip["URL"] = $decodedResponse["link"];
$quicktip["summary"] = implode(", ", $decodedResponse["translations"]);
$quicktip['gefVon'] = trans('metaGerSearch.quicktips.dictcc.adress');
if (App::isLocale('de')) {
array_unshift($mquicktips, $quicktip);
} else {
$mquicktips[] = $quicktip;
}
}
}
# wussten Sie schon
$file = storage_path() . "/app/public/tips.txt";
if (file_exists($file)) {
$tips = file($file);
$tip = $tips[array_rand($tips)];
$mquicktips[] = ['title' => trans('metaGerSearch.quicktips.tips.title'), 'summary' => $tip, 'URL' => '/tips'];
}
# Werbelinks
$file = storage_path() . "/app/public/ads.txt";
if (file_exists($file)) {
$ads = json_decode(file_get_contents($file), true);
$ad = $ads[array_rand($ads)];
if (isset($ads['details'])) {
$mquicktips[] = ['title' => $ad['title'], 'summary' => $ad['summary'], 'details' => $ad['details'], 'URL' => $ad['URL']];
} else {
$mquicktips[] = ['title' => $ad['title'], 'summary' => $ad['summary'], 'URL' => $ad['URL']];
}
}
}
return view('quicktip')
->with('spruch', $spruch)
->with('mqs', $mquicktips);
}
public function tips()
{
$file = storage_path() . "/app/public/tips.txt";
$tips = [];
if (file_exists($file)) {
$tips = file($file);
}
return view('tips')
->with('title', 'MetaGer - Tipps & Tricks')
->with('tips', $tips);
}
public function get($url)
{
$ctx = stream_context_create(array('http'=>array('timeout' => 2,)));
......
......@@ -23,7 +23,6 @@ elixir(function (mix) {
| settings.js
*/
mix.scripts(['lib/jquery.js', 'lib/jquery-ui.min.js', 'lib/bootstrap.js', 'lib/lightslider.js', 'lib/masonry.js', 'lib/imagesloaded.js', 'lib/openpgp.min.js', 'lib/iframeResizer.min.js', 'lib/md5.js'], 'public/js/lib.js')
mix.scripts(['lib/jquery.js', 'lib/iframeResizer.contentWindow.min.js'], 'public/js/quicktips.js')
mix.scripts(['scriptStartPage.js', 'results.js'], 'public/js/scriptStartPage.js');
mix.scripts(['scriptResultPage.js', 'results.js'], 'public/js/scriptResultPage.js');
//mix.scripts(['scriptSubpages.js'], 'public/js/scriptSubpages.js');
......@@ -31,6 +30,6 @@ elixir(function (mix) {
// utility
mix.scripts(['utility.js'], 'public/js/utility.js');
mix.less('utility.less', 'public/css/utility.css');
mix.version(['css/themes/default.css', 'js/lib.js', 'js/quicktips.js', 'js/utility.js', 'css/utility.css', 'js/widgets.js', 'js/editLanguage.js', 'js/kontakt.js', 'js/scriptResultPage.js', 'js/scriptStartPage.js', 'js/settings.js', 'css/beitritt.css']);
mix.version(['css/themes/default.css', 'js/lib.js', 'js/utility.js', 'css/utility.css', 'js/widgets.js', 'js/editLanguage.js', 'js/kontakt.js', 'js/scriptResultPage.js', 'js/scriptStartPage.js', 'js/settings.js', 'css/beitritt.css']);
})
......@@ -9,6 +9,8 @@ $(document).ready(function () {
if (document.location.href.indexOf('focus=container') !== -1) {
$($('#foki > li#savedFokiTabSelector').get(0)).find('>a').tab('show');
}
loadQuicktips();
});
function activateJSOnlyContent () {
......@@ -533,3 +535,27 @@ function resultSaver (index) {
$('div.tab-pane.active .result[data-count=' + index + ']').transfer({to: to, duration: 1000});
new Results().updateResultPageInterface();
}
function loadQuicktips () {
/*
{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "/qt") }}
?q={{ $metager->getQ() }}
&sprueche={{ $metager->getSprueche() }}
&lang={{ Request::input('lang', 'all') }}
&unfilteredLink={{ base64_encode($metager->getUnfilteredLink()) }}
*/
var mainDiv = $('#quicktips');
mainDiv.attr('status', 'loading');
mainDiv.append('<h1>Hello World</h1>');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
mainDiv.append(this.responseXML);
console.log(this);
}
};
xhttp.open("GET", "/quicktips.xml?search=test" /*TODO change*/, true);
xhttp.send();
}
{{-- Verwendung: @include('layouts.utility') --}}
<script src="{{ elixir('js/utility.js') }}"></script>
<script defer src="{{ elixir('js/utility.js') }}"></script>
<link type="text/css" rel="stylesheet" href="{{ elixir('css/utility.css') }}" />
......@@ -56,8 +56,6 @@
</nav>
</div>
@if( $metager->showQuicktips() )
<div class="hidden-xs col-md-4" id="quicktips">
<iframe class="col-mod-4 hidden-xs hidden-sm" src="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "/qt") }}?q={{ $metager->getQ() }}&sprueche={{ $metager->getSprueche() }}&lang={{ Request::input('lang', 'all') }}&unfilteredLink={{ base64_encode($metager->getUnfilteredLink()) }}"></iframe>
</div>
<div class="col-md-4 hidden-xs hidden-sm" id="quicktips"></div>
@endif
@endsection
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<title>MetaGer quicktip search: @yield{'search'}</title>
<link href="https://www.metager.de/quicktips.xml?search=test"/>
<updated>@yield{'time'}</updated>
<opensearch:totalResults>{{ $quicktips->amount }}</opensearch:totalResults>
@foreach ($quicktips as $quicktip)
<entry>
$quicktip
</entry>
@endforeach
</feed>
......@@ -16,3 +16,9 @@ use Illuminate\Http\Request;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:api');
Route::get('/quicktips', function (Request $request) {
return view('quicktips')
->with('search', 'TODO')
->with('time', 'TODO');
})->middleware('auth:api');
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment