Unfinished activity ('quantified self') tracker
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

transport.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?PHP
  2. class TransportSource implements Source {
  3. private $data = array('last' => 0, 'data' => array());
  4. public function __construct() {
  5. $cache = dirname(dirname(__FILE__)) . '/cache/transport.dat';
  6. if (file_exists($cache)) {
  7. $this->data = unserialize(file_get_contents($cache));
  8. if (filemtime($cache) > strtotime('-1 day')) { return; }
  9. }
  10. $url = 'https://oyster.tfl.gov.uk/oyster/security_check';
  11. $fields = 'j_username=&j_password=';
  12. $info = $this->do_post_request($url, $fields);
  13. foreach ($info['data']['wrapper_data'] as $header) {
  14. if (substr($header, 0, 11) == 'Set-Cookie:') {
  15. $cookie = preg_replace('/^Set-Cookie: (.*?); Path.*$/', '\1', $header);
  16. break;
  17. }
  18. }
  19. $url = 'https://oyster.tfl.gov.uk/oyster/ppvStatement.do';
  20. $fields = '';
  21. $headers = 'Cookie: ' . $cookie;
  22. $info = $this->do_post_request($url, $fields, $headers, 'GET');
  23. $info = $this->do_post_request($url, $fields, $headers, 'GET');
  24. preg_match('/<table class="journeyhistory">.*?<tr>.*?<\/tr>(.*?)<\/table>/is', $info['response'], $m);
  25. preg_match_all('/<tr>(.*?)<\/tr>/is', $m[0], $m2);
  26. $mtime = $this->data['last'];
  27. foreach ($m2[1] as $match) {
  28. preg_match_all('/<td.*?>(.*?)<\/td>/is', $match, $m3);
  29. if (trim($m3[1][0]) != '&nbsp;') {
  30. $date = explode('/', trim($m3[1][0]));
  31. $date = $date[1] . '/' . $date[0] . '/' . $date[2];
  32. $date = strtotime($date);
  33. }
  34. $time = strtotime($m3[1][1], $date);
  35. $mtime = max($mtime, $time);
  36. if ($time > $this->data['last']) {
  37. $station = trim($m3[1][2]);
  38. $enter = 'Entry' == trim($m3[1][3]);
  39. $message = $enter ? 'Entered <strong>' : 'Departed <strong>';
  40. $message .= $station . ' Station</strong>';
  41. $this->data['data'][] = new Event($time, $message);
  42. }
  43. }
  44. $this->data['last'] = $mtime;
  45. file_put_contents($cache, serialize($this->data));
  46. }
  47. public function getData() {
  48. return $this->data['data'];
  49. }
  50. private function do_post_request($url, $data, $optional_headers = null, $method = 'POST') {
  51. $params = array('http' => array(
  52. 'method' => $method,
  53. 'content' => $data
  54. ));
  55. if ($optional_headers !== null) {
  56. $params['http']['header'] = $optional_headers;
  57. }
  58. $ctx = stream_context_create($params);
  59. $fp = @fopen($url, 'rb', false, $ctx);
  60. if (!$fp) {
  61. throw new Exception("Problem with $url, $php_errormsg");
  62. }
  63. $response = @stream_get_contents($fp);
  64. if ($response === false) {
  65. throw new Exception("Problem reading data from $url, $php_errormsg");
  66. }
  67. return array('response' => $response, 'data' => stream_get_meta_data($fp));
  68. }
  69. }
  70. $_SOURCES[] =& new TransportSource();
  71. ?>