Commit e36c4e76 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

vendor and hack pywebcopy

parent 04585b7b
Loading
Loading
Loading
Loading
+3 −1
Changes for pyproject.toml: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -12,8 +12,10 @@ dependencies = [
    "basexclient>=8.4.4",
    "beautifulsoup4>=4.12.3",
    "lxml>=5.3.0",
    "pywebcopy>=7.0.2",
    "lxml-html-clean>=0.2.2",
    "six>=1.16.0",
    "msgpack>=1.1.0",
    "cachecontrol>=0.14.0",
]
readme = "README.md"
requires-python = ">= 3.12"
+3 −2
Changes for src/climax/__main__.py: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -584,13 +584,14 @@ def mirror(
    start(WEB_HOST, port_number, http_stop_port=stop_port, service=True)
    # copy website
    pywebcopy_config["encoding"] = "utf-8"
    start_url = f"http://localhost:{port_number}/{config.languages[0]}/pages/index.html"
    print(start_url)
    try:
        for lang in config.languages:
            start_url = f"http://localhost:{port_number}/{lang}/tdm.html"
            save_website(
                url=start_url,
                project_folder=str(directory),
                project_name=config.title,
                threaded=False,
            )
    except Exception as e:
        stop(stop_port)
+172
Changes for src/pywebcopy/__init__.py: 172 added lines, 0 removed lines.
Original line number Diff line number Diff line
r"""
    ____       _       __     __    ______                     _____
   / __ \__  _| |     / /__  / /_  / ____/___  ____  __  __   /__  /
  / /_/ / / / / | /| / / _ \/ __ \/ /   / __ \/ __ \/ / / /     / /
 / ____/ /_/ /| |/ |/ /  __/ /_/ / /___/ /_/ / /_/ / /_/ /     / /
/_/    \__, / |__/|__/\___/_.___/\____/\____/ .___/\__, /     /_/
      /____/                               /_/    /____/

PyWebCopy is a free tool for copying full or partial websites locally
onto your hard-disk for offline viewing.

PyWebCopy will scan the specified website and download its content onto your hard-disk.
Links to resources such as style-sheets, images, and other pages in the website
will automatically be remapped to match the local path.
Using its extensive configuration you can define which parts of a website will be copied and how.

What can PyWebCopy do?
PyWebCopy will examine the HTML mark-up of a website and attempt to discover all linked resources
such as other pages, images, videos, file downloads - anything and everything.
It will download all of theses resources, and continue to search for more.
In this manner, WebCopy can "crawl" an entire website and download everything it sees
in an effort to create a reasonable facsimile of the source website.

What can PyWebCopy not do?
PyWebCopy does not include a virtual DOM or any form of JavaScript parsing.
If a website makes heavy use of JavaScript to operate, it is unlikely PyWebCopy will be able
to make a true copy if it is unable to discover all of the website due to
JavaScript being used to dynamically generate links.

PyWebCopy does not download the raw source code of a web site,
it can only download what the HTTP server returns.
While it will do its best to create an offline copy of a website,
advanced data driven websites may not work as expected once they have been copied.


# Copyright 2020; Raja Tomar
# See license for more details
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

..todo::
    *1. Fix parser breaks on empty string.
    *2. Fix WebPage links and files method emptying the response stream.
    *3. Fix meta element in the head of the WebPage.
    *4. Add threading flag in the cmd and api.
    *5. Fix the MultiParser should be sub class of GenericElement.
    6. Fix Asynchronous http requests for the sub elements.
    7. Fix infinite nesting of anchor links requests on a single element.
    *8. Fix concurrent delay b/w requests to be domain specific.
"""

import logging
import warnings

logging.getLogger(__name__).addHandler(logging.NullHandler())

__all__ = ["save_website", "save_webpage"]


def save_page(
    url,
    project_folder=None,
    project_name=None,
    bypass_robots=None,
    debug=False,
    open_in_browser=True,
    delay=None,
    threaded=None,
):
    """Easiest way to save any single webpage with images, css and js.

    example::

        from pywebcopy import save_webpage
        save_webpage(
            url="https://httpbin.org/",
            project_folder="E://savedpages//",
            project_name="my_site",
            bypass_robots=True,
            debug=True,
            open_in_browser=True,
            delay=None,
            threaded=False,
        )

    :param url: url of the web page to work with
    :type url: str
    :param project_folder: folder in which the files will be downloaded
    :type project_folder: str
    :param project_name: name of the project to distinguish it
    :type project_name: str | None
    :param bypass_robots: whether to follow the robots.txt rules or not
    :param debug: whether to print deep logs or not.
    :param open_in_browser: whether or not to open a new tab after saving the webpage.
    :type open_in_browser: bool
    :param delay: amount of delay between two concurrent requests to a same server.
    :param threaded: whether to use threading or not (it can break some site).
    """
    from .configs import get_config

    config = get_config(
        url, project_folder, project_name, bypass_robots, debug, delay, threaded
    )
    page = config.create_page()
    page.get(url)
    if threaded:
        warnings.warn("Opening in browser is not supported when threading is enabled!")
        open_in_browser = False
    page.save_complete(pop=open_in_browser)


save_web_page = save_webpage = save_page


def save_website(
    url,
    project_folder=None,
    project_name=None,
    bypass_robots=None,
    debug=False,
    open_in_browser=False,
    delay=None,
    threaded=None,
):
    """Crawls the entire website for html, images, css and js.

    example::

        from pywebcopy import save_website
        save_website(
            url="https://httpbin.org/",
            project_folder="E://savedpages//",
            project_name="my_site",
            bypass_robots=True,
            debug=False,
            open_in_browser=True,
            delay=None,
            threaded=False,
        )

    :param url: url of the web page to work with
    :type url: str
    :param project_folder: folder in which the files will be downloaded
    :type project_folder: str
    :param project_name: name of the project to distinguish it
    :type project_name: str | None
    :param bypass_robots: whether to follow the robots.txt rules or not
    :param debug: whether to print deep logs or not.
    :param open_in_browser: whether or not to open a new tab after saving the webpage.
    :type open_in_browser: bool
    :param delay: amount of delay between two concurrent requests to a same server.
    :param threaded: whether to use threading or not (it can break some site).
    """
    from .configs import get_config

    config = get_config(
        url, project_folder, project_name, bypass_robots, debug, delay, threaded
    )
    crawler = config.create_crawler()
    crawler.get(url)
    if threaded:
        warnings.warn("Opening in browser is not supported when threading is enabled!")
        open_in_browser = False
    crawler.save_complete(pop=open_in_browser)
+121
Changes for src/pywebcopy/__main__.py: 121 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright 2020; Raja Tomar
# See license for more details
import os
import sys
import optparse
import six

from pywebcopy.__version__ import __title__
from pywebcopy.__version__ import __version__
from pywebcopy.__version__ import __description__
from pywebcopy import save_webpage
from pywebcopy import save_website


parser = optparse.OptionParser(
    usage="%prog [-p|--page|-s|--site|-t|--tests] "
    "[--url=URL [,--location=LOCATION [,--name=NAME "
    "[,--pop [,--bypass_robots [,--quite [,--delay=DELAY]]]]]]] ",
    version=__version__,
    prog=__title__,
    description=__description__,
)

#: Multiple saving modes.
options = optparse.OptionGroup(
    parser, "CLI Actions List", "Primary actions available through cli."
)
options.add_option(
    "-p", "--page", action="store_true", help="Quickly saves a single page."
)
options.add_option("-s", "--site", action="store_true", help="Saves the complete site.")
options.add_option(
    "-t", "--tests", action="store_true", help="Runs tests for this library."
)

parser.add_option_group(options)

#: Required params
parser.add_option(
    "--url", type="string", help="url of the entry point to be retrieved."
)
parser.add_option(
    "--location", type="string", help="Location where files are to be stored."
)

#: Optional params
parser.add_option(
    "-n", "--name", default=None, type="string", help="Project name of this run."
)
parser.add_option(
    "-d",
    "--delay",
    type="float",
    help="Delay between consecutive requests to the server.",
)

#: Optional flags
parser.add_option(
    "--bypass_robots",
    default=True,
    action="store_true",
    help="Bypass the robots.txt restrictions.",
)
parser.add_option(
    "--threaded",
    default=False,
    action="store_true",
    help="Use threads for faster downloading.",
)
parser.add_option(
    "-q",
    "--quite",
    default=False,
    action="store_true",
    help="Suppress the logging from this library.",
)
parser.add_option(
    "--pop",
    default=False,
    action="store_true",
    help="open the html page in default browser window after finishing the task.",
)

args, remainder = parser.parse_args()

# type checks
if bool(args.page) or bool(args.site):
    if not args.url or not isinstance(args.url, six.string_types):
        parser.error("--url option requires 1 string type argument")
    if args.location and not isinstance(args.location, six.string_types):
        parser.error("--location option requires 1 string type argument")
    if args.name and not isinstance(args.name, six.string_types):
        parser.error("--name option requires 1 string type argument")

if args.page:
    save_webpage(
        url=args.url,
        project_folder=args.location,
        bypass_robots=args.bypass_robots,
        open_in_browser=args.pop,
        debug=not args.quite,
        delay=args.delay,
        threaded=args.threaded,
    )
elif args.site:
    save_website(
        url=args.url,
        project_folder=args.location,
        bypass_robots=args.bypass_robots,
        open_in_browser=args.pop,
        debug=not args.quite,
        delay=args.delay,
        threaded=args.threaded,
    )
elif args.tests:
    os.system(
        "%s -m unittest discover -s pywebcopy/tests" % os.path.normpath(sys.executable)
    )
else:
    parser.print_help()
    sys.exit(1)
+9
Changes for src/pywebcopy/__version__.py: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright 2020; Raja Tomar
# See license for more details
__title__ = "pywebcopy"
__url__ = "https://github.com/rajatomar788/pywebcopy/"
__author__ = "Raja Tomar"
__email__ = "rajatomar788@gmail.com"
__license__ = "Apache Software License"
__version__ = "7.0.2"
__description__ = "Python library to clone/archive pages or sites from the Internet."
Loading