Coder Social home page Coder Social logo

grobotstxt's Introduction

grobotstxt

Apache 2.0 Build Status codecov Go Report Card Used By Godoc

grobotstxt is a native Go port of Google's robots.txt parser and matcher C++ library.

  • Direct function-for-function conversion/port
  • Preserves all behaviour of original library
  • All 100% of original test suite functionality
  • Minor language-specific cleanups
  • Added a helper to extract Sitemap URIs
  • Super simple API

As per Google's original library, we include a small standalone binary executable, for webmasters, that allows testing a single URL and user-agent against a robots.txt. Ours is called icanhasrobot, and its inputs and outputs are compatible with the original tool.

About

Quoting the README from Google's robots.txt parser and matcher repo:

The Robots Exclusion Protocol (REP) is a standard that enables website owners to control which URLs may be accessed by automated clients (i.e. crawlers) through a simple text file with a specific syntax. It's one of the basic building blocks of the internet as we know it and what allows search engines to operate.

Because the REP was only a de-facto standard for the past 25 years, different implementers implement parsing of robots.txt slightly differently, leading to confusion. This project aims to fix that by releasing the parser that Google uses.

The library is slightly modified (i.e. some internal headers and equivalent symbols) production code used by Googlebot, Google's crawler, to determine which URLs it may access based on rules provided by webmasters in robots.txt files. The library is released open-source to help developers build tools that better reflect Google's robots.txt parsing and matching.

Package grobotstxt aims to be a faithful conversion, from C++ to Go, of Google's robots.txt parser and matcher.

Quickstart

Installation

For developers

Get the package (only needed if not using modules):

go get github.com/jimsmart/grobotstxt

Use the package within your code (see examples below):

import "github.com/jimsmart/grobotstxt"

For webmasters

Assumes Go is installed, and its environment is already set up.

Fetch the package:

go get github.com/jimsmart/grobotstxt

Build and install the standalone binary executable:

go install github.com/jimsmart/grobotstxt/...

By default, the resulting binary executable will be ~/go/bin/icanhasrobot (assuming no customisation has been made to $GOPATH or $GOBIN).

Use the tool:

$ icanhasrobot ~/local/path/to/robots.txt YourBot https://example.com/url
user-agent 'YourBot' with URI 'https://example.com/url': ALLOWED

If $GOBIN is not included in your environment's $PATH, use the full path ~/go/bin/icanhasrobot when invoking the executable.

Example Code

AgentAllowed

import "github.com/jimsmart/grobotstxt"

// Contents of robots.txt file.
robotsTxt := `
    # robots.txt with restricted area

    User-agent: *
    Disallow: /members/*

    Sitemap: http://example.net/sitemap.xml
`

// Target URI.
uri := "http://example.net/members/index.html"


// Is bot allowed to visit this page?
ok := grobotstxt.AgentAllowed(robotsTxt, "FooBot/1.0", uri)

See also AgentsAllowed.

Sitemaps

Additionally, one can also extract all Sitemap URIs from a given robots.txt file:

sitemaps := grobotstxt.Sitemaps(robotsTxt)

Documentation

GoDocs https://godoc.org/github.com/jimsmart/grobotstxt

Testing

To run the tests execute go test inside the project folder.

For a full coverage report, try:

go test -coverprofile=coverage.out && go tool cover -html=coverage.out

Notes

The original library required that the URI passed to the AgentAllowed and AgentsAllowed functions, or to the URI parameter of the standalone binary tool, should follow the encoding/escaping format specified by RFC3986, because the library itself does not perform URI normalisation.

In Go, with its native UTF-8 strings, this requirement is not in line with other commonly used APIs, and is therefore somewhat of a surprising/unexpected behaviour to Go developers.

Because of this, the Go API presented here has been ammended to automatically handle UTF-8 URIs, and performs any necessary normalisation internally.

This is the only behavioural change between grobotstxt and the original C++ library.

License

Like the original library, package grobotstxt is licensed under the terms of the Apache License, Version 2.0.

See LICENSE for more information.

Links

History

  • v1.0.1 (2021-04-19) Updated modules. Switch from Travis CI to GitHub Actions.
  • v1.0.0 (2021-04-18) Tagged as stable.
  • v0.2.1 (2021-01-16) Expose more methods of RobotsMatcher as public. Thanks to anatolym
  • v0.2.0 (2020-04-24) Removed requirement for pre-encoded RFC3986 URIs on front-facing API.
  • v0.1.0 (2020-04-23) Initial release.

grobotstxt's People

Contributors

jimsmart avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.