A tool for displaying and manipulating Private Web Archive Web Request+Response (pwebarc WRR) files of Private Web Archive (pwebarc) project
Project description
What?
wrrarms
(pwebarc-wrrarms
) is a tool for displaying and manipulating Private Web Archive Web Request+Response (pwebarc WRR) files produced by pWebArc browser extension.
Quickstart
Installation
- Install with:
pip install pwebarc-wrrarms
and run aswrrarms --help
- Alternatively, install it via Nix
nix-env -i -f ./default.nix
- Alternatively, run without installing:
python3 -m wrrarms --help
How to use
See the examples section for examples.
Usage
wrrarms
A tool to pretty-print, compute and print values from, search, organize (programmatically rename/move/symlink/hardlink files), (WIP: check, deduplicate, and edit) pWebArc WRR (WEBREQRES, Web REQuest+RESponse) archive files.
Terminology: a reqres
(Reqres
when a Python type) is an instance of a structure representing HTTP request+response pair with some additional metadata.
-
options:
--version
: show program's version number and exit-h, --help
: show this help message and exit--markdown
: show help messages formatted in Markdown
-
subcommands:
{pprint,get,run,find,organize,stream}
pprint
: pretty-print WRR filesget
: print an expression computed from a WRR file to stdoutrun
: spawn a process on generated temporary files produced from expressions computed on WRR filesfind
: print paths of WRR files matching specified criteriaorganize
: rename/hardlink/symlink WRR files based on their metadatastream
: produce a stream structured lists containing expressions computed from specified WRR files to stdout, a generalizedwrrarms get
wrrarms pprint
Pretty-print given WRR files to stdout.
-
positional arguments:
PATH
: inputs, can be a mix of files and directories (which will be traversed recursively)
-
options:
--errors {fail,skip,ignore}
: when an error occurs:fail
: report failure and stop the execution (default)skip
: report failure but skip the reqres that produced it from the output and continueignore
:skip
, but don't report the failure
-u, --unabridged
: print all data in full--abridged
: shorten long strings for brevity (useful when you want to visually scan through batch data dumps) (default)
-
filters:
--or EXPR
: only work on reqres which match any of these expressions...--and EXPR
: ... and all of these expressions, both can be specified multiple times, both use the same expression format aswrrarms get --expr
, which see
wrrarms get
Compute an expression EXPR for a reqres stored at PATH and then print it to stdout.
-
positional arguments:
PATH
: input WRR file path
-
options:
-e EXPR, --expr EXPR
: an expression to compute (default:response.body|es
), a state-transformer (pipeline) which starts from valueNone
and applies to it a program built from the following:- constants and functions:
es
: replaceNone
value with an empty string""
eb
: replaceNone
value with an empty byte stringb""
false
: replaceNone
value withFalse
true
: replaceNone
value withTrue
0
: replaceNone
value with0
1
: replaceNone
value with1
not
: apply logicalnot
to valuelen
: applylen
to valuestr
: cast value tostr
or failbytes
: cast value tobytes
or failbool
: cast value tobool
or failint
: cast value toint
or failfloat
: cast value tofloat
or failunquote
: percent-encoding-unquote valueunquote_plus
: percent-encoding-unquote value and replace+
symbols with spacessha256
: computehex(sha256(value.encode("utf-8"))
==
: apply== arg
,arg
is cast to the same type as the current value!=
: apply!= arg
, similarly<
: apply< arg
, similarly<=
: apply<= arg
, similarly>
: apply> arg
, similarly>=
: apply>= arg
, similarlyprefix
: take firstarg
characterssuffix
: take lastarg
charactersabbrev
: leave the current value as if if its length is less or equal thanarg
characters, otherwise take firstarg/2
followed by lastarg/2
characters
- reqres fields, these work the same way as constants above, i.e. they replace current value of
None
with field's value, if reqres is missing the field in question, which could happen forresponse*
fields, the result isNone
:version
: WEBREQRES format version; intsource
:+
-separated list of applications that produced this reqres; strprotocol
: protocol (e.g."HTTP/1.0"
,"HTTP/2.0"
); strrequest.started_at
: request start time in milliseconds since 1970-01-01 00:00; EpochMsecrequest.method
: request HTTP method ("GET"
,"POST"
, etc); strrequest.url
: request URL, including the fragment/hash part; strrequest.headers
: request headers; list[tuple[str, bytes]]request.complete
: is request body complete?; boolrequest.body
: request body; bytesresponse.started_at
: response start time in milliseconds since 1970-01-01 00:00; EpochMsecresponse.code
: HTTP response code (like200
,404
, etc); intresponse.reason
: HTTP response reason (like"OK"
,"Not Found"
, etc); usually empty for Chromium and filled for Firefox; strresponse.headers
: response headers; list[tuple[str, bytes]]response.complete
: is response body complete?; boolresponse.body
: response body; Firefox gives raw bytes, Chromium gives UTF-8 encoded strings; bytes | strfinished_at
: request completion time in milliseconds since 1970-01-01 00:00; EpochMsec
- derived attributes:
fs_path
: file system path for the WRR file containing this reqres; strstime_ms
: aliast forrequest.started_at
; intstime
:stime_ms
rounded down to seconds (UNIX epoch); intstime_msq
: three least significant digits ofstime_ms
; intsyear
: year number ofgmtime(rtime)
(UTC year number ofrtime
); intsmonth
: month number ofgmtime(rtime)
; intsday
: day of the month ofgmtime(rtime)
; intshour
: hour ofgmtime(rtime)
in 24h format; intsminute
: minute ofgmtime(rtime)
; intssecond
: second ofgmtime(rtime)
; intrtime_ms
:response.started_at
if there was a response,finished_at
otherwise; intrtime
:rtime_ms
rounded down to seconds (UNIX epoch); intrtime_msq
: three least significant digits ofrtime_msq
; intryear
: similar tosyear
, but forrtime
; intrmonth
: similar tosmonth
, but forrtime
; intrday
: similar tosday
, but forrtime
; intrhour
: similar toshour
, but forrtime
; intrminute
: similar tosminute
, but forrtime
; intrsecond
: similar tossecond
, but forrtime
; intftime_ms
: aliast forfinished_at
; intftime
:ftime_ms
rounded down to seconds (UNIX epoch); intftime_msq
: three least significant digits offtime_msq
; intfyear
: similar tosyear
, but forftime
; intfmonth
: similar tosmonth
, but forftime
; intfday
: similar tosday
, but forftime
; intfhour
: similar toshour
, but forftime
; intfminute
: similar tosminute
, but forftime
; intfsecond
: similar tossecond
, but forftime
; intstatus
:"NR"
if there was no response,str(response.code) + "C"
if response was complete,str(response.code) + "N"
otherwise; strmethod
: aliast forrequest.method
; strfull_url
: aliast forrequest.url
; strnet_url
:request.url
without the fragment/hash part, if any, this is the URL that actually gets sent to the server; strscheme
: scheme part ofrequest.url
(http
,https
); strnetloc
: netloc part ofrequest.url
(i.e., in the most general case,<username>:<password>@<hostname>:<port>
)hostname
: hostname part ofrequest.url
rhostname
: hostname part ofrequest.url
with the order of parts reversed, e.g."https://www.example.com"
->"com.example.www"
raw_path
: raw path part ofrequest.url
, e.g."https://www.example.com"
->""
,"https://www.example.com/"
->"/"
,"https://www.example.com/index.html"
->"/index.html"
path
:request.url.path_raw
without the leading slash, if any, e.g."https://www.example.com"
->""
,"https://www.example.com/"
->""
,"https://www.example.com/index.html"
->"index.html"
ipath
:path + "index.html"
ifpath
is empty or ends with a slashquery
: query part ofrequest.url
(everything after the?
character and before the#
character)nquery
: normalizedquery
(with empty query parameters removed)oqm
: optional question mark:?
character ifquery
is non-empty and an empty string otherwise
- a compound expression built by piping (
|
) the above, for example:net_url|sha256
net_url|sha256|prefix 4
path|unquote
query|unquote_plus|abbrev 128
response.complete
: this will print the value ofresponse.complete
orNone
, if there was no responseresponse.complete|false
: this will printresponse.complete
orFalse
response.body|eb
: this will printresponse.body
or an empty string, if there was no response
- constants and functions:
wrrarms run
Compute an expression EXPR for each of NUM reqres stored at PATHs, dump the results into into newly created temporary files, spawn a given COMMAND with given arguments ARGs and the resulting temporary file paths appended as the last NUM arguments, wait for it to finish, delete the temporary files, exit with the return code of the spawned process.
-
positional arguments:
COMMAND
: command to spawnARG
: additional arguments to give to the COMMANDPATH
: input WRR file paths to be mapped into new temporary files
-
options:
-e EXPR, --expr EXPR
: the expression to compute, see{__package__} get --expr
for more info on expression format (default:response.body|es
)-n NUM, --num-args NUM
: number of PATHs (default:1
)
wrrarms find
Print paths of WRR files matching specified criteria.
-
positional arguments:
PATH
: inputs, can be a mix of files and directories (which will be traversed recursively)
-
options:
--errors {fail,skip,ignore}
: when an error occurs:fail
: report failure and stop the execution (default)skip
: report failure but skip the reqres that produced it from the output and continueignore
:skip
, but don't report the failure
--stdin0
: read zero-terminated PATHs from stdin, these will be processed after PATHs specified as command-line arguments, requires specified--to
-l, --lf-terminated
: output absolute paths of matching WRR files terminated with\n
(LF) newline characters to stdout (default)-z, --zero-terminated
: output absolute paths of matching WRR files terminated with\0
(NUL) bytes to stdout
-
filters:
--or EXPR
: only work on reqres which match any of these expressions...--and EXPR
: ... and all of these expressions, both can be specified multiple times, both use the same expression format aswrrarms get --expr
, which see
wrrarms organize
Rename/hardlink/symlink given WRR files to DESTINATION based on their metadata.
Operations that could lead to accidental data loss are not permitted.
E.g. wrrarms organize --action rename
will not overwrite any files, which is why the default --output
contains %(num)d
.
-
positional arguments:
PATH
: inputs, can be a mix of files and directories (which will be traversed recursively)
-
options:
--errors {fail,skip,ignore}
: when an error occurs:fail
: report failure and stop the execution (default)skip
: report failure but skip the reqres that produced it from the output and continueignore
:skip
, but don't report the failure
--dry-run
: perform a trial run without actually performing any changes--stdin0
: read zero-terminated PATHs from stdin, these will be processed after PATHs specified as command-line arguments, requires specified--to
-n, --no-output
: don't print anything to stdout (default)-l, --lf-terminated
: output absolute paths of newly produced files terminated with\n
(LF) newline characters to stdout-z, --zero-terminated
: output absolute paths of newly produced files terminated with\0
(NUL) bytes to stdout--action {rename,hardlink,symlink,symlink-update}
: organize how:rename
: rename source files under DESTINATION, will fail if target already exists (default)hardlink
: create hardlinks from source files to paths under DESTINATION, will fail if target already existssymlink
: create symlinks from source files to paths under DESTINATION, will fail if target already existssymlink-update
: create symlinks from source files to paths under DESTINATION, will overwrite the target ifrtime_ms
for the source reqres is newer than the same value for the target
-o FORMAT, --output FORMAT
: format describing the generated output path, an alias name or a custom pythonic %-substitution string:- available aliases and corresponding %-substitutions:
default
:%(ryear)d/%(rmonth)02d/%(rday)02d/%(rhour)02d%(rminute)02d%(rsecond)02d%(rtime_msq)03d_%(stime_ms)s_%(method)s_%(net_url|sha256|prefix 4)s_%(status)s_%(hostname)s.%(num)d.wrr
(default)short
:%(ryear)d/%(rmonth)02d/%(rday)02d/%(rtime_ms)d_%(stime_ms)s.%(num)d.wrr
surl_msn
:%(scheme)s/%(netloc)s/%(path|abbrev 120)s%(oqm)s%(query|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
url_msn
:%(netloc)s/%(path|abbrev 120)s%(oqm)s%(query|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
shpq_msn
:%(scheme)s/%(hostname)s/%(path|abbrev 120)s%(oqm)s%(query|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
hpq_msn
:%(hostname)s/%(path|abbrev 120)s%(oqm)s%(query|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
shupq_msn
:%(scheme)s/%(hostname)s/%(path|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
hupq_msn
:%(hostname)s/%(path|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
srhupq_msn
:%(scheme)s/%(rhostname)s/%(path|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
rhupq_msn
:%(rhostname)s/%(path|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 100)s_%(method)s_%(status)s.%(num)d.wrr
full_shpq
:%(scheme)s/%(hostname)s/%(ipath)s%(oqm)s%(query)s.wrr
full_hpq
:%(hostname)s/%(ipath)s%(oqm)s%(query)s.wrr
shpq
:%(scheme)s/%(hostname)s/%(ipath|abbrev 120)s%(oqm)s%(query|abbrev 120)s.wrr
hpq
:%(hostname)s/%(ipath|abbrev 120)s%(oqm)s%(query|abbrev 120)s.wrr
shupq
:%(scheme)s/%(hostname)s/%(ipath|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 120)s.wrr
hupq
:%(hostname)s/%(ipath|unquote|abbrev 120)s%(oqm)s%(query|unquote_plus|abbrev 120)s.wrr
hupnq
:%(hostname)s/%(ipath|unquote|abbrev 120)s%(oqm)s%(nquery|unquote_plus|abbrev 120)s.wrr
- available substitutions:
num
: number of times an output path like this was seen; this value gets incremened for each new WRR file generating the same path withnum
set to0
and when the file at the path generated with the current value ofnum
already exists; i.e. adding this parameter to your--output
format will ensure all generated file names will be unique- all expressions of
wrrarms get --expr
, which see
- available aliases and corresponding %-substitutions:
-t DESTINATION, --to DESTINATION
: target directory, if not set then each source PATH must be a directory which will be is its own DESTINATION
-
filters:
--or EXPR
: only work on reqres which match any of these expressions...--and EXPR
: ... and all of these expressions, both can be specified multiple times, both use the same expression format aswrrarms get --expr
, which see
wrrarms stream
Compute given expressions for each of given WRR files, encode them into a requested format, and print the result to stdout.
-
positional arguments:
PATH
: inputs, can be a mix of files and directories (which will be traversed recursively)
-
options:
--errors {fail,skip,ignore}
: when an error occurs:fail
: report failure and stop the execution (default)skip
: report failure but skip the reqres that produced it from the output and continueignore
:skip
, but don't report the failure
-u, --unabridged
: print all data in full--abridged
: shorten long strings for brevity (useful when you want to visually scan through batch data dumps) (default)--format {py,cbor,json,raw}
: generate output in:- py: Pythonic Object Representation aka
repr
(default) - cbor: CBOR (RFC8949)
- json: JavaScript Object Notation aka JSON (binary data can't be represented, UNICODE replacement characters will be used)
- raw: concatenate raw values (termination is controlled by
*-terminated
options)
- py: Pythonic Object Representation aka
-l, --lf-terminated
: terminateraw
output values with\n
(LF) newline characters (default)-z, --zero-terminated
: terminateraw
output values with\0
(NUL) bytes-n, --not-terminated
: don't terminateraw
output values with anything, just concatenate them-e EXPR, --expr EXPR
: an expression to compute, seewrrarms get --expr
for more info on expression format, can be specified multiple times (default:[]
); to dump all the fields of a reqres, specify ".
"
-
filters:
--or EXPR
: only work on reqres which match any of these expressions...--and EXPR
: ... and all of these expressions, both can be specified multiple times, both use the same expression format aswrrarms get --expr
, which see
Examples
-
Pretty-print all reqres in
../dumb_server/pwebarc-dump
using an abridged (for ease of reading and rendering) verbose textual representation:wrrarms pprint ../dumb_server/pwebarc-dump
-
Pipe response body from a given WRR file to stdout:
wrrarms get ../dumb_server/pwebarc-dump/path/to/file.wrr
-
Get first 4 characters of a hex digest of sha256 hash computed on the URL without the fragment/hash part:
wrrarms get -e "net_url|sha256|prefix 4" ../dumb_server/pwebarc-dump/path/to/file.wrr
-
Pipe response body from a given WRR file to stdout, but less efficiently, by generating a temporary file and giving it to
cat
:wrrarms run cat ../dumb_server/pwebarc-dump/path/to/file.wrr
Thus
wrrarms run
can be used to do almost anything you want, e.g.wrrarms run less ../dumb_server/pwebarc-dump/path/to/file.wrr
wrrarms run -- sort -R ../dumb_server/pwebarc-dump/path/to/file.wrr
wrrarms run -n 2 -- diff -u ../dumb_server/pwebarc-dump/path/to/file-v1.wrr ../dumb_server/pwebarc-dump/path/to/file-v2.wrr
-
List paths of all WRR files from
../dumb_server/pwebarc-dump
that contain only complete200 OK
responses with bodies larger than 1K:wrrarms find --and "status|== 200C" --and "response.body|len|> 1024" ../dumb_server/pwebarc-dump
-
Rename all WRR files in
../dumb_server/pwebarc-dump/default
according to their metadata using--output default
(see thewrrarms organize
section for its definition, thedefault
format is designed to be human-readable while causing almost no collisions, thus makingnum
substitution parameter to almost always stay equal to0
, making things nice and deterministic):wrrarms organize ../dumb_server/pwebarc-dump/default
alternatively, just show what would be done
wrrarms organize --dry-run ../dumb_server/pwebarc-dump/default
-
The output of
wrrarms organize --zero-terminated
can be piped intowrrarms organize --stdin0
to perform complex updates. E.g. the following will rename new reqres from../dumb_server/pwebarc-dump
to~/pwebarc/raw
renaming them with--output default
, thefor
loop is there to preserve profiles:for arg in ../dumb_server/pwebarc-dump/* ; do wrrarms organize --zero-terminated --to ~/pwebarc/raw/"$(basename "$arg")" "$arg" done > changes
then, we can reuse
changes
to symlink all new files from~/pwebarc/raw
to~/pwebarc/all
using--output hupq_msn
, which would show most of the URL in the file name:wrrarms organize --stdin0 --action symlink --to ~/pwebarc/all --output hupq_msn < changes
and then, we can reuse
changes
again and use them to update~/pwebarc/latest
, filling it with symlinks pointing to the latest200 OK
complete reqres from~/pwebarc/raw
, similar to whatwget -r
would produce (exceptwget
would do network requests and produce responce bodies, while this will build a file system tree of symlinks to WRR files in/pwebarc/raw
):wrrarms organize --stdin0 --action symlink-update --to ~/pwebarc/latest --output hupq --and "status|== 200C" < changes
Advanced examples
-
Pretty-print all reqres in
../dumb_server/pwebarc-dump
by dumping their whole structure into an abridged Pythonic Object Representation (repr):wrrarms stream --expr . ../dumb_server/pwebarc-dump
wrrarms stream -e . ../dumb_server/pwebarc-dump
-
Pretty-print all reqres in
../dumb_server/pwebarc-dump
using the unabridged verbose textual representation:wrrarms pprint --unabridged ../dumb_server/pwebarc-dump
wrrarms pprint -u ../dumb_server/pwebarc-dump
-
Pretty-print all reqres in
../dumb_server/pwebarc-dump
by dumping their whole structure into the unabridged Pythonic Object Representation (repr) format:wrrarms stream --unabridged --expr . ../dumb_server/pwebarc-dump
wrrarms stream -ue . ../dumb_server/pwebarc-dump
-
Produce a JSON list of
[<file path>, <time it finished loading in milliseconds since UNIX epoch>, <URL>]
tuples (one per reqres) and pipe it intojq
for indented and colored output:wrrarms stream --format=json -ue fs_path -e finished_at -e request.url ../dumb_server/pwebarc-dump | jq .
-
Similarly, but produce a CBOR output:
wrrarms stream --format=cbor -ue fs_path -e finished_at -e request.url ../dumb_server/pwebarc-dump | less
-
Concatenate all response bodies of all the requests in
../dumb_server/pwebarc-dump
:wrrarms stream --format=raw --not-terminated -ue "response.body|es" ../dumb_server/pwebarc-dump | less
-
Print all unique visited URLs, one per line:
wrrarms stream --format=raw --lf-terminated -ue request.url ../dumb_server/pwebarc-dump | sort | uniq
-
Same idea, but using NUL bytes while processing, and prints two URLs per line:
wrrarms stream --format=raw --zero-terminated -ue request.url ../dumb_server/pwebarc-dump | sort -z | uniq -z | xargs -0 -n2 echo
Handling binary data
Trying to use response bodies produced by wrrarms stream --format=json
is likely to result garbled data as JSON can't represent raw sequences of bytes, thus binary data will have to be encoded into UNICODE using replacement characters:
wrrarms stream --format=json -ue . ../dumb_server/pwebarc-dump/path/to/file.wrr | jq .
The most generic solution to this is to use --format=cbor
instead, which would produce a verbose CBOR representation equivalent to the one used by --format=json
but with binary data preserved as-is:
wrrarms stream --format=cbor -ue . ../dumb_server/pwebarc-dump/path/to/file.wrr | less
Or you could just dump raw response bodies separately:
wrrarms stream --format=raw -ue response.body ../dumb_server/pwebarc-dump/path/to/file.wrr | less
wrrarms get ../dumb_server/pwebarc-dump/path/to/file.wrr | less
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pwebarc_wrrarms-0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ebf39d118e43c89411d2bc108b58ccac8bda163dd99709f4f75c41901261ebe |
|
MD5 | 81fcf26f27ea6c7824c9ea8795b0725d |
|
BLAKE2b-256 | 959d3491195137c56775b2c0e4dae99a323a2912862e12a5765243ea34402bf1 |