picofeed 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env php
  2. <?php
  3. require_once 'vendor/autoload.php';
  4. use PicoFeed\Config\Config;
  5. use PicoFeed\Reader\Favicon;
  6. use PicoFeed\Scraper\Scraper;
  7. use PicoFeed\Reader\Reader;
  8. use PicoFeed\Logging\Logger;
  9. use PicoFeed\PicoFeedException;
  10. Logger::enable();
  11. function get_feed($url, $disable_filtering = false)
  12. {
  13. try {
  14. $reader = new Reader;
  15. $resource = $reader->discover($url);
  16. $parser = $reader->getParser(
  17. $resource->getUrl(),
  18. $resource->getContent(),
  19. $resource->getEncoding()
  20. );
  21. if ($disable_filtering) {
  22. $parser->disableContentFiltering();
  23. }
  24. return $parser->execute();
  25. }
  26. catch (PicoFeedException $e) {
  27. echo 'Exception thrown ===> "'.$e->getMessage().'"'.PHP_EOL;
  28. return false;
  29. }
  30. }
  31. function get_item($feed, $item_id)
  32. {
  33. foreach ($feed->items as $item) {
  34. if ($item->getId() === $item_id) {
  35. echo $item;
  36. echo "============= CONTENT ================\n";
  37. echo $item->getContent();
  38. echo "\n============= CONTENT ================\n";
  39. break;
  40. }
  41. }
  42. }
  43. function dump_feed($url)
  44. {
  45. $feed = get_feed($url);
  46. echo $feed;
  47. }
  48. function debug_feed($url)
  49. {
  50. get_feed($url);
  51. print_r(Logger::getMessages());
  52. }
  53. function dump_item($url, $item_id)
  54. {
  55. $feed = get_feed($url);
  56. if ($feed !== false) {
  57. get_item($feed, $item_id);
  58. }
  59. }
  60. function nofilter_item($url, $item_id)
  61. {
  62. $feed = get_feed($url, true);
  63. if ($feed !== false) {
  64. get_item($feed, $item_id);
  65. }
  66. }
  67. function grabber($url)
  68. {
  69. $grabber = new Scraper(new Config);
  70. $grabber->setUrl($url);
  71. $grabber->execute();
  72. print_r(Logger::getMessages());
  73. echo "============= CONTENT ================\n";
  74. echo $grabber->getRelevantContent().PHP_EOL;
  75. echo "============= FILTERED ================\n";
  76. echo $grabber->getFilteredContent().PHP_EOL;
  77. }
  78. function fetch_favicon($url)
  79. {
  80. $favicon = new Favicon();
  81. echo $favicon->find($url) . PHP_EOL;
  82. }
  83. // Parse command line arguments
  84. if ($argc === 4) {
  85. switch ($argv[1]) {
  86. case 'item':
  87. dump_item($argv[2], $argv[3]);
  88. die;
  89. case 'nofilter':
  90. nofilter_item($argv[2], $argv[3]);
  91. die;
  92. }
  93. } else if ($argc === 3) {
  94. switch ($argv[1]) {
  95. case 'feed':
  96. dump_feed($argv[2]);
  97. die;
  98. case 'debug':
  99. debug_feed($argv[2]);
  100. die;
  101. case 'grabber':
  102. grabber($argv[2]);
  103. die;
  104. case 'favicon':
  105. fetch_favicon($argv[2]);
  106. die;
  107. }
  108. }
  109. printf("Usage:\n");
  110. printf("%s feed <feed-url>\n", $argv[0]);
  111. printf("%s debug <feed-url>\n", $argv[0]);
  112. printf("%s item <feed-url> <item-id>\n", $argv[0]);
  113. printf("%s nofilter <feed-url> <item-id>\n", $argv[0]);
  114. printf("%s grabber <url>\n", $argv[0]);
  115. printf("%s favicon <url>\n", $argv[0]);