Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. <?php
  2. //Script to connect to a database, get a list of websites
  3. //check most recent post date against the database and determine which sites
  4. //have been updated.
  5. //It then sorts the data nicely and outputs it.
  6. //Require and init the library
  7. require_once('lastrss.php');
  8. $rss = new lastRSS;
  9. //Set cache information
  10. $rss->cache_dir = 'tmp';
  11. $rss->cache_time = 3600;
  12. //Init connection to the database and query it for a list of all stored websites
  13. $db = new mysqli('localhost','root','','startpage');
  14. $q = $db->query("SELECT * FROM sitelist");
  15. //Begin unordered list
  16. echo "<ul>";
  17. //Begin a loop through each item in the array
  18. while ($site = $q->fetch_assoc()) {
  19. //Grab the RSS feed location, shortURL and item name
  20. $url = $site['rsslocation'];
  21. $surl = $site['url'];
  22. $name = $site['name'];
  23. //Parse the feed
  24. $feed = $rss->get($url);
  25. //Stop it from continuing if feed parsing failed
  26. if(!$feed) {
  27. echo "<li>Error: Failed to fetch RSS data for $name</li>";
  28. }
  29. //Hack to handle Concession's feed, which lacks publish dates.
  30. if($name=='Concession') {
  31. //Get most recent post's title, strip unnecessary data and add year
  32. $datething = $feed['items'][0]['title'];
  33. $datething = str_replace('Concession, ','',$datething);
  34. $date = $datething." ".date('Y');
  35. //Convert it to epoch
  36. $mostrecent = (int) strtotime($date);
  37. } else {
  38. //Get publish date and convert to epoch
  39. $pubdate = $feed['items'][0]['pubDate'];
  40. $mostrecent = (int) strtotime($pubdate);
  41. }
  42. //Get epoch of the latest known RSS update from the DB
  43. $lastupdate = (int) $site['lastupdated'];
  44. //Get epoch of the time that the last successful check was completed
  45. $lastcheck = (int) $site['timesincelastcheck'];
  46. //Get the time
  47. $lcheck = time();
  48. if($lastupdate == $mostrecent || $lastcheck > $lcheck + 3600) {
  49. //If there's no updates to the RSS, and the last update was found more than an hour ago, loop round to the next item
  50. } else {
  51. //Output website name
  52. echo "<li><a href=\"/go/$surl\">$name</a></li>";
  53. //Update database so that it doesn't keep showing the same websites.
  54. //Only perform this update if the check completed successfully and new RSS entries were found
  55. if($lastupdate != $mostrecent) {
  56. $udb = $db->query("UPDATE sitelist SET lastupdated='$mostrecent' timesincelastcheck='$lcheck' WHERE rsslocation='$url'");
  57. if(!$udb) {
  58. echo "database update failed";
  59. }
  60. }
  61. }
  62. //Loop round to the next website.
  63. }
  64. echo "</ul>";
  65. ?>
Add Comment
Please, Sign In to add comment