Skip to main content

Safarnama is a versatile web crawler that explores websites, cleans HTML, and uses a language model to generate summaries and tags—writing its own digital story. Use it via CLI or in Python projects.

Project description

Safarnama

Safarnama is named after the famous travel book Safarnama by Nasir Khusraw, a well-known Persian traveler, philosopher, and writer. In his book, Nasir Khusraw shared his adventures and experiences as he traveled far and wide. Inspired by this classic work, our tool sets out on a journey across the internet, visiting websites and gathering useful information.

As it explores, Safarnama cleans up web pages by removing unnecessary parts like scripts and styles. It also uses a language model to create easy-to-read summaries and extract key tags.

Whether you are a developer looking to add web crawling to your project or a user who enjoys a simple command-line tool, Safarnama is designed for both. It works both from the command line and programmatically in your own Python code.

Join Safarnama on its adventure as it writes its own digital story—one website at a time.

Features

  • Web Crawling: Starts at a base URL and explores linked pages up to a specified depth.
  • Content Processing: Cleans HTML content by removing scripts, styles, comments, and other extraneous elements.
  • LLM Integration: Summarizes page content and extracts relevant tags by communicating with a Language Model endpoint.
  • Data Storage: Saves discovered URL data, summaries, and tags in a SQLite database.
  • Sitemap Generation: Optionally creates an XML sitemap of all crawled URLs.

Installation

Install Safarnama via pip:

pip install safarnama

Usage (Command-Line)

Begin your digital expedition from the command line:

safarnama init      # Initialize the configuration file (interactive or quiet mode)
safarnama start     # Start crawling based on the configuration
safarnama test_llm  # Test connectivity to the LLM endpoint

Running the tool without any arguments will automatically display the help message.

Sample Configuration File (config.yaml)

base_url: "https://www.python.org"
max_depth: 2
delay: 1
db_path: "python.db"
verbose: true
save: true
log_file: "python.log"
generate_sitemap: true
binary_extensions:
  - ".pdf"
  - ".zip"
  - ".exe"
  - ".tar"
  - ".tar.gz"
  - ".tgz"
  - ".rar"
  - ".iso"
  - ".bin"
  - ".7z"
  - ".dmg"
  - ".tar.xz"
  - ".pkg"
  - ".bz2"
accepted_content_types:
  - "text/html"
  - "application/xhtml+xml"
  - "text/plain"
  - "text/xml"
  - "application/xml"
  - "application/json"
llm:
  endpoint: "http://localhost:1234/v1/chat/completions"
  model: "jinaai.readerlm-v2@q4_k_m"
  max_tokens: 16529
  temperature: 0.7

Note: The LLM API key is managed via a separate .env file and is not stored in this configuration file.

Programmatic Usage

Safarnama is not only a command-line tool but can also be used programmatically in your Python projects. Below is an example of how to integrate its crawling capabilities:

from safarnama import load_config, SiteCrawler

# Load configuration from the YAML file
config = load_config("config.yaml")

# Create a crawler instance with the loaded configuration
crawler = SiteCrawler(config)

# Start crawling and retrieve a set of visited URLs
visited_urls = crawler.crawl()

# Display the number of URLs crawled
print(f"Crawled {len(visited_urls)} URLs.")

# Optionally, generate an XML sitemap of the crawled URLs
sitemap_tree = crawler.generate_sitemap(visited_urls)
sitemap_tree.write("sitemap.xml", encoding="utf-8", xml_declaration=True)

# Close the crawler's database connection
crawler.close()

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Write your code and tests.
  4. Run the tests to make sure everything is working.
  5. Submit a pull request with a clear description of your changes.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

✨ Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safarnama-0.1.2.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safarnama-0.1.2-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file safarnama-0.1.2.tar.gz.

File metadata

  • Download URL: safarnama-0.1.2.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.7

File hashes

Hashes for safarnama-0.1.2.tar.gz
Algorithm Hash digest
SHA256 663c181e11598c394e799f729d08f64df9ffe4cc88752782714107452e75fd29
MD5 d15c2c95931bb776ee079692f247038f
BLAKE2b-256 0f47fed03a7eedfc4594b97e5771ed76386cffb50a49bcf86e74abb68741d1c0

See more details on using hashes here.

File details

Details for the file safarnama-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: safarnama-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.7

File hashes

Hashes for safarnama-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 68a6b5014abffb75347f553a1862ed11d90b0e4fd68156f734f9c85316755d97
MD5 3e3a455cbb4bbe5f339d227420b690a3
BLAKE2b-256 7b6bd19b324787388b17229fec2cf97313745bd7d2643a3dda34fbea7b5f989a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page