summaryrefslogtreecommitdiff
path: root/index.php
blob: 31154be358a1ab3ed8131e310cab5bb8a588ef65 (plain)
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
<?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);

	$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();