snippetify/snippet-sniffer
Composer 安装命令:
composer require snippetify/snippet-sniffer
包简介
Crawling and scraping web pages to extract snippets
README 文档
README
Snippet sniffer allows you to extract code snippets from any websites.
What it does
This library allows you
- To get code snippets using search engine api (Google)
- To get code snippets from any web page by crawling url seeds.
How to use it
$ composer require snippetify/snippet-sniffer
Snippet Sniffer
use Snippetify\SnippetSniffer\SnippetSniffer; // Configurations $config = [ // Required // Search engine api configuration keys 'provider' => [ "cx" => "your google Search engine ID", "key" => "your google API key" 'name' => 'provider name (google)', ], // Optional // Useful for adding meta information to each snippet 'app' => [ "name" => "your App name", 'version' => 'your App version', ], // Optional // Useful for logging 'logger' => [ "name" => "logger name", 'file' => 'logger file path', ] ]; // Required // Your query $query = "your query"; // Optional // Meta params $meta = [ "page" => 1, "limit" => 10, ]; // Fetch snippets // @return Snippetify\SnippetSniffer\Common\Snippet[] $snippets = SnippetSniffer::create($config)->fetch($query, $meta); /* * Snippet object public attributes [ * title: string, * code: string, * description: string, * tags: array, // Array of string, also contains the snippet language * meta: array *] */
Providers
Providers allow you to get a stack of seeds(urls to scrape) from search engine API. Only Google search engine API is supported at this time, but you can create your own.
use Snippetify\SnippetSniffer\Providers\GoogleProvider; // Search engine api configuration keys $config = [ "cx" => "your google Search engine ID", "key" => "your google API key" ]; // Your query $query = "your query"; // Meta params $meta = [ "page" => 1, "limit" => 10, ]; // url seeds // @return GuzzleHttp\Psr7\Uri[] $urlSeeds = GoogleProvider::create($config)->fetch($query, $meta);
Add new providers to package
- Git clone the project
- Create your new class in the
Snippetify\SnippetSniffer\Providersfolder - Each provider implements
Snippetify\SnippetSniffer\Providers\ProviderInterface - Take a look at
Snippetify\SnippetSniffer\Providers\GoogleProviderto get you helped - Your fetch method must return an array of
Psr\Http\Message\UriInterface - Add it in the providers stacks in the
Snippetify\SnippetSniffer\Core.php - Write tests. Take a look at
Snippetify\SnippetSniffer\Tests\Providers\GoogleProviderTestto get you helped - Send a pull request to us
Use your own providers
- Your provider must implement
Snippetify\SnippetSniffer\Providers\ProviderInterface - Take a look at
Snippetify\SnippetSniffer\Providers\GoogleProviderto get you helped - Your fetch method must return an array of
Psr\Http\Message\UriInterface - Pass your new provider in the configuration parameter or use the
addProvidermethod
use Snippetify\SnippetSniffer\SnippetSniffer; // Use Configurations $config = [ "providers" => [ "provider_name" => ProviderClass::class, "provider_2_name" => Provider2Class::class // You can add as many as you want ] ]; // Or use addProvider method as follow SnippetSniffer::create(...) ->addProvider('provider_name', ProviderClass::class) ->addProvider('provider_2_name', Provider2Class::class) // You can add as many as you want ...
Scrapers
Scrappers allow you to scrape html page and extract the snippets.
use GuzzleHttp\Psr7\Uri; use Snippetify\SnippetSniffer\Scrapers\DefaultScraper; // Configurations $config = [ // Optional // Useful for adding meta information to each snippet 'app' => [ "name" => "your App name", 'version' => 'your App version', ], // Optional // Useful for logging 'logger' => [ "name" => "logger name", 'file' => 'logger file path', ] ]; // Your url $urlSeed = "website url to scrape"; // Fetch snippets // @return Snippetify\SnippetSniffer\Common\Snippet[] $snippets = (new DefaultScraper($config))->fetch(new Uri($urlSeed));
Add new scrapers to package
- Git clone the project
- Create your new class in the
Snippetify\SnippetSniffer\Scrapersfolder - Each scraper implements
Snippetify\SnippetSniffer\Scrapers\ScraperInterface - Take a look at
Snippetify\SnippetSniffer\Scrapers\StackoverflowScraperto get you helped - Your fetch method must return an array of
Snippetify\SnippetSniffer\Common\Snippet - Add it in the scrapers stacks in the
Snippetify\SnippetSniffer\Core.php - Write tests. Take a look at
Snippetify\SnippetSniffer\Tests\Scrapers\StackoverflowScraperTestto get you helped - Send a pull request to us
Use your own scrapers
- Your scraper must implement
Snippetify\SnippetSniffer\Scrapers\ScraperInterface - Take a look at
Snippetify\SnippetSniffer\Scrapers\StackoverflowScraperto get you helped - Your fetch method must return an array of
Snippetify\SnippetSniffer\Common\Snippet - Pass your new scraper in the configuration parameter or use the
addScrapermethod
use Snippetify\SnippetSniffer\SnippetSniffer; // Important: Scrapper's name must be the website uri without the scheme. i.e. vuejs.org // Configurations $config = [ "scrapers" => [ "scraper_name" => ScraperClass::class, "scraper_2_name" => Scraper2Class::class // You can add as many as you want ] ]; // Or use addProvider method as follow SnippetSniffer::create(...) ->addScraper('scraper_name', ScraperClass::class) ->addScraper('scraper_2_name', Scraper2Class::class) // You can add as many as you want ...
Snippet crawler
Snippet crawler allows you to extract all snippets from a website by crawling it.
use Snippetify\SnippetSniffer\WebCrawler; // Optional $config = [...]; // @return Snippetify\SnippetSniffer\Common\MetaSnippetCollection[] $snippets = WebCrawler::create($config)->fetch(['your uri']);
Configuration reference
$config = [ // Required // Search engine api configuration keys // https://developers.google.com/custom-search/v1/introduction 'provider' => [ "cx" => "your google Search engine ID", "key" => "your google API key" 'name' => 'provider name (google)', ], // Optional // Useful for adding meta information to each snippet 'app' => [ "name" => "your App name", 'version' => 'your App version', ], // Optional // Useful for logging 'logger' => [ "name" => "logger name", 'file' => 'logger file path', ], // Optional // Useful for scraping "html_tags" => [ "snippet" => "pre[class] code, div[class] code, .highlight pre, code[class]", // Tags to fetch snippets "index" => "h1, h2, h3, h4, h5, h6, p, li" // Tags to index ], // Optional // Useful for adding new scrapers // The name must be the website host without the scheme i.e. not https://foo.com but foo.com "scrapers" => [ "scraper_name" => ScraperClass::class, "scraper_2_name" => Scraper2Class::class // You can add as many as you want ], // Optional // Useful for adding new providers "providers" => [ "provider_name" => ProviderClass::class, "provider_2_name" => Provider2Class::class // You can add as many as you want ], // Optional // Useful for web crawling // Please follow the link below for more information as we use Spatie crawler // https://github.com/spatie/crawler "crawler" => [ "langs" => ['en'], "profile" => CrawlSubdomainsAndUniqueUri::class, "user_agent" => 'your user agent', "concurrency" => 10, "ignore_robots" => false, "maximum_depth" => null, "execute_javascript" => false, "maximum_crawl_count" => null, "parseable_mime_types" => 'text/html', "maximum_response_size" => 1024 * 1024 * 3, "delay_between_requests" => 250, ] ];
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
You must set the PROVIDER_NAME, PROVIDER_CX, PROVIDER_KEY, CRAWLER_URI, DEFAULT_SCRAPER_URI, STACKOVERFLOW_SCRAPER_URI keys in phpunit.xml file before running tests.
Important: Those links must contains at least one snippet otherwise the tests will failed. The Stackoverflow uri must be a question link with an accepted answer otherwise the tests will failed.
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
snippetify/snippet-sniffer 适用场景与选型建议
snippetify/snippet-sniffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「bot」 「crawling」 「scraping」 「snippet」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 snippetify/snippet-sniffer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snippetify/snippet-sniffer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 snippetify/snippet-sniffer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight, dependency free PHP class that acts as wrapper for Crawlbase API
Dialogflow php sdk
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Laravel 5.6 Monolog custom telegram channel
Crawlzone is a fast asynchronous internet crawling framework aiming to provide open source web search and testing solution. It can be used for a wide range of purposes, from extracting and indexing structured data to monitoring and automated testing.
TYPO3 extension for generating .well-known/llm.txt links according to llmstxt.org specification
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-26