Source.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. Microblog bot for game release notifications
  4. Copyright (C) 2017 Alexander Yakovlev
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. namespace Source;
  17. use \PHPHtmlParser\Dom;
  18. use \Game;
  19. abstract class Source {
  20. public $title;
  21. protected $dom;
  22. protected $period;
  23. protected $output;
  24. public $games;
  25. public function __construct() {
  26. global $lastrun;
  27. $this->dom = new Dom;
  28. $this->period = $lastrun;
  29. $this->output = '';
  30. $this->games = [];
  31. }
  32. /**
  33. * Function to start the section.
  34. * @param whether to return or print the text
  35. */
  36. protected function startSection($return = false) {
  37. if (FORMAT === 'MARKDOWN') {
  38. $text = "\n#### ".$this->title."\n";
  39. }
  40. if (FORMAT === 'HTML') {
  41. $text = "\n<spoiler title='".$this->title."'><h4>".$this->title."</h4>\n<ul>";
  42. }
  43. if ($return) {
  44. return $text;
  45. }
  46. $this->output .= $text;
  47. }
  48. protected function endSection() {
  49. if (FORMAT === 'HTML') {
  50. $text = "</ul></spoiler>\n";
  51. }
  52. $this->output .= $text;
  53. }
  54. abstract protected function parse();
  55. protected function get_text($url) {
  56. $curl = curl_init();
  57. curl_setopt_array($curl, array(
  58. CURLOPT_RETURNTRANSFER => 1,
  59. CURLOPT_URL => $url,
  60. CURLOPT_CONNECTTIMEOUT => 30,
  61. ));
  62. $resp = curl_exec($curl);
  63. curl_close($curl);
  64. return $resp;
  65. }
  66. public function check() {
  67. try {
  68. $this->parse();
  69. return $this->output;
  70. } catch (Exception $e) {
  71. echo $e->getMessage();
  72. echo $e->getTraceAsString();
  73. }
  74. }
  75. }