Added RSS folder to misc

RSS source now added
This commit is contained in:
MrLuit 2016-11-19 16:44:33 +01:00
commit 4138da492a
3 changed files with 1282 additions and 0 deletions

83
misc/RSS/rss.class.php Normal file
View file

@ -0,0 +1,83 @@
<?php
class RSS {
public $old_file;
public $db_file;
public function __construct($old_file,$db_file) {
require('yaml.class.php');
date_default_timezone_set('GMT');
$this->old_file = $old_file;
$this->db_file = $db_file;
}
public function getYAML() {
return spyc_load(file_get_contents("https://raw.githubusercontent.com/TrumpTracker/trumptracker.github.io/master/_data/data.yaml"));
}
public function stripQuotes($text) {
$unquoted = preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $text);
return $unquoted;
}
public function newMessage($message,$title,$original) {
if(file_exists($this->db_file)) {
$old = json_decode(file_get_contents($this->db_file),true);
}
else {
$old = array();
}
$old[] = array("content" => $this->stripQuotes($message),"time" => time(),"title" => $title,"author" => "TrumpTracker","uri" => "https://trumptracker.github.io","url" => $original['source']);
$old = json_encode($old);
file_put_contents($this->db_file,$old);
}
public function parsePoints($yaml) {
$points = array();
foreach($yaml['tabs'] as $t) {
foreach($t['sections'] as $s) {
foreach($s['points'] as $p) {
$points[$p['text']] = $p;
}
}
}
return $points;
}
public function parseDifference($points) {
if(file_exists($this->old_file)) {
$yaml_old = file_get_contents($this->old_file);
$yaml_old = json_decode($yaml_old,true);
foreach($yaml_old as $y) {
if(!isset($points[$y['text']])) {
$this->newMessage("\"$y[text]\" has been removed from the list of policies.","Policy removed",$y);
}
elseif($points[$y['text']]['status'] != $y['status']) {
if($points[$y['text']]['status'] == "notStarted") {
$this->newMessage("\"$y[text]\" is not started anymore :(","Policy updated",$y);
}
elseif($points[$y['text']]['status'] == "inProgress") {
$this->newMessage("\"$y[text]\" is now in progress!","Policy updated",$y);
}
elseif($points[$y['text']]['status'] == "achieved") {
$this->newMessage("\"$y[text]\" has been achieved!","Policy updated",$y);
}
elseif($points[$y['text']]['status'] == "broken") {
$this->newMessage("\"$y[text]\" has been broken :(","Policy updated",$y);
}
}
}
foreach($points as $p) {
if(!isset($yaml_old[$p['text']])) {
$this->newMessage("\"$p[text]\" has been added to the list of policies!","Policy added",$p);
}
}
}
}
public function updateOld($points) {
$points = json_encode($points);
file_put_contents($this->old_file,$points);
}
public function getDB() {
if(file_exists("db.json")) {
$rss = file_get_contents("db.json");
$rss = json_decode($rss,true);
krsort($rss);
return $rss;
}
}
}

38
misc/RSS/rss.php Normal file
View file

@ -0,0 +1,38 @@
<?php
require('rss.class.php');
$rss = new RSS("old_data.json","db.json"); //Create RSS instance
$yaml = $rss->getYAML(); //Get the newest YAML from github
$points = $rss->parsePoints($yaml); //Parse the YAML correctly
$rss->parseDifference($points); //See if there is any difference
$rss->updateOld($points); //Update the yaml file to the newest YAML
$rss = $rss->getDB(); //Get all previous changes
//Start actually parsing the RSS
$i = 0;
header("Content-Type: application/xml; charset=UTF-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
?>
<rss version="2.0">
<channel>
<title>Recent TrumpTracker updates</title>
<link>https://trumptracker.github.io</link>
<description>RSS feed for all policy updates on Trump's presidency</description>
<language>en-US</language>
<?
foreach($rss as $id => $item) {
$i++;
if($i < 10) {
echo "<item>\n";
echo "<guid isPermaLink='false'>$id</guid>\n";
echo "<title>$item[title]</title>\n";
echo "<link>$item[url]</link>\n";
echo "<description>$item[content]</description>\n";
$time = date('D, d M Y H:i:s T',$item['time']);
echo "<pubDate>$time</pubDate>\n";
echo "</item>\n";
}
}
?>
</channel>
</rss>

1161
misc/RSS/yaml.class.php Normal file

File diff suppressed because it is too large Load diff