|
| 1 | +# Snippet sniffer |
| 2 | + |
| 3 | +**Snippet sniffer** allows you to extract code snippets from any websites. |
| 4 | + |
| 5 | +## What it does |
| 6 | + |
| 7 | +This library allows you |
| 8 | + |
| 9 | +1. To get url seeds from search engine api (Google) |
| 10 | +2. Get code snippets from any web page by crawling url seeds. |
| 11 | + |
| 12 | +## How to use it |
| 13 | + |
| 14 | +```bash |
| 15 | +$ composer require snippetify/snippet-sniffer |
| 16 | +``` |
| 17 | + |
| 18 | +```php |
| 19 | +use Snippetify\SnippetSniffer\SnippetSniffer; |
| 20 | + |
| 21 | +// Configurations |
| 22 | +$config = [ |
| 23 | + // Required |
| 24 | + // Search engine api configuration keys |
| 25 | + 'provider' => [ |
| 26 | + "cx" => "your google Search engine ID", |
| 27 | + "key" => "your google API key" |
| 28 | + 'name' => 'provider name (google)', |
| 29 | + ], |
| 30 | + // Optional |
| 31 | + // Useful for adding meta information to each snippet |
| 32 | + 'app' => [ |
| 33 | + "name" => "your App name", |
| 34 | + 'version' => 'your App version', |
| 35 | + ], |
| 36 | + // Optional |
| 37 | + // Useful for logging |
| 38 | + 'logger' => [ |
| 39 | + "name" => "logger name", |
| 40 | + 'file' => 'logger file path', |
| 41 | + ] |
| 42 | +]; |
| 43 | + |
| 44 | +// Required |
| 45 | +// Your query |
| 46 | +$query = "your query"; |
| 47 | + |
| 48 | +// Optional |
| 49 | +// Meta params |
| 50 | +$meta = [ |
| 51 | + "page" => 1, |
| 52 | + "limit" => 10, |
| 53 | +]; |
| 54 | + |
| 55 | +// Fetch snippets |
| 56 | +// @return Snippetify\SnippetSniffer\Common\Snippet[] |
| 57 | +$snippets = SnippetSniffer::create($config)->fetch($query, $meta); |
| 58 | +/* |
| 59 | +* Snippet object public attributes [ |
| 60 | +* title: string, |
| 61 | +* code: string, |
| 62 | +* description: string, |
| 63 | +* tags: array, // Array of string, also contains the snippet language |
| 64 | +* meta: array |
| 65 | +*] |
| 66 | +*/ |
| 67 | +``` |
| 68 | + |
| 69 | +#### Providers |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +```php |
| 74 | +use Snippetify\SnippetSniffer\Providers\GoogleProvider; |
| 75 | + |
| 76 | +// Search engine api configuration keys |
| 77 | +$config = [ |
| 78 | + "cx" => "your google Search engine ID", |
| 79 | + "key" => "your google API key" |
| 80 | +]; |
| 81 | + |
| 82 | +// Your query |
| 83 | +$query = "your query"; |
| 84 | + |
| 85 | +// Meta params |
| 86 | +$meta = [ |
| 87 | + "page" => 1, |
| 88 | + "limit" => 10, |
| 89 | +]; |
| 90 | + |
| 91 | +// url seeds |
| 92 | +// @return GuzzleHttp\Psr7\Uri[] |
| 93 | +$urlSeeds = GoogleProvider::create($config)->fetch($query, $meta); |
| 94 | +``` |
| 95 | + |
| 96 | +##### Add new providers |
| 97 | + |
| 98 | +1. Git clone the project |
| 99 | +2. Create your new class in the ``Snippetify\SnippetSniffer\Providers` folder |
| 100 | +3. Each provider implements `Snippetify\SnippetSniffer\Providers\ProviderInterface` |
| 101 | +4. Take a look at `Snippetify\SnippetSniffer\Providers\GoogleProvider` to get you helped |
| 102 | +5. Your fetch method must return an array of `GuzzleHttp\Psr7\Uri` |
| 103 | +6. Add it in the providers stacks in the `Snippetify\SnippetSniffer\Core.php` |
| 104 | +7. Write tests. Take a look at `Snippetify\SnippetSniffer\Tests\Providers\GoogleProviderTest` to get you helped |
| 105 | +8. Send a pull request to us |
| 106 | + |
| 107 | +#### Scrapers |
| 108 | + |
| 109 | +Scrappers allow you to scrape html page and extract the snippets. |
| 110 | + |
| 111 | +```php |
| 112 | +use GuzzleHttp\Psr7\Uri; |
| 113 | +use Snippetify\SnippetSniffer\Scrapers\DefaultScraper; |
| 114 | + |
| 115 | +// Configurations |
| 116 | +$config = [ |
| 117 | + // Optional |
| 118 | + // Useful for adding meta information to each snippet |
| 119 | + 'app' => [ |
| 120 | + "name" => "your App name", |
| 121 | + 'version' => 'your App version', |
| 122 | + ], |
| 123 | + // Optional |
| 124 | + // Useful for logging |
| 125 | + 'logger' => [ |
| 126 | + "name" => "logger name", |
| 127 | + 'file' => 'logger file path', |
| 128 | + ] |
| 129 | +]; |
| 130 | + |
| 131 | +// Your url |
| 132 | +$urlSeed = "website url to scrape"; |
| 133 | + |
| 134 | +// Fetch snippets |
| 135 | +// @return Snippetify\SnippetSniffer\Common\Snippet[] |
| 136 | +$snippets = (new DefaultScraper($config))->fetch(new Uri($urlSeed)); |
| 137 | +``` |
| 138 | + |
| 139 | +##### Add new scrapers |
| 140 | + |
| 141 | +1. Git clone the project |
| 142 | +2. Create your new class in the ``Snippetify\SnippetSniffer\Scrapers` folder |
| 143 | +3. Each scraper implements `Snippetify\SnippetSniffer\Scrapers\ScraperInterface` |
| 144 | +4. Take a look at `Snippetify\SnippetSniffer\Scrapers\StackoverflowScraper` to get you helped |
| 145 | +5. Your fetch method must return an array of `Snippetify\SnippetSniffer\Common\Snippet` |
| 146 | +6. Add it in the scrapers stacks in the `Snippetify\SnippetSniffer\Core.php` |
| 147 | +7. Write tests. Take a look at `Snippetify\SnippetSniffer\Tests\Scrapers\StackoverflowScraperTest` to get you helped |
| 148 | +8. Send a pull request to us |
| 149 | + |
| 150 | +## Changelog |
| 151 | + |
| 152 | +Please see [CHANGELOG](https://github.com/snippetify/snippet-sniffer/blob/master/CHANGELOG.md) for more information what has changed recently. |
| 153 | + |
| 154 | +## Testing |
| 155 | + |
| 156 | + You must set the **PROVIDER_NAME**, **PROVIDER_CX**, **PROVIDER_KEY** keys in phpunit.xml file before running tests. |
| 157 | + |
| 158 | +```bash |
| 159 | +composer test |
| 160 | +``` |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +Please see [CONTRIBUTING](https://github.com/snippetify/snippet-sniffer/blob/master/CONTRIBUTING.md) for details. |
| 165 | + |
| 166 | +## Credits |
| 167 | + |
| 168 | +1. [Evens Pierre](https://github.com/pierrevensy) |
| 169 | + |
| 170 | +## License |
| 171 | + |
| 172 | +The MIT License (MIT). Please see [License File](https://github.com/snippetify/snippet-sniffer/blob/master/LICENSE.md) for more information. |
| 173 | + |
0 commit comments