CastorusBridge.php 2.9 KB

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