summaryrefslogtreecommitdiff
path: root/plugins/af_pennyarcade/init.php
blob: 06c1230a6f718ccdc845c98a00677576d37a46a4 (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
81
82
83
84
85
86
87
88
89
<?php
class Af_PennyArcade extends Plugin {

	private $link;
	private $host;

	function about() {
		return array(1.1,
			"Strip unnecessary stuff from PA feeds",
			"fox");
	}

	function init($host) {
		$this->link = $host->get_link();
		$this->host = $host;

		$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
	}

	function hook_article_filter($article) {
		$owner_uid = $article["owner_uid"];

		if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "Comic:") !== FALSE) {
			if (strpos($article["plugin_data"], "pennyarcade,$owner_uid:") === FALSE) {
				
				if ($debug_enabled) {
					_debug("af_pennyarcade: Processing comic");
				}
				
				$doc = new DOMDocument();
				$doc->loadHTML(fetch_file_contents($article["link"]));

				$basenode = false;

				if ($doc) {
					$xpath = new DOMXPath($doc);
					$entries = $xpath->query('(//div[@class="post comic"])');

					foreach ($entries as $entry) {
						$basenode = $entry;
					}

					if ($basenode) {
						$article["content"] = $doc->saveXML($basenode);
						$article["plugin_data"] = "pennyarcade,$owner_uid:" . $article["plugin_data"];
					}
				}
			} else if (isset($article["stored"]["content"])) {
				$article["content"] = $article["stored"]["content"];
			}
		}
	
		if (strpos($article["link"], "penny-arcade.com") !== FALSE && strpos($article["title"], "News Post:") !== FALSE) {
			if (strpos($article["plugin_data"], "pennyarcade,$owner_uid:") === FALSE) {
				if ($debug_enabled) {
					_debug("af_pennyarcade: Processing news post");
				}
				$doc = new DOMDocument();
				$doc->loadHTML(fetch_file_contents($article["link"]));
				
				if ($doc) {
					$xpath = new DOMXPath($doc);
					$entries = $xpath->query('(//div[@class="post"])');
					
					$basenode = false;
					
					foreach ($entries as $entry) {
						$basenode = $entry;
					}
					
					$uninteresting = $xpath->query('(//div[@class="heading"])');
					foreach ($uninteresting as $i) {
						$i->parentNode->removeChild($i);
					}
					
					if ($basenode){
						$article["content"] = $doc->saveXML($basenode);
						$article["plugin_data"] = "pennyarcade,$owner_uid:" . $article["plugin_data"];
					}
				}
			} else if (isset($article["stored"]["content"])) {
				$article["content"] = $article["stored"]["content"];
			}
		}

		return $article;
	}
}
?>