|
import wolframalpha |
|
import requests |
|
import os |
|
import argparse |
|
|
|
|
|
def sanitize_filename(name): |
|
bad_chars = ['[', ']', ',', '/', '\\', '\\w', '\\s', '-', '+', '\"', '\'', '>', '<', ' ', '=', ')', '(', ':', '^'] |
|
for char in bad_chars: |
|
name = name.replace(char, "_") |
|
return name |
|
|
|
|
|
def extract_and_save_images(query, app_id, output_dir): |
|
|
|
client = wolframalpha.Client(app_id) |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
res = client.query(query) |
|
|
|
saved_files = [] |
|
if res['@success']: |
|
try: |
|
|
|
print("<basic_results>") |
|
print(next(res.results).text) |
|
print("</basic_results>") |
|
except StopIteration: |
|
pass |
|
|
|
print("\n\n") |
|
print("<detailed_results>") |
|
for i, pod in enumerate(res.pods): |
|
print(f"\nPod: {pod.title}") |
|
for j, sub in enumerate(pod.subpods): |
|
|
|
if sub.plaintext: |
|
print(f" Subpod {j + 1} Text: {sub.plaintext}") |
|
|
|
|
|
if hasattr(sub, 'img'): |
|
image_url = sub.img.src |
|
try: |
|
|
|
response = requests.get(image_url) |
|
response.raise_for_status() |
|
|
|
|
|
content_type = response.headers.get('content-type') |
|
ext = content_type.split('/')[-1] if content_type else 'png' |
|
|
|
title = sanitize_filename(pod.title)[:20] |
|
sub_title = sanitize_filename(sub.img.title.strip())[:20] |
|
|
|
|
|
filename = f"image_{title}_{sub_title}_{i}_{j}.{ext}" |
|
filepath = os.path.join(output_dir, filename) |
|
|
|
|
|
with open(filepath, 'wb') as f: |
|
f.write(response.content) |
|
|
|
saved_files.append(filepath) |
|
print(f" Saved image: {filepath}") |
|
except requests.RequestException as e: |
|
print(f" Error downloading {image_url}: {e}") |
|
print("</detailed_results>") |
|
else: |
|
print( |
|
"Script ran, but query was not successful. Please try a simpler input (e.g. instead of 'plot rule 30', just say 'rule 30') and try again.") |
|
print("Error: ", res['@error']) |
|
|
|
return saved_files |
|
|
|
|
|
def main(): |
|
|
|
parser = argparse.ArgumentParser( |
|
description="Extract and save images and text from Wolfram Alpha based on a query.") |
|
parser.add_argument("-q", "--query", type=str, required=True, help="The query to send to Wolfram Alpha") |
|
parser.add_argument("-o", "--output_dir", "--file", default="wolfram_images", |
|
help="Output directory for saved images (default: wolfram_images)") |
|
parser.add_argument("-a", "--appid", help="Your Wolfram Alpha App ID") |
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
app_id = args.appid or os.environ.get("WOLFRAM_ALPHA_APPID") |
|
if not app_id: |
|
parser.error( |
|
"Wolfram Alpha App ID must be provided either as an argument or as WOLFRAM_ALPHA_APP_ID environment variable") |
|
|
|
try: |
|
print(f"Query: {args.query}\n") |
|
saved_files = extract_and_save_images(args.query, app_id, args.output_dir) |
|
print(f"\nSummary: Saved {len(saved_files)} images to {args.output_dir}/") |
|
except Exception as e: |
|
print(f"An error occurred: {str(e)}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|