Game.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. use League\HTMLToMarkdown\HtmlConverter;
  17. use Mremi\UrlShortener\Model\Link;
  18. class Game {
  19. public $url;
  20. public $title;
  21. public $author;
  22. public $description;
  23. public $date;
  24. public function text() {
  25. global $bitlyProvider;
  26. $link = new Link;
  27. try {
  28. $link->setLongUrl($this->url);
  29. $bitlyProvider->shorten($link);
  30. $link = $link->getShortUrl();
  31. } catch (Exception $e) {
  32. echo $e->getMessage()."\n";
  33. $link = '';
  34. }
  35. $title = $this->author.' “'.$this->title.'”';
  36. $description = $this->description;
  37. $image = NULL;
  38. $description = $this->formatdsc($description);
  39. $limit = 500 - strlen($link) - strlen($title) - 5;
  40. $description = "$title\n\n".$this->ellipse($description, $limit);
  41. $description .= "\n$link";
  42. return $description;
  43. }
  44. protected function formatdsc($description) {
  45. global $pandoc;
  46. $description = strip_tags($description, 'img');
  47. $description = $pandoc->runWith($description, [
  48. 'from' => 'html',
  49. 'to' => 'plain',
  50. 'wrap' => 'none'
  51. ]);
  52. $description = str_replace('Читать дальше', '', $description);
  53. $description = trim($description);
  54. return $description;
  55. }
  56. protected function ellipse($str,$n_chars,$crop_str='[...]')
  57. {
  58. $buff = $str;
  59. if(strlen($buff) > $n_chars)
  60. {
  61. $cut_index=strpos($buff,' ',$n_chars);
  62. $buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
  63. }
  64. return $buff;
  65. }
  66. }