Skip to content

Commit d68145e

Browse files
committed
Add URL input support using --url argument
1 parent 816a1ce commit d68145e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

fact_checker_cli/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ A command-line tool that identifies false or misleading claims in articles or st
1616
### 1. Install required dependencies
1717

1818
```bash
19-
pip install requests pydantic
19+
# Ensure you are using the same pip associated with the python3 you intend to run the script with
20+
pip install requests pydantic newspaper3k
2021
```
2122

2223
### 2. Make the script executable
@@ -76,6 +77,12 @@ This will analyze the claim, research it using Perplexity's Sonar API, and retur
7677
./fact_checker.py --file article.txt
7778
```
7879

80+
### Check an article from a URL
81+
82+
```bash
83+
./fact_checker.py --url https://www.example.com/news/article-to-check
84+
```
85+
7986
### Specify a different model
8087

8188
```bash
@@ -156,4 +163,4 @@ Claim 1: ❌ FALSE
156163
- The accuracy of fact-checking depends on the quality of information available through the Perplexity Sonar API.
157164
- Like all language models, the underlying AI may have limitations in certain specialized domains.
158165
- The structured outputs feature requires a Tier 3 or higher Perplexity API account.
159-
- The tool does not replace professional fact-checking services for highly sensitive or complex content.
166+
- The tool does not replace professional fact-checking services for highly sensitive or complex content.

fact_checker_cli/fact_checker.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,10 @@ def main():
296296
input_group = parser.add_mutually_exclusive_group(required=True)
297297
input_group.add_argument("-t", "--text", type=str, help="Text to fact check")
298298
input_group.add_argument("-f", "--file", type=str, help="Path to file containing text to fact check")
299+
input_group.add_argument("-u", "--url", type=str, help="URL of the article to fact check")
299300

300301
parser.add_argument(
301-
"-m",
302+
"-m",
302303
"--model",
303304
type=str,
304305
default=FactChecker.DEFAULT_MODEL,
@@ -340,9 +341,35 @@ def main():
340341
except Exception as e:
341342
print(f"Error reading file: {e}", file=sys.stderr)
342343
return 1
343-
else:
344+
elif args.url:
345+
try:
346+
print(f"Fetching content from URL: {args.url}", file=sys.stderr)
347+
response = requests.get(args.url, timeout=15) # Add a timeout
348+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
349+
350+
article = Article(url=args.url)
351+
article.download(input_html=response.text)
352+
article.parse()
353+
text = article.text
354+
if not text:
355+
print(f"Error: Could not extract text from URL: {args.url}", file=sys.stderr)
356+
return 1
357+
except RequestException as e:
358+
print(f"Error fetching URL: {e}", file=sys.stderr)
359+
return 1
360+
except ArticleException as e:
361+
print(f"Error parsing article content: {e}", file=sys.stderr)
362+
return 1
363+
except Exception as e: # Catch other potential errors during fetch/parse
364+
print(f"An unexpected error occurred while processing the URL: {e}", file=sys.stderr)
365+
return 1
366+
else: # This corresponds to args.text
344367
text = args.text
345-
368+
369+
if not text: # Ensure text is not empty before proceeding
370+
print("Error: No text found to fact check.", file=sys.stderr)
371+
return 1
372+
346373
print("Fact checking in progress...", file=sys.stderr)
347374
results = fact_checker.check_claim(
348375
text,

0 commit comments

Comments
 (0)