Git Product home page Git Product logo

robotspy's Issues

Issue with get_uri when the encoding isn't 'utf-8'

The current implementation of gen_uri would fail with an entry in the robots.txt line which isn't correctly encoded in 'utf-8' additionally it is assuming that all robots.txt files are encoded in 'utf-8' which isn't a safe option. The following version solves the problem:

def gen_uri(self, uri: str):
        """Instantiate a generator from a URI (either http:// or https:// or file:///"""
        try:
            with urllib.request.urlopen(uri) as f:
                self._time = time.time()
                encoding = f.headers.get_content_charset() or "utf-8"
                for line in f:
                    try:
                        yield line.decode(encoding)
                    except UnicodeDecodeError:
                        yield line.decode("utf-8", "replace")
        except urllib.error.HTTPError as err:
            if err.code in (401, 403):
                self.disallow_all = True
                self._errors.append((str(err.code), str(err)))
            elif 400 <= err.code < 500:
                self.allow_all = True
                self._warnings.append((str(err.code), str(err)))
            self._time = 0
        except urllib.error.URLError as err:
            self._errors.append(("", str(err)))

I have tried to submit a pull request but I have no rights 😉

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.