From 2e64e60f2ee5cf6182254cda833fd65a0ab042d4 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 24 Apr 2017 21:51:56 -0400 Subject: [PATCH] Implement part of adaptive update interval; improves #51 Implements part of algorithm used when a feed has not been updated; this is much simpler than when a feed has been modified --- lib/Database.php | 10 +++++----- lib/Feed.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index e7f50bb..b35ddd1 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -517,10 +517,9 @@ class Database { try { if(is_null($articleID)) { $articleID = $this->db->prepare( - 'INSERT INTO arsse_articles(feed,url,title,author,published,edited,guid,content,url_title_hash,url_content_hash,title_content_hash) values(?,?,?,?,?,?,?,?,?,?,?)', - 'int', 'str', 'str', 'str', 'datetime', 'datetime', 'str', 'str', 'str', 'str', 'str' + 'INSERT INTO arsse_articles(url,title,author,published,edited,guid,content,url_title_hash,url_content_hash,title_content_hash,feed) values(?,?,?,?,?,?,?,?,?,?,?)', + 'str', 'str', 'str', 'datetime', 'datetime', 'str', 'str', 'str', 'str', 'str', 'int' )->run( - $feedID, $article->url, $article->title, $article->author, @@ -530,7 +529,8 @@ class Database { $article->content, $article->urlTitleHash, $article->urlContentHash, - $article->titleContentHash + $article->titleContentHash, + $feedID )->lastId(); } else { $this->db->prepare( @@ -553,7 +553,7 @@ class Database { // If the article has categories add them into the categories database. $this->db->prepare('DELETE FROM arsse_categories WHERE article is ?', 'int')->run($articleID); $this->categoriesAdd($articleID, $article); - // increease the article edition ID + // add a new article edition ID $this->db->prepare('INSERT INTO arse_editions(article) values(?)', 'int')->run($articleID); } catch(\Throwable $e) { $this->db->rollback(); diff --git a/lib/Feed.php b/lib/Feed.php index bfddb15..d6ac8ff 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -12,6 +12,8 @@ class Feed { public $parser; public $reader; public $resource; + public $modified = false; + public $lastModified = null; public $newItems = []; public $changedItems = []; @@ -23,6 +25,11 @@ class Feed { $this->reader = new Reader($config); $this->resource = $this->reader->download($url, $lastModified, $etag, $username, $password); + $lastMod = $this->resource->getLastModified(); + if(strlen($lastMod)) { + $this->$lastModified = \DateTime::createFromFormat("!D, d M Y H:i:s e", $lastMod); + } + $this->modified = $this->resource->isModified(); } catch (PicoFeedException $e) { throw new Feed\Exception($url, $e); } @@ -218,4 +225,29 @@ class Feed { } return true; } + + public function nextFetch(): \DateTime { + if(!$this->modified) { + $now = time(); + $diff = $now - $this->lastModified->getTimestamp(); + if($diff < (30 * 60)) { // less than 30 minutes + $offset = "15 minutes"; + } else if($diff < (60 * 60)) { // less than an hour + $offset = "30 minutes"; + } else if($diff < (3 * 60 * 60)) { // less than three hours + $offset = "1 hour"; + } else if($diff > (36 * 60 * 60)) { // more than 36 hours + $offset = "1 day"; + } else { + $offset = "3 hours"; + } + $t = new \DateTime(); + $t->setTimestamp($now); + $t->modify("+".$offset); + return $t; + } else { + // FIXME: implement algorithm to use when a feed has been updated + return new \DateTime("now + 3 hours"); + } + } } \ No newline at end of file