File size: 2,679 Bytes
64341e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import sys

# Configuration
API_BASE_URL = "https://kalhdrawi-pdf1.hf.space"
# API_BASE_URL = "http://localhost:7860" # Use this for local testing

def convert_and_download(file_path):
    """Method 1: Convert and immediately download the binary file."""
    url = f"{API_BASE_URL}/api/convert"
    print(f"\n--- Method 1: Direct Download ---\nUploading {file_path} to {url}...")
    
    try:
        with open(file_path, 'rb') as f:
            files = {'file': f}
            response = requests.post(url, files=files, stream=True)
            
        if response.status_code == 200:
            # Extract filename from header or default
            output_filename = "downloaded_output.pdf"
            if "content-disposition" in response.headers:
                print(f"Header: {response.headers['content-disposition']}")
            
            with open(output_filename, 'wb') as f:
                for chunk in response.iter_content(chunk_size=8192):
                    f.write(chunk)
            print(f"βœ… Success! File saved to: {output_filename}")
        else:
            print(f"❌ Error {response.status_code}: {response.text}")
            
    except Exception as e:
        print(f"❌ Failed: {e}")

def convert_and_get_link(file_path):
    """Method 2: Convert and get a URL to view/share."""
    url = f"{API_BASE_URL}/api/convert/url"
    print(f"\n--- Method 2: Get View/Download Link ---\nUploading {file_path} to {url}...")
    
    try:
        with open(file_path, 'rb') as f:
            files = {'file': f}
            response = requests.post(url, files=files)
            
        if response.status_code == 200:
            data = response.json()
            print("βœ… Success!")
            print(f"πŸ“„ Filename: {data['filename']}")
            print(f"πŸ”— Direct Link: {data['url']}")
            print("You can open this link in any browser.")
        else:
            print(f"❌ Error {response.status_code}: {response.text}")
            
    except Exception as e:
        print(f"❌ Failed: {e}")

if __name__ == "__main__":
    # Create a dummy file if none exists to test
    target_file = "test_doc.docx"
    import os
    if not os.path.exists(target_file):
        with open(target_file, "w") as f: # Only works for txt content really, but simulates file presence
            f.write("This is a test file.") 
        print(f"Created dummy {target_file} (Note: Real conversion needs a real DOCX)")

    if len(sys.argv) > 1:
        target_file = sys.argv[1]

    convert_and_download(target_file)
    convert_and_get_link(target_file)