<commit_before>.. image:: https://raw.githubusercontent.com/oduwsdl/ipwb/master/docs/logo.png
    :target: https://pypi.python.org/pypi/ipwb

InterPlanetary Wayback (ipwb)
=============================
Peer-To-Peer Permanence of Web Archives
---------------------------------------

|travis| |pypi| |codecov|

InterPlanetary Wayback (ipwb) facilitates permanence and collaboration in web archives by disseminating the contents of `WARC`_ files into the IPFS network. `IPFS`_ is a peer-to-peer content-addressable file system that inherently allows deduplication and facilitates opt-in replication. ipwb splits the header and payload of WARC response records before disseminating into IPFS to leverage the deduplication, builds a `CDXJ index`_ with references to the IPFS hashes returns, and combines the header and payload from IPFS at the time of replay.

InterPlanetary Wayback primarily consists of two scripts:

- **ipwb/indexer.py** - archival indexing script that takes the path to a WARC input, extracts the HTTP headers, HTTP payload (response body), and relevant parts of the WARC-response record header from the WARC specified and creates byte string representations. The indexer then pushes the byte strings into IPFS using a locally running ipfs daemon then creates a `CDXJ`_ file with this metadata for `replay.py`.
- **ipwb/replay.py** - rudimentary replay script to resolve requests for archival content contained in IPFS for replay in the browser.

A pictorial representation of the ipwb indexing and replay process:

.. image:: https://raw.githubusercontent.com/oduwsdl/ipwb/master/docs/diagram_72.png

Installing
----------
InterPlanetary Wayback requires Python 2.7+ though we are working on having it work on Python 3 as well (see `#51`_).

The latest release of ipwb can be installed using pip:

.. code-block:: bash

      $ pip install ipwb

The latest development version containing changes not yet released can be installed from source:

.. code-block:: bash

      $ git clone https://github.com/oduwsdl/ipwb
      $ cd ipwb
      $ pip install -r requirements.txt
      $ pip install ./

Setup
-----
The InterPlanetary Filesystem (ipfs) daemon must be installed and running before starting ipwb. See the `Install IPFS`_ page to accomplish this. In the future, we hope to make this more automated. Once ipfs is installed, start the daemon:

.. code-block:: bash

      $ ipfs daemon


If you encounter a conflict with the default API port of 5001 when starting the daemon, running the following prior to launching the daemon will change the API port to access to one of your choosing (here, shown to be 5002):

.. code-block:: bash

      $ ipfs config Addresses.API /ip4/127.0.0.1/tcp/5002

Indexing
--------
In a separate terminal session (or the same if you started the daemon in the background), instruct ipwb to push a WARC into IPFS:

.. code-block:: bash

      $ ipwb index (path to warc or warc.gz)


...for example, from the root of the ipwb repository:

.. code-block:: bash

      $ ipwb index ipwb/samples/warcs/salam-home.warc


`indexer.py`, the default script called by the ipwb binary, parititions the WARC into WARC Records, extracts the WARC Response headers, HTTP response headers, and HTTP response body (payload). Relevant information is extracted from the WARC Response headers, temporary byte strings are created for the HTTP response headers and payload, and these two bytes strings are pushed into IPFS. The resulting CDXJ data is written to `stdout` by default but can be redirected to a file, e.g.,

.. code-block:: bash

      $ ipwb index (path to warc or warc.gz) >> myArchiveIndex.cdxj

.. (TODO: add info about specifying the out file as a parameter)

Replaying
---------
.. (TODO: add more detailed info in this section, better sample data with more URIs in the repo for better demonstration of ipwb functionality)

An archival replay system is also included with ipwb to re-experience the content disseminated to IPFS . The replay system can be launched using the provided sample data with:

.. code-block:: bash

      $ ipwb replay

A CDXJ index can also be provided and used by the ipwb replay system by specifying the path of the index file as a parameter to the replay system:

.. code-block:: bash

      $ ipwb replay <path/to/cdxj>

ipwb also supports using an IPFS hash or any HTTP location as the source of the CDXJ:

.. code-block:: bash

      $ ipwb replay http://myDomain/files/myIndex.cdxj
      $ ipwb replay QmYwAPJzv5CZsnANOTaREALhashYgPpHdWEz79ojWnPbdG

Once started, the replay system's web interface can be accessed through a web browser, e.g., http://localhost:5000/ by default.

.. (TODO: provide instructions on specifying a CDXJ file/directory to be read from the CDXJ replay system)

Using Docker
------------

A pre-built Docker image is made available that can be run as following:

.. code-block:: bash

      $ docker container run -it --rm -p 5000:5000 oduwsdl/ipwb

The container will run an IPFS daemon, index a sample WARC file, and replay it using the newly created index.
It will take a few seconds to be ready, then the replay will be accessible at http://localhost:5000/ with a sample archived page.
To index and replay your own WARC file, mount your WARC store at `/warc` using `-v` (or `--volume`) flag and run the following command:

.. code-block:: bash

      $ docker container run -it --rm -v /path/to/warc/folder:/warc oduwsdl/ipwb ipwb index /warc/custom.warc.gz > /cdxj/custom.cdxj
      $ docker container run -it --rm -p 5000:5000 oduwsdl/ipwb ipwb replay /cdxj/custom.cdxj

Generated index files (CDXJ) and the IPFS store can also be persisted using volumes.
Bind mount corresponding host directories at `/cdxj` and `/ipfs`.

To build an image from the source, run the following command from the directory where the source code is checked out.

.. code-block:: bash

      $ docker image build -t ipwb .

Help
-------------
Usage of sub-commands in ipwb can be accessed through providing the `-h` or `--help` flag, like any of the below.

.. code-block:: bash

      $ ipwb -h
      usage: ipwb [-h] [-d DAEMON_ADDRESS] [-o OUTFILE] [-v] {index,replay} ...

      InterPlanetary Wayback (ipwb)

      optional arguments:
        -h, --help            show this help message and exit<commit_msg>Elaborated on mounting mechanizm with simplified example<commit_after>.. image:: https://raw.githubusercontent.com/oduwsdl/ipwb/master/docs/logo.png
    :target: https://pypi.python.org/pypi/ipwb

InterPlanetary Wayback (ipwb)
=============================
Peer-To-Peer Permanence of Web Archives
---------------------------------------

|travis| |pypi| |codecov|

InterPlanetary Wayback (ipwb) facilitates permanence and collaboration in web archives by disseminating the contents of `WARC`_ files into the IPFS network. `IPFS`_ is a peer-to-peer content-addressable file system that inherently allows deduplication and facilitates opt-in replication. ipwb splits the header and payload of WARC response records before disseminating into IPFS to leverage the deduplication, builds a `CDXJ index`_ with references to the IPFS hashes returns, and combines the header and payload from IPFS at the time of replay.

InterPlanetary Wayback primarily consists of two scripts:

- **ipwb/indexer.py** - archival indexing script that takes the path to a WARC input, extracts the HTTP headers, HTTP payload (response body), and relevant parts of the WARC-response record header from the WARC specified and creates byte string representations. The indexer then pushes the byte strings into IPFS using a locally running ipfs daemon then creates a `CDXJ`_ file with this metadata for `replay.py`.
- **ipwb/replay.py** - rudimentary replay script to resolve requests for archival content contained in IPFS for replay in the browser.

A pictorial representation of the ipwb indexing and replay process:

.. image:: https://raw.githubusercontent.com/oduwsdl/ipwb/master/docs/diagram_72.png

Installing
----------
InterPlanetary Wayback requires Python 2.7+ though we are working on having it work on Python 3 as well (see `#51`_).

The latest release of ipwb can be installed using pip:

.. code-block:: bash

      $ pip install ipwb

The latest development version containing changes not yet released can be installed from source:

.. code-block:: bash

      $ git clone https://github.com/oduwsdl/ipwb
      $ cd ipwb
      $ pip install -r requirements.txt
      $ pip install ./

Setup
-----
The InterPlanetary Filesystem (ipfs) daemon must be installed and running before starting ipwb. See the `Install IPFS`_ page to accomplish this. In the future, we hope to make this more automated. Once ipfs is installed, start the daemon:

.. code-block:: bash

      $ ipfs daemon


If you encounter a conflict with the default API port of 5001 when starting the daemon, running the following prior to launching the daemon will change the API port to access to one of your choosing (here, shown to be 5002):

.. code-block:: bash

      $ ipfs config Addresses.API /ip4/127.0.0.1/tcp/5002

Indexing
--------
In a separate terminal session (or the same if you started the daemon in the background), instruct ipwb to push a WARC into IPFS:

.. code-block:: bash

      $ ipwb index (path to warc or warc.gz)


...for example, from the root of the ipwb repository:

.. code-block:: bash

      $ ipwb index ipwb/samples/warcs/salam-home.warc


`indexer.py`, the default script called by the ipwb binary, parititions the WARC into WARC Records, extracts the WARC Response headers, HTTP response headers, and HTTP response body (payload). Relevant information is extracted from the WARC Response headers, temporary byte strings are created for the HTTP response headers and payload, and these two bytes strings are pushed into IPFS. The resulting CDXJ data is written to `stdout` by default but can be redirected to a file, e.g.,

.. code-block:: bash

      $ ipwb index (path to warc or warc.gz) >> myArchiveIndex.cdxj

.. (TODO: add info about specifying the out file as a parameter)

Replaying
---------
.. (TODO: add more detailed info in this section, better sample data with more URIs in the repo for better demonstration of ipwb functionality)

An archival replay system is also included with ipwb to re-experience the content disseminated to IPFS . The replay system can be launched using the provided sample data with:

.. code-block:: bash

      $ ipwb replay

A CDXJ index can also be provided and used by the ipwb replay system by specifying the path of the index file as a parameter to the replay system:

.. code-block:: bash

      $ ipwb replay <path/to/cdxj>

ipwb also supports using an IPFS hash or any HTTP location as the source of the CDXJ:

.. code-block:: bash

      $ ipwb replay http://myDomain/files/myIndex.cdxj
      $ ipwb replay QmYwAPJzv5CZsnANOTaREALhashYgPpHdWEz79ojWnPbdG

Once started, the replay system's web interface can be accessed through a web browser, e.g., http://localhost:5000/ by default.

.. (TODO: provide instructions on specifying a CDXJ file/directory to be read from the CDXJ replay system)

Using Docker
------------

A pre-built Docker image is made available that can be run as following:

.. code-block:: bash

      $ docker container run -it --rm -p 5000:5000 oduwsdl/ipwb

The container will run an IPFS daemon, index a sample WARC file, and replay it using the newly created index.
It will take a few seconds to be ready, then the replay will be accessible at http://localhost:5000/ with a sample archived page.

To index and replay your own WARC file, bind mount your data folders inside the container using `-v` (or `--volume`) flag and run commands accordingly. The provided docker image has designated `/data` directory, inside which there are `warc`, `cdxj`, and `ipfs` folders where host folders can be mounted separately or as a single mount point at the parent `/data` directory. Assuming that the host machine has a `/path/to/data` folder under which there are `warc`, `cdxj`, and `ipfs` folders and a WARC file at `/path/to/data/warc/custom.warc.gz`.

.. code-block:: bash

      $ docker container run -it --rm -v /path/to/data:/data oduwsdl/ipwb ipwb index /data/warc/custom.warc.gz -o /data/cdxj/custom.cdxj
      $ docker container run -it --rm -v /path/to/data:/data -p 5000:5000 oduwsdl/ipwb ipwb replay /data/cdxj/custom.cdxj

If the host folder structure is something other than `/some/path/{warcc,cdxj,ipfs}` then these volumes need to be mounted separately.

To build an image from the source, run the following command from the directory where the source code is checked out.

.. code-block:: bash

      $ docker image build -t ipwb .

Help
-------------
Usage of sub-commands in ipwb can be accessed through providing the `-h` or `--help` flag, like any of the below.

.. code-block:: bash

      $ ipwb -h
      usage: ipwb [-h] [-d DAEMON_ADDRESS] [-o OUTFILE] [-v] {index,replay} ...

      InterPlanetary Wayback (ipwb)

      optional arguments:
        -h, --help            show this help message and exit
{'fraction_non_alphanumeric': 0.08835893908125048, 'fraction_numerical': 0.00810211725139494, 'mean_word_length': 3.418777440054036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '161937', 'n_tokens_mistral': 3859, 'n_tokens_neox': 3582, 'n_words': 1771}