This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_eye.php
81 lines (64 loc) · 2.14 KB
/
rss_eye.php
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
<?php
class rss_eye {
var $url;
var $hashMethod;
var $tmpDir;
var $items = array(
"new" => Array(),
"cached" => Array(),
);
var $feed;
function rss_eye($url, $tmpDir = '/tmp/', $hashMethod = 'md5') {
$this->url = $url;
$this->tmpDir = $tmpDir;
$this->hashMethod = $hashMethod;
$this->check();
}
function getFeed() {
$feed = file_get_contents($this->url);
$xml = simplexml_load_string($feed);
$this->feed = $xml;
return $this->feed;
}
function getFeedJson() {
$this->getFeed();
return json_encode($this->feed);
}
function check() {
$this->getFeed();
$xml = $this->feed;
$items = Array();
$i=0;
while (!empty($xml->channel->item[$i]) && !is_null($xml->channel->item[$i]->title)) {
// Generate Hash values
$rssUrlHash = hash($this->hashMethod, $this->url);
$pubDateHash = hash($this->hashMethod, $xml->channel->item[$i]->pubDate);
$titleHash = hash($this->hashMethod, $xml->channel->item[$i]->title);
// Compile unique identifier for this RSS item
$itemHashUUID = "$rssUrlHash.$pubDateHash.$titleHash";
// Convert to JSON for cache storage
$itemJSON = json_encode($xml->channel->item[$i]);
// Unneccessary for hash checks, but useful for other things
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$title = $xml->channel->item[$i]->title;
$pubDate = $xml->channel->item[$i]->pubDate;
// Use file-cache to see if they exists
if (!file_exists($this->tmpDir.$itemHashUUID)) {
// RSS Item is either new or has not been cached before
// Cache the item and contents for later reference
$fp = fopen($this->tmpDir.$itemHashUUID, 'w');
fwrite($fp, $itemJSON);
fclose($fp);
array_push($this->items['new'],$xml->channel->item[$i]);
}
else {
// RSS Item has already been cached.
array_push($this->items['cached'],$xml->channel->item[$i]);
}
// Increase while-loop counter
$i++;
} // close while loop
return true; // All good!
} // close check();
}