返回提交历史
Modified
g4f/api/__init__.py
+2
-3
Modified
g4f/requests/cdp.py
+11
-11
XFEstudio/gpt4free
Update image format
db910f66
代码差异
2 个文件
+13
-14
@@ -1325,11 +1325,10 @@ class Api:
1325
1325
await session.start()
1326
1326
try:
1327
1327
screenshot_path = await session.capture_screenshot(url)
1328
print(f"Screenshot saved to: {screenshot_path}")
1329
1328
return FileResponse(
1330
1329
screenshot_path,
1331
media_type="image/jpeg",
1332
#headers={"Cache-Control": "max-age=8600"},
1330
media_type="image/webp",
1331
headers={"Cache-Control": "max-age=604800"},
1333
1332
)
1334
1333
finally:
1335
1334
await session.close()
@@ -783,7 +783,7 @@ class CDPSession:
783
783
784
784
async def capture_screenshot(self, url: str, n: int = 3) -> AsyncIterator[str]:
785
785
"""Navigate to a URL and capture a screenshot, caching the result."""
786
url_without_suffix = url[:-6] if url.endswith("_2.jpg") or url.endswith("_3.jpg") else url
786
url_without_suffix = url[:-7] if url.endswith("_2.webp") or url.endswith("_3.webp") else url
787
787
url_with_noads = f"{url_without_suffix}&noads={int(time.time())}" if "?" in url_without_suffix else f"{url_without_suffix}?noads={int(time.time())}"
788
788
await self.navigate(url_with_noads)
789
789
@@ -802,11 +802,11 @@ class CDPSession:
802
802
return result
803
803
804
804
async def _capture_screenshot_impl(self, url: str, n: int) -> str:
805
url_without_suffix = url[:-6] if url.endswith("_2.jpg") or url.endswith("_3.jpg") else url
805
url_without_suffix = url[:-7] if url.endswith("_2.webp") or url.endswith("_3.webp") else url
806
806
datekey = datetime.date.today().isoformat()
807
807
screenshot_dir = get_screenshot_dir(datekey)
808
808
# Use original URL for filename to distinguish between similar URLs
809
filepath = os.path.join(screenshot_dir, f"{secure_filename(url_without_suffix.replace('https://', '').replace('http://', '').replace('www.', ''))}{'.jpg' if n == 1 else f'_{n}.jpg'}")
809
filepath = os.path.join(screenshot_dir, f"{secure_filename(url_without_suffix.replace('https://', '').replace('http://', '').replace('www.', ''))}{'.webp' if n == 1 else f'_{n}.webp'}")
810
810
if os.path.exists(filepath):
811
811
debug.log(f"Screenshot already exists: {filepath}")
812
812
return filepath
@@ -824,17 +824,17 @@ class CDPSession:
824
824
await self.wait_for_network_idle(idle_time=5, timeout=15.0)
825
825
result = await self.call("Page.captureScreenshot")
826
826
image_bytes = base64.b64decode(result["data"])
827
828
# Resize to 1200x630 and save as JPEG to reduce file size
827
828
# Resize to 1200x630 and save as WebP to reduce file size
829
829
if has_pillow:
830
830
from io import BytesIO
831
831
image = Image.open(BytesIO(image_bytes))
832
832
image = image.resize((1200, 630), Image.Resampling.LANCZOS)
833
833
width, height = image.size
834
834
image = image.crop((0, 0, max(0, width - 14), height))
835
image = image.convert("RGB") # JPEG does not support alpha channel
835
image = image.convert("RGB")
836
836
output = BytesIO()
837
image.save(output, format="JPEG", quality=85, optimize=True)
837
image.save(output, format="WEBP", quality=85, method=6)
838
838
image_bytes = output.getvalue()
839
839
840
840
# Try to click any "Accept" or "Einwilligen" cookie consent buttons
@@ -1152,7 +1152,7 @@ class SyncCDPSession:
1152
1152
"""Navigate to a URL and capture a screenshot, caching the result."""
1153
1153
datekey = datetime.date.today().isoformat()
1154
1154
screenshots_dir = get_screenshot_dir(datekey)
1155
filename = f"{secure_filename(url)}.jpg"
1155
filename = f"{secure_filename(url)}.webp"
1156
1156
filepath = os.path.join(screenshots_dir, filename)
1157
1157
1158
1158
if os.path.exists(filepath):
@@ -1167,16 +1167,16 @@ class SyncCDPSession:
1167
1167
result = self.call("Page.captureScreenshot")
1168
1168
image_bytes = base64.b64decode(result["data"])
1169
1169
1170
# Resize to 1200x675 and save as JPEG to reduce file size
1170
# Resize to 1200x675 and save as WebP to reduce file size
1171
1171
if has_pillow:
1172
1172
from io import BytesIO
1173
1173
image = Image.open(BytesIO(image_bytes))
1174
1174
image = image.resize((1200, 675), Image.Resampling.LANCZOS)
1175
1175
width, height = image.size
1176
1176
image = image.crop((0, 0, max(0, width - 10), height))
1177
image = image.convert("RGB") # JPEG does not support alpha channel
1177
image = image.convert("RGB")
1178
1178
output = BytesIO()
1179
image.save(output, format="JPEG", quality=85, optimize=True)
1179
image.save(output, format="WEBP", quality=85, method=6)
1180
1180
image_bytes = output.getvalue()
1181
1181
1182
1182
Path(filepath).write_bytes(image_bytes)