diff options
| author | Horus | 2021-01-17 23:06:59 +0100 |
|---|---|---|
| committer | Horus | 2021-01-17 23:06:59 +0100 |
| commit | dbb49c2d9160ef8c13670113bb34ce8452385a62 (patch) | |
| tree | af5e5f7aec807efc7f7558673c035203e4a4619e /index.php | |
| download | dilbert_rss-dbb49c2d9160ef8c13670113bb34ce8452385a62.tar.gz | |
Initial commit.
Diffstat (limited to 'index.php')
| -rw-r--r-- | index.php | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/index.php b/index.php new file mode 100644 index 0000000..f6f8872 --- /dev/null +++ b/index.php @@ -0,0 +1,83 @@ +<?php + +define('PUBLIC_DIR', 'public'); + +require_once __DIR__ . '/vendor/autoload.php'; + +use DiDom\Document; +use Suin\RSSWriter\Channel; +use Suin\RSSWriter\Feed; +use Suin\RSSWriter\Item; + +function get_dilbert_items(){ + $xml = simplexml_load_file("http://dilbert.com/feed"); + if (false === $xml) { + die('Error: Loading http://dilbert.com/feed failed. ' . strtotime(date("r")) ); + } + + $return_values = array(); + $count = 0; + + foreach ($xml as $node) { + $link = (string)$node->link["href"]; + if ( $link != "" ) { + + $document = new Document($link, true); + $img = $document->find(".img-comic-container")[0]->find(".img-comic-link")[0]->find("img")[0]; + + $time = (string)$node->updated; + + $doc = new DOMDocument(); + $doc->loadHTML($img); + $xpath = new DOMXPath($doc); + $src = $xpath->evaluate("string(//img/@src)"); + $title = $xpath->evaluate("string(//img/@alt)"); + + $count++; + $return_values[$count]["title"] = $title; + $return_values[$count]["link"] = $src; + $return_values[$count]["time"] = $time; + $return_values[$count]["description"] = "<img src='".$src."' title='".$title."' alt='".$title."'>"; + } + } + + return $return_values; +} + +function generate() { + $feed = new Feed(); + + $channel = new Channel(); + $channel + ->title("Dilbert") + ->description("Dilbert - with Pictures") + ->url( "https://feeds.iamfabulous.de/Dilbert.xml" ) + ->pubDate(strtotime(date("r", time()))) + ->lastBuildDate(strtotime(date("r", time()))) + ->ttl(5) + ->appendTo($feed); + + #->url("http://feeds.iamfabulous.de/".$name) + + + $r = get_dilbert_items(); + + foreach($r as $item) { + $item_out = new Item(); + + $item_out + ->title($item["title"]) + ->description($item["description"]) + ->url($item["link"]) + ->pubDate(strtotime(date($item["time"]))) + ->appendTo($channel); + } + + if ( ! file_exists( PUBLIC_DIR ) ) { + mkdir( PUBLIC_DIR ); + } + + file_put_contents(PUBLIC_DIR . '/Dilbert.xml', $feed ); +} + +generate(); |
