readability-addon/xExtension-Readability/extension.php

56 lines
1.1 KiB
PHP
Raw Normal View History

2022-05-20 17:55:33 +00:00
<?php
2022-05-20 18:07:42 +00:00
class ReadabilityExtension extends Minz_Extension {
2022-05-20 20:10:13 +00:00
2022-05-20 17:55:33 +00:00
public function init() {
2022-05-21 14:11:26 +00:00
#$this->registerTranslates();
2022-05-20 17:55:33 +00:00
2022-05-21 14:11:26 +00:00
#$current_user = Minz_Session::param('currentUser');
2022-05-20 17:55:33 +00:00
$this->registerHook('entry_before_insert', array($this, 'fetchStuff'));
}
public function fetchStuff($entry) {
2022-05-20 18:07:42 +00:00
2022-05-20 20:10:13 +00:00
$read = false;
2022-05-21 14:11:26 +00:00
$regex = [
'washingtonpost.com',
'heise.de',
'nytimes.com'
];
2022-05-20 20:10:13 +00:00
foreach ( $regex as $ex ) {
2022-05-21 14:11:26 +00:00
print "Iterating";
if (false !== strpos($entry->link(), $ex ) ) {
$read = true;
}
2022-05-20 20:10:13 +00:00
}
if (! $read){
2022-05-21 14:11:26 +00:00
print "Constantly failing";
2022-05-20 20:10:13 +00:00
return $entry;
}
2022-05-21 14:11:26 +00:00
print "should work";
2022-05-20 17:55:33 +00:00
$data = "{\"url\": \"" . $entry->link() ."\"}";
$headers[] = 'Content-Type: application/json';
$c = curl_init("http://read:3000/");
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($c);
$c_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
//$c_error = curl_error($c);
curl_close($c);
if ($c_status !== 200) {
return $entry;
}
$val = json_decode($result, true);
$entry->_content($val["content"]);
return $entry;
}
}