-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
163 lines (140 loc) · 5.23 KB
/
example.py
File metadata and controls
163 lines (140 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
"""
Example usage of the Instaparser Python Library.
Run a single API call (article, pdf, or summary) with options via argparse.
API token can be passed via --api-key or INSTAPARSER_API_KEY environment variable.
"""
import argparse
import os
import sys
from instaparser import (
InstaparserAPIError,
InstaparserAuthenticationError,
InstaparserClient,
InstaparserRateLimitError,
InstaparserValidationError,
)
def get_api_key(args: argparse.Namespace) -> str:
"""Resolve API key from argument or environment."""
key = getattr(args, "api_key", None) or os.environ.get("INSTAPARSER_API_KEY")
if not key:
print("Error: API key required. Use --api-key or set INSTAPARSER_API_KEY.", file=sys.stderr)
sys.exit(1)
return key
def cmd_article(client: InstaparserClient, args: argparse.Namespace) -> None:
"""Call Article API and print result."""
article = client.article(
url=args.url,
output=args.output,
use_cache=not args.no_cache,
)
print(f"Title: {article.title}")
print(f"Author: {article.author}")
print(f"Words: {article.words}")
if args.output == "text" and article.text:
print(f"Body (first 400 chars): {article.text[:400]}...")
elif article.body:
print(f"Body (first 400 chars): {article.body[:400]}...")
else:
print("Body: (none)")
def cmd_summary(client: InstaparserClient, args: argparse.Namespace) -> None:
"""Call Summary API and print result."""
def on_stream(line: str) -> None:
if args.stream:
print(f" {line}")
summary = client.summary(
url=args.url,
use_cache=not args.no_cache,
stream_callback=on_stream if args.stream else None,
)
print(f"Overview: {summary.overview}")
print("Key sentences:")
for s in summary.key_sentences:
print(f" - {s}")
def cmd_pdf(client: InstaparserClient, args: argparse.Namespace) -> None:
"""Call PDF API and print result."""
if args.file:
with open(args.file, "rb") as f:
pdf = client.pdf(
file=f,
url=args.url or None,
output=args.output,
use_cache=not args.no_cache,
)
else:
if not args.url:
print("Error: --url or --file required for pdf.", file=sys.stderr)
sys.exit(1)
pdf = client.pdf(
url=args.url,
output=args.output,
use_cache=not args.no_cache,
)
print(f"Title: {pdf.title}")
print(f"Words: {pdf.words}")
body = pdf.body or pdf.text or ""
print(f"Body (first 400 chars): {body[:400]}..." if body else "Body: (none)")
def main() -> None:
parser = argparse.ArgumentParser(
description="Instaparser API: run one API call (article, pdf, or summary).",
)
parser.add_argument(
"--api-key",
default=os.environ.get("INSTAPARSER_API_KEY"),
help="API key (default: INSTAPARSER_API_KEY env var)",
)
subparsers = parser.add_subparsers(dest="command", required=True, help="API to call")
# article
article_parser = subparsers.add_parser("article", help="Parse article from URL")
article_parser.add_argument("url", help="Article URL")
article_parser.add_argument(
"--output",
choices=("html", "text", "markdown"),
default="html",
help="Output format (default: html)",
)
article_parser.add_argument("--no-cache", action="store_true", help="Disable cache")
# summary
summary_parser = subparsers.add_parser("summary", help="Summarize article from URL")
summary_parser.add_argument("url", help="Article URL")
summary_parser.add_argument("--no-cache", action="store_true", help="Disable cache")
summary_parser.add_argument(
"--stream",
action="store_true",
help="Stream summary output line by line",
)
# pdf
pdf_parser = subparsers.add_parser("pdf", help="Parse PDF from URL or file")
pdf_parser.add_argument("--url", help="PDF URL")
pdf_parser.add_argument("--file", help="Path to PDF file (alternative to --url)")
pdf_parser.add_argument(
"--output",
choices=("html", "text", "markdown"),
default="html",
help="Output format (default: html)",
)
pdf_parser.add_argument("--no-cache", action="store_true", help="Disable cache")
args = parser.parse_args()
api_key = get_api_key(args)
client = InstaparserClient(api_key=api_key)
try:
if args.command == "article":
cmd_article(client, args)
elif args.command == "summary":
cmd_summary(client, args)
elif args.command == "pdf":
cmd_pdf(client, args)
except InstaparserAuthenticationError:
print("Authentication failed - check your API key", file=sys.stderr)
sys.exit(1)
except InstaparserRateLimitError:
print("Rate limit exceeded - slow down your requests", file=sys.stderr)
sys.exit(1)
except InstaparserValidationError as e:
print(f"Validation error: {e}", file=sys.stderr)
sys.exit(1)
except InstaparserAPIError as e:
print(f"API error: {e} (status: {e.status_code})", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()