|
| 1 | +# LeafyGreen Crawler Tool |
| 2 | + |
| 3 | +A CLI tool for crawling and analyzing website content for LeafyGreen AI. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This tool crawls websites and stores the content in MongoDB collections for use with LeafyGreen AI systems. The crawler can process either specific URLs or use pre-configured website sources. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Node.js (v16 or higher) |
| 12 | +- Yarn package manager |
| 13 | +- MongoDB Atlas account with connection details |
| 14 | +- Environment variables properly configured |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +# From the root of the leafygreen-ui-private repository |
| 20 | +cd tools/crawler |
| 21 | +yarn install |
| 22 | +``` |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +Create a `.env` file in the `tools/crawler` directory with the following variables: |
| 27 | + |
| 28 | +``` |
| 29 | +MONGODB_USER=your_mongodb_user |
| 30 | +MONGODB_PASSWORD=your_mongodb_password |
| 31 | +MONGODB_PROJECT_URL=your_project_url |
| 32 | +MONGODB_APP_NAME=your_app_name |
| 33 | +``` |
| 34 | + |
| 35 | +### Default Sources |
| 36 | + |
| 37 | +The crawler comes with pre-configured sources in `src/constants.ts`: |
| 38 | + |
| 39 | +- MongoDB Design (https://mongodb.design) |
| 40 | +- React Documentation (https://react.dev) |
| 41 | +- MDN Web Docs (https://developer.mozilla.org) |
| 42 | + |
| 43 | +To add or modify sources, edit the `SOURCES` array in `src/constants.ts`. |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Building the Tool |
| 48 | + |
| 49 | +```bash |
| 50 | +yarn build |
| 51 | +``` |
| 52 | + |
| 53 | +### Basic Usage |
| 54 | + |
| 55 | +```bash |
| 56 | +# Use the built version |
| 57 | +yarn lg-crawler |
| 58 | + |
| 59 | +# Or use the development version |
| 60 | +yarn crawl |
| 61 | +``` |
| 62 | + |
| 63 | +### Command Line Options |
| 64 | + |
| 65 | +- `-v, --verbose`: Enable verbose output |
| 66 | +- `-d, --depth <number>`: Set maximum crawl depth (default: 3) |
| 67 | +- `--url <url>`: Specify a single URL to crawl |
| 68 | +- `--dry-run`: Run crawler without inserting documents into MongoDB |
| 69 | + |
| 70 | +### Examples |
| 71 | + |
| 72 | +```bash |
| 73 | +# Crawl all pre-configured sources with verbose output |
| 74 | +yarn crawl --verbose |
| 75 | + |
| 76 | +# Crawl a specific URL with a depth of 2 |
| 77 | +yarn crawl --url https://example.com --depth 2 |
| 78 | + |
| 79 | +# Test crawling without saving to MongoDB |
| 80 | +yarn crawl --dry-run --verbose |
| 81 | +``` |
| 82 | + |
| 83 | +## Development |
| 84 | + |
| 85 | +### Project Structure |
| 86 | + |
| 87 | +- `src/index.ts`: Main entry point and command-line interface |
| 88 | +- `src/crawler.ts`: Core crawler implementation |
| 89 | +- `src/constants.ts`: Configuration constants and source definitions |
| 90 | +- `src/utils/`: Helper utilities for crawling and data processing |
| 91 | + |
| 92 | +### Adding New Features |
| 93 | + |
| 94 | +1. Make your code changes |
| 95 | +2. Build the project: `yarn build` |
| 96 | +3. Test your changes: `yarn crawl --dry-run --verbose` |
| 97 | + |
| 98 | +### Running Tests |
| 99 | + |
| 100 | +```bash |
| 101 | +yarn test |
| 102 | +``` |
| 103 | + |
| 104 | +## Troubleshooting |
| 105 | + |
| 106 | +- **MongoDB Connection Issues**: Verify your `.env` file has the correct credentials |
| 107 | +- **Crawling Errors**: Use the `--verbose` flag to get detailed logs |
| 108 | +- **Rate Limiting**: Some websites may block the crawler if too many requests are made |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +Apache-2.0 |
0 commit comments