Skip to content

Commit fc49f9a

Browse files
committed
chg: make "pages" key in HAR file optional
Support HAR files created by vvakame/go-harlog.
1 parent 02e2cbe commit fc49f9a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

har2tree/har2tree.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ def __init__(self, harfile: Path, capture_uuid: str):
159159
# Used to find the root entry of a page in the capture
160160
# NOTE 2020-05-19: Turns out multiple pages can have the exact same timestamp...
161161
self.pages_start_times: dict[str, list[dict[str, Any]]] = defaultdict(list)
162-
for page in self.har['log']['pages']:
163-
self.pages_start_times[page['startedDateTime']].append(page)
164-
# The first entry has a different start time as the one from the list, add that
165-
if self.entries:
166-
self.pages_start_times[self.initial_start_time].append(self.har['log']['pages'][0])
162+
if 'pages' in self.har['log']:
163+
for page in self.har['log']['pages']:
164+
self.pages_start_times[page['startedDateTime']].append(page)
165+
# The first entry has a different start time as the one from the list, add that
166+
if self.entries:
167+
self.pages_start_times[self.initial_start_time].append(self.har['log']['pages'][0])
167168

168169
# Set to false if initial_redirects fails to find the chain.
169170
self.need_tree_redirects = False
@@ -200,7 +201,7 @@ def number_entries(self) -> int:
200201
@property
201202
def initial_title(self) -> str:
202203
"""Title of the first page in the capture"""
203-
if self.har['log']['pages'][0]['title']:
204+
if 'pages' in self.har['log'] and self.har['log']['pages'][0]['title']:
204205
return self.har['log']['pages'][0]['title']
205206
else:
206207
return '!! No title found !!'
@@ -670,10 +671,12 @@ def _make_subtree_fallback(self, node: URLNode, dev_debug: bool=False) -> None:
670671

671672
# Sometimes, the har has a list of pages, generally when we have HTTP redirects.
672673
# IF we have more than one page in the list
673-
# AND the orphan node's pageref points to an other page than the first one <= FIXME not enabled yet
674+
# AND the orphan node's pageref points to an other page than the first one
674675
# AND we already have a node in the tree with this pageref
675676
# => attach to that node.
676-
if len(self.har.har['log']['pages']) > 1 and node.pageref != self.har.har['log']['pages'][0] and self.pages_root[node.pageref] != node.uuid:
677+
if ('pages' in self.har.har['log'] and len(self.har.har['log']['pages']) > 1
678+
and node.pageref != self.har.har['log']['pages'][0]
679+
and self.pages_root[node.pageref] != node.uuid):
677680
# In that case, we check if there is already a page with the pageref of the orphan node,
678681
# and attach the node to that. NOTE: we can only do that if there is already a node with this pageref in the tree.
679682
# This node is not a page root, we can attach it \o/
@@ -689,7 +692,7 @@ def _make_subtree_fallback(self, node: URLNode, dev_debug: bool=False) -> None:
689692
if dev_debug:
690693
self.logger.warning(f'Failed to attach URLNode in the normal process, attaching node to final redirect: {self.har.final_redirect}.')
691694
self._make_subtree(self.url_tree.search_nodes(name=self.har.final_redirect)[0], [node])
692-
else:
695+
elif 'pages' in self.har.har['log']:
693696
# No luck, the node is root for this pageref, let's attach it to the prior page in the list, or the very first node (tree root)
694697
page_before = self.har.har['log']['pages'][0]
695698
for page in self.har.har['log']['pages'][1:]:
@@ -711,6 +714,9 @@ def _make_subtree_fallback(self, node: URLNode, dev_debug: bool=False) -> None:
711714
page_root_node = self.url_tree
712715
self.logger.warning('The pages in the HAR are in in the wrong order, this should not happen but here we are')
713716
self._make_subtree(page_root_node, [node])
717+
else:
718+
# no way to attach it to anything else, attach to the root node
719+
self._make_subtree(self.url_tree, [node])
714720

715721
@trace_make_subtree
716722
def _make_subtree(self, root: URLNode, nodes_to_attach: list[URLNode] | None=None, dev_debug: bool=False) -> None:

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)