PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/parsers/housmans.php

https://github.com/hubgit/events
PHP | 72 lines | 52 code | 18 blank | 2 comment | 4 complexity | 5f991359ee99d75f07d07af2f2dd5398 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. require __DIR_ . '/../main.inc.php';
  4. $calendar_name = 'Housmans Bookshop';
  5. $text = file_get_contents('http://www.housmans.com/events.php');
  6. preg_match_all('!<(?:strong|span)[^>]*>(\d+)\..+?\((.+?)\).*?<br />\s*(?:</strong>)?(?:</span>)?(.+?)</(?:strong|span)>.*?<(?:em|span)[^>]*>(.+?)</(?:em|span)>.*?(?:<img[^>]+src="(.+?)")?!is', $text, $matches, PREG_SET_ORDER);
  7. //print_r($matches); exit();
  8. $items = array();
  9. foreach ($matches as $item){
  10. array_shift($item);
  11. $items[] = array_map('fix_housmans', $item);
  12. }
  13. $today = strtotime(date('Y-m-d'));
  14. $year = date('Y');
  15. $events = array();
  16. foreach ($events as $item){
  17. list($number, $type, $summary, $date) = $item;
  18. $datetime = preg_split('/\s*(\–|\-)\s*/', $date);
  19. if (count($datetime) < 2)
  20. continue;
  21. foreach ($datetime as $key => $value){
  22. $value = preg_replace('/^\W+/', '', $value);
  23. $value = preg_replace('/\W+$/', '', $value);
  24. $datetime[$key] = trim($value);
  25. }
  26. if (isset($datetime[2]) && preg_match('/([ap]m)$/i', $datetime[2], $matches))
  27. $datetime[1] .= $matches[1];
  28. unset($datetime[2]);
  29. debug($datetime);
  30. $date_input = implode(' ', $datetime);
  31. debug($date_input);
  32. $start = DateTime::createFromFormat('l jS F Ga', $date_input);
  33. if (!is_object($start))
  34. continue;
  35. debug($start->format('Y-m-d H:i:s') . "\n===\n");
  36. $start = $start->getTimestamp();
  37. $events[] = array(
  38. 'start' => $start,
  39. 'end' => $start + 60*60, // 1hr
  40. 'uri' => '',
  41. 'image' => '',
  42. 'summary' => $summary,
  43. 'description' => $summary,
  44. 'location' => $calendar_name,
  45. );
  46. }
  47. ical($calendar_name, $events);
  48. function fix_housmans($data){
  49. $data = html_entity_decode($data);
  50. $data = preg_replace('/&.+?;/', '', $data);
  51. $data = preg_replace('/\s+/', ' ', $data);
  52. $data = strip_tags($data);
  53. return $data;
  54. }