Skip to content

Commit 97a03c5

Browse files
committed
feat: httpc-next-data cli 추가
1 parent 141c78f commit 97a03c5

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ catcher = ["zstd>=1.5.6.6"]
2828

2929
[project.scripts]
3030
httpc-clean = "httpc._base:_extract_headers_cli"
31+
httpc-next-data = "httpc._base:_extract_next_data_cli"
3132

3233
[tool.uv]
3334
dev-dependencies = [

src/httpc/_base.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,90 @@ def _extract_headers_cli() -> None:
130130
# console.print(headers)
131131

132132

133+
def _extract_next_data_cli() -> None:
134+
import sys
135+
from pathlib import Path
136+
137+
if len(sys.argv) == 1:
138+
print("Enter html text below.")
139+
text = ""
140+
while input_ := input():
141+
text += input_ + "\n"
142+
args = None
143+
else:
144+
from argparse import ArgumentParser
145+
parser = ArgumentParser(description="Extract next data from httpc script.")
146+
parser.add_argument("file", type=Path, default=None, help="Path to the script file.")
147+
parser.add_argument("--include-prefixed", "-p", action="store_true")
148+
parser.add_argument("--include", "-i", action="append", type=str, default=[], help="Include only specific prefixes.")
149+
parser.add_argument("--exclude", "-x", action="append", type=str, default=[], help="Exclude specific prefixes.")
150+
parser.add_argument("--outline", action="store_true", help="Show a outline for the data.")
151+
args = parser.parse_args()
152+
# return print(args)
153+
154+
if args.file:
155+
text = Path(args.file).read_text(encoding="utf-8")
156+
else:
157+
print("Enter html text below.")
158+
text = ""
159+
while input_ := input():
160+
text += input_ + "\n"
161+
args = None
162+
163+
from httpc import ParseTool
164+
from rich.console import Console
165+
166+
console = Console()
167+
data = ParseTool(text).extract_next_data()
168+
169+
if not args:
170+
for item in data:
171+
console.rule(f"[b]{item.hexdigit}[/b]")
172+
console.print(item.value)
173+
return
174+
175+
if args.outline:
176+
from rich.table import Table
177+
178+
table = Table(title="Next Data Outline")
179+
table.add_column("[blue]Hexdigit", style="cyan", no_wrap=True, justify="right")
180+
if args.include_prefixed:
181+
table.add_column("[blue]Prefix", style="magenta", no_wrap=True)
182+
table.add_column("[blue]Length", style="green", justify="right")
183+
table.add_column("[blue]Value Starting", style="green", justify="left")
184+
185+
for item in data:
186+
if args.include and item.hexdigit not in args.include:
187+
continue
188+
if args.exclude and item.hexdigit in args.exclude:
189+
continue
190+
if not args.include_prefixed and item.prefix:
191+
continue
192+
data_raw = json.dumps(item.value, ensure_ascii=False)
193+
truncated = data_raw[:80]
194+
if len(data_raw) < 80:
195+
truncated = truncated + " " + "." * (80 - len(truncated))
196+
if not args.include_prefixed:
197+
table.add_row(item.hexdigit, str(len(data_raw)), truncated)
198+
else:
199+
table.add_row(item.hexdigit, item.prefix, str(len(data_raw)), truncated)
200+
201+
console.print(table)
202+
return
203+
204+
for item in data:
205+
if args.include and item.hexdigit not in args.include:
206+
continue
207+
if args.exclude and item.hexdigit in args.exclude:
208+
continue
209+
if not args.include_prefixed and item.prefix:
210+
continue
211+
console.rule(f"[b]{item.hexdigit} start[/b]")
212+
console.print(item.value)
213+
console.rule(f"[b]{item.hexdigit} end [/b]")
214+
return
215+
216+
133217
class FullDunder:
134218
@abstractmethod
135219
def __getattr__(self, name: str, /):

0 commit comments

Comments
 (0)