-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_download.ruff
More file actions
149 lines (138 loc) · 5.08 KB
/
Copy pathhttp_download.ruff
File metadata and controls
149 lines (138 loc) · 5.08 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
# HTTP Binary Download Demo
#
# This example demonstrates how to download binary files via HTTP
# using the http_get_binary() function.
#
# Note: This example uses placeholder URLs. In real usage, replace
# with actual URLs to downloadable binary resources.
print("=== HTTP Binary Download Demo ===")
print()
print("This example demonstrates downloading binary files via HTTP.")
print("The http_get_binary() function is used to fetch binary data")
print("such as images, PDFs, archives, and other non-text files.")
print()
# Example 1: Basic binary download (conceptual)
print("Example 1: Download an image file")
print("-------------------------------------------")
print("Code example:")
print()
print(" # Download an image from a URL")
print(" image_url := \"https://example.com/photo.jpg\"")
print(" image_data := http_get_binary(image_url)")
print()
print(" # Save it to a local file")
print(" write_binary_file(\"photo.jpg\", image_data)")
print(" print(\"Downloaded and saved photo.jpg\")")
print()
# Example 2: PDF download
print("Example 2: Download a PDF document")
print("-------------------------------------------")
print("Code example:")
print()
print(" # Download a PDF file")
print(" pdf_url := \"https://example.com/document.pdf\"")
print(" pdf_data := http_get_binary(pdf_url)")
print()
print(" # Save the PDF")
print(" write_binary_file(\"document.pdf\", pdf_data)")
print(" print(\"Downloaded: \" + len(pdf_data) + \" bytes\")")
print()
# Example 3: Archive file download
print("Example 3: Download and save a ZIP archive")
print("-------------------------------------------")
print("Code example:")
print()
print(" # Download a ZIP file")
print(" zip_url := \"https://example.com/archive.zip\"")
print(" zip_data := http_get_binary(zip_url)")
print()
print(" # Save to disk")
print(" write_binary_file(\"archive.zip\", zip_data)")
print()
# Example 4: Download with error handling
print("Example 4: Error handling")
print("-------------------------------------------")
print("Code example:")
print()
print(" # Try to download with error handling")
print(" let url := \"https://example.com/file.bin\"")
print(" let result := http_get_binary(url)")
print()
print(" # Check if download was successful")
print(" # (result will be Error type if failed)")
print(" write_binary_file(\"downloaded_file.bin\", result)")
print()
# Example 5: AI-generated image download
print("Example 5: Download AI-generated image")
print("-------------------------------------------")
print("Use case: After generating an image with DALL-E or")
print(" Stable Diffusion API, download the result")
print()
print("Code example:")
print()
print(" # After calling image generation API, you get an image URL")
print(" generated_image_url := api_response[\"url\"]")
print()
print(" # Download the generated image")
print(" image_bytes := http_get_binary(generated_image_url)")
print()
print(" # Save it locally")
print(" write_binary_file(\"ai_generated.png\", image_bytes)")
print()
print(" # Or convert to base64 for embedding in HTML/JSON")
print(" base64_image := encode_base64(image_bytes)")
print(" print(\"Base64 image: \" + base64_image)")
print()
# Example 6: Multiple file downloads
print("Example 6: Batch download multiple files")
print("-------------------------------------------")
print("Code example:")
print()
print(" let urls := [")
print(" \"https://example.com/image1.jpg\",")
print(" \"https://example.com/image2.jpg\",")
print(" \"https://example.com/image3.jpg\"")
print(" ]")
print()
print(" let index := 0.0")
print(" for url in urls {")
print(" let data := http_get_binary(url)")
print(" let filename := \"image\" + index + \".jpg\"")
print(" write_binary_file(filename, data)")
print(" print(\"Downloaded: \" + filename)")
print(" index := index + 1.0")
print(" }")
print()
print("=== Common Use Cases ===")
print()
print("1. AI Image Generation:")
print(" • Download images from DALL-E, Midjourney, Stable Diffusion")
print(" • Process and store generated artwork")
print()
print("2. API Integration:")
print(" • Fetch binary assets from REST APIs")
print(" • Download PDFs, reports, exports")
print()
print("3. Media Processing:")
print(" • Download audio files for processing")
print(" • Fetch video thumbnails")
print(" • Get profile pictures from social APIs")
print()
print("4. Backup & Archiving:")
print(" • Download backup files from cloud storage")
print(" • Fetch database exports")
print()
print("5. Web Scraping:")
print(" • Download images from websites")
print(" • Fetch documents, spreadsheets")
print()
print("=== Key Functions ===")
print()
print("• http_get_binary(url) - Download binary data from URL")
print("• write_binary_file(path, data) - Save binary data to file")
print("• read_binary_file(path) - Load binary data from file")
print("• encode_base64(data) - Convert binary to base64 string")
print("• decode_base64(string) - Convert base64 string to binary")
print("• len(binary_data) - Get size of binary data in bytes")
print()
print("Demo complete!")