<?php
class PostFromInternet {
public $hasTopic;
public $websiteAdress;
private $regexp;
private $regexp2;
private $botUsername;
private $debug;
function __construct($botUsername, $topic) {
$this->hasTopic = false;
$this->debug = false;
$this->botUsername = $botUsername;
$this->getUrlSetting($topic);
}
public function getPost() {
$websiteCurler = new MiniCurl($this->websiteAdress);
$content = $websiteCurler->getData();
// $testMessage = var_export ( $websiteCurler, true );
// file_put_contents ( "testpostfrompapasagtphoto.html", $testMessage, FILE_APPEND | LOCK_EX );
return $this->extractRelatedData($content);
}
public function selectRandom($cats) {
return(array_rand($cats,1));
}
public function extractRelatedData($content) {
if ($this->botUsername == "@bilderbuch_bot") {
if (preg_match_all ( $this->regexp, $content, $matches )) {
$found[]= $matches [1] [0];
}
if (preg_match_all ( $this->regexp2, $content, $matches )) {
$found[]= $matches [1] [0];
}
return $found;
} else if ($this->botUsername == "@khabaralman_bot") {
$content = str_replace("media:thumbnail", "media", $content);
$content = str_replace("media:content", "media", $content);
// $xml = simplexml_load_string($content);
// $json = json_encode($xml);
// $array = json_decode($json,TRUE);
// print_r($array);
$rss = new DOMDocument();
$rss->loadXML($content);
$feed = array ();
foreach ( $rss->getElementsByTagName ( 'item' ) as $node ) {
$description = $node->getElementsByTagName ( 'description' )->item ( 0 )->nodeValue;
$regex ='/<img\s[^>]*?alt="(.*?)"\salign[^>]*>/';
// $regex ='/<img\s[^>]*?alt="([^"]*)"\s[^>]*?/';
if (preg_match_all ( $regex, $description, $matches )) {
$alt = $matches [1] [0];
}
if(isset($matches [0] [0])){
$descr= str_replace($matches [0] [0], "", $description);
$descr= str_replace("<br", "", $descr);
$descr= str_replace("/>", "", $descr);
//$desc = str_replace("media:content", "media", $content);
// if($node->getElementsByTagName ( 'media' )->item ( 0 ) !== null){
// $media = $node->getElementsByTagName ( 'media' )->item ( 0 );
// $media2 = $node->getElementsByTagName ( 'media' )->item ( 0 );
$media = $node->getElementsByTagName ( 'media' )->item ( 0 )->getAttribute('url');
$media2 = $node->getElementsByTagName ( 'media' )->item ( 1 )->getAttribute('url');
$item = array (
'title' => $node->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue,
'alt' => $alt,
'desc' => trim($descr),
'link' => $node->getElementsByTagName ( 'link' )->item ( 0 )->nodeValue,
'pubdate' => $node->getElementsByTagName ( 'pubDate' )->item ( 0 )->nodeValue,
'media' => $media,
'media2' => $media2,
'enclosure' => $node->getElementsByTagName ( 'enclosure' )->item ( 0 )->getAttribute('url')
);
array_push ( $feed, $item );
}
}
return $feed;
}
}
public function getUrlSetting($topic){
// if($this->debug)
// $this->botUsername="@khabaralman_bot";
// $testMessage = var_export ( $this->botUsername, true );
// file_put_contents ( "testeinfaaaach.html", $testMessage, FILE_APPEND | LOCK_EX );
if($this->botUsername == "@bilderbuch_bot"){
$random=true;
if($random)
$this->websiteAdress= "http://meinpapasagt.de/zufalls-zitat/";
else
{
$cats=array("a"=>"zitat","b"=>"green","c"=>"blue","d"=>"yellow");
$selectedCat=$this->selectRandom($cats);
$this->websiteAdress = "http://meinpapasagt.de/".$selectedCat;
}
$this->regexp = '/<img\s[^>]*?src="([^>]*)"\s[^>]*?class="attachment-post-thumbnail/';
$this->regexp2 = '/<blockquote>([^$]*)<\/blockquote>/';
}
else if($this->botUsername == "@khabaralman_bot"){
$random=false;
if($random)
$this->websiteAdress= "http://www.bild.de/rss-feeds/rss-16725492,feed=bremen.bild.html";
else
{
$this->websiteAdress = $this->getLinkByTopic($topic);
}
$this->regexp = '/<img\s[^>]*?src="([^>]*)"\s[^>]*?class="attachment-post-thumbnail/';
}
}
public function getLinkByTopic($topic){
$newsArray = array (
"Alle Artikel" => "http://www.bild.de/rssfeeds/rss3-20745882,feed=alles.bild.html",
"Homepage" => "http://www.bild.de/rss-feeds/rss-16725492,feed=home.bild.html",
"Politik" => "http://www.bild.de/rss-feeds/rss-16725492,feed=politik.bild.html",
"Unterhaltung" => "http://www.bild.de/rss-feeds/rss-16725492,feed=unterhaltung.bild.html",
"Sport" => "http://www.bild.de/rss-feeds/rss-16725492,feed=sport.bild.html",
"Lifestyle" => "http://www.bild.de/rss-feeds/rss-16725492,feed=lifestyle.bild.html",
"Ratgeber" => "http://www.bild.de/rss-feeds/rss-16725492,feed=ratgeber.bild.html",
"Auto" => "http://www.bild.de/rss-feeds/rss-16725492,feed=auto.bild.html",
"Digital" => "http://www.bild.de/rss-feeds/rss-16725492,feed=digital.bild.html",
"Spiele" => "http://www.bild.de/rss-feeds/rss-16725492,feed=spiele.bild.html",
"Leserreporter 1414:" => "http://www.bild.de/rss-feeds/rss-16725492,feed=leserreporter.bild.html",
"Berlin" => "http://www.bild.de/rss-feeds/rss-16725492,feed=berlin.bild.html",
"Chemnitz" => "http://www.bild.de/rssfeeds/rss3/rss3-20745882,feed=ressort-regio-chemnitz.bild.html",
"Bremen" => "http://www.bild.de/rss-feeds/rss-16725492,feed=bremen.bild.html",
"D�sseldorf" => "http://www.bild.de/rss-feeds/rss-16725492,feed=duesseldorf.bild.html",
"Duesseldorf" => "http://www.bild.de/rss-feeds/rss-16725492,feed=duesseldorf.bild.html",
"Dresden" => "http://www.bild.de/rss-feeds/rss-16725492,feed=dresden.bild.html",
"Frankfurt/Main" => "http://www.bild.de/rss-feeds/rss-16725492,feed=regio-frankfurt.bild.html",
"Hannover" => "http://www.bild.de/rss-feeds/rss-16725492,feed=regio-hannover.bild.html",
"Hamburg" => "http://www.bild.de/rss-feeds/rss-16725492,feed=hamburg.bild.html",
"K�ln" => "http://www.bild.de/rss-feeds/rss-16725492,feed=regio-koeln.bild.html",
"Koeln" => "http://www.bild.de/rss-feeds/rss-16725492,feed=regio-koeln.bild.html",
"Leipzig" => "http://www.bild.de/rss-feeds/rss-16725492,feed=leipzig.bild.html",
"M�nchen" => "http://www.bild.de/rss-feeds/rss-16725492,feed=muenchen.bild.html",
"Muenchen" => "http://www.bild.de/rss-feeds/rss-16725492,feed=muenchen.bild.html",
"Ruhrgebiet" => "http://www.bild.de/rss-feeds/rss-16725492,feed=ruhrgebiet.bild.html",
"Saarland" => "http://www.bild.de/rssfeeds/rss3/rss3-20745882,feed=regional-saarland.bild.html",
"Stuttgart" => "http://www.bild.de/rss-feeds/rss-16725492,feed=regio-stuttgart.bild.html",
"spiegel online uni" => "http://www.spiegel.de/politik/deutschland/index.rss",
"welt" => "https://www.welt.de/feeds/section/reise.rss",
"Newsticker" => "http://www.bild.de/rss-feeds/rss-16725492,feed=Newsticker.bild.html"
);
$result ="";
$topic = str_replace("/", "", $topic);
$topic = str_replace("@khabaralman_bot", "", $topic);
$topic = strtolower($topic);
$self=$this;
foreach ($newsArray as $key => $value){
if(strpos(strtolower($key), $topic) !== false){
$result = $value;
$self->hasTopic = true;
}
}
// $testMessage = var_export ( $topic, true );
// file_put_contents ( "testcity.html", $result, FILE_APPEND | LOCK_EX );
return $result;
}
}
?>