GithubHelp home page GithubHelp logo

newblue / robotstxt.go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from temoto/robotstxt

1.0 2.0 0.0 88 KB

The robots.txt exclusion protocol implementation for Go language #golang

License: MIT License

robotstxt.go's Introduction

What

This is a robots.txt exclusion protocol implementation for Go language (golang).

Usage

1. Parse

First of all, you need to parse robots.txt data. You can do it with function `FromString(body string) (*RobotsData, os.Error)`:

robots, err := robotstxt.FromString("User-agent: *\nDisallow:")

There is a convenient function FromResponse(statusCode int, body string) (*RobotsData, os.Error) to init robots data from HTTP response status code and body:

robots, err := robotstxt.FromResponse(resp.StatusCode, resp.Body)
if err != nil {
    // robots.txt parse error
    return false, err
}

Passing status code applies following trivial logic:

  • status code = 401, 403 -> disallow all
  • status code = 404 -> allow all
  • all other statuses -> parse body with FromString and apply rules listed there.

2. Query

Parsing robots.txt content builds a kind of logic database, which you can query with (r *RobotsData) TestAgent(url, agent string) (bool, os.Error).

Explicit passing of agent is useful if you want to query for different agents. For single agent users there is a convenient option: (r *RobotsData) Test(url) (bool, os.Error) which is identical to TestAgent, but uses r.DefaultAgent as user agent for each query.

Query parsed robots data with explicit user agent.

allow, err := robots.TestAgent("/", "FooBot")
if err != nil {
    // robots.txt check error
    return false, err
}

Or with implicit user agent.

robots.DefaultAgent = "OtherBot"
allow, err := robots.TestAgent("/")
if err != nil {
    // robots.txt check error
    return false, err
}

robotstxt.go's People

Contributors

temoto avatar igrigorik avatar

Stargazers

newblue avatar

Watchers

newblue avatar James Cloos 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.