CastorusBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. class CastorusBridge extends BridgeAbstract {
  3. const MAINTAINER = "logmanoriginal";
  4. const NAME = "Castorus Bridge";
  5. const URI = 'http://www.castorus.com';
  6. const DESCRIPTION = "Returns the latest changes";
  7. const PARAMETERS = array(
  8. 'Get latest changes' => array(),
  9. 'Get latest changes via ZIP code' => array(
  10. 'zip'=>array(
  11. 'name'=>'ZIP code',
  12. 'type'=>'text',
  13. 'required'=>true,
  14. 'exampleValue'=>'74910, 74',
  15. 'title'=>'Insert ZIP code (complete or partial)'
  16. )
  17. ),
  18. 'Get latest changes via city name' => array(
  19. 'city'=>array(
  20. 'name'=>'City name',
  21. 'type'=>'text',
  22. 'required'=>true,
  23. 'exampleValue'=>'Seyssel, Seys',
  24. 'title'=>'Insert city name (complete or partial)'
  25. )
  26. )
  27. );
  28. // Extracts the tile from an actitiy
  29. private function ExtractActivityTitle($activity){
  30. $title = $activity->find('a', 0);
  31. if(!$title)
  32. returnServerError('Cannot find title!');
  33. return htmlspecialchars(trim($title->plaintext));
  34. }
  35. // Extracts the url from an actitiy
  36. private function ExtractActivityUrl($activity){
  37. $url = $activity->find('a', 0);
  38. if(!$url)
  39. returnServerError('Cannot find url!');
  40. return self::URI . $url->href;
  41. }
  42. // Extracts the time from an activity
  43. private function ExtractActivityTime($activity){
  44. // Unfortunately the time is part of the parent node,
  45. // so we have to clear all child nodes first
  46. $nodes = $activity->find('*');
  47. if(!$nodes)
  48. returnServerError('Cannot find nodes!');
  49. foreach($nodes as $node){
  50. $node->outertext = '';
  51. }
  52. return strtotime($activity->innertext);
  53. }
  54. // Extracts the price change
  55. private function ExtractActivityPrice($activity){
  56. $price = $activity->find('span', 1);
  57. if(!$price)
  58. returnServerError('Cannot find price!');
  59. return $price->innertext;
  60. }
  61. public function collectData(){
  62. $zip_filter = trim($this->getInput('zip'));
  63. $city_filter = trim($this->getInput('city'));
  64. $html = getSimpleHTMLDOM(self::URI);
  65. if(!$html)
  66. returnServerError('Could not load data from ' . self::URI . '!');
  67. $activities = $html->find('div#activite/li');
  68. if(!$activities)
  69. returnServerError('Failed to find activities!');
  70. foreach($activities as $activity){
  71. $item = array();
  72. $item['title'] = $this->ExtractActivityTitle($activity);
  73. $item['uri'] = $this->ExtractActivityUrl($activity);
  74. $item['timestamp'] = $this->ExtractActivityTime($activity);
  75. $item['content'] = '<a href="' . $item['uri'] . '">' . $item['title'] . '</a><br><p>'
  76. . $this->ExtractActivityPrice($activity) . '</p>';
  77. if(isset($zip_filter) && !(substr($item['title'], 0, strlen($zip_filter)) === $zip_filter)){
  78. continue; // Skip this item
  79. }
  80. if(isset($city_filter) && !(substr($item['title'], strpos($item['title'], ' ') + 1, strlen($city_filter)) === $city_filter)){
  81. continue; // Skip this item
  82. }
  83. $this->items[] = $item;
  84. }
  85. }
  86. public function getCacheDuration(){
  87. return 600; // 10 minutes
  88. }
  89. }