1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

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
This commit is contained in:
J. King 2017-04-24 21:51:56 -04:00
parent f842439b01
commit 2e64e60f2e
2 changed files with 37 additions and 5 deletions

View file

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

View file

@ -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");
}
}
}