2023-09-13 18:26:51 +02:00
|
|
|
import asyncio
|
|
|
|
import sys
|
|
|
|
from pyppeteer import launch
|
|
|
|
|
|
|
|
async def take_screenshot(url, output_file, width, height):
|
|
|
|
browser = await launch(headless=True)
|
|
|
|
page = await browser.newPage()
|
|
|
|
|
|
|
|
try:
|
|
|
|
await page.setViewport({'width': width, 'height': height}) # Set the viewport size
|
|
|
|
await page.goto(url, waitUntil='networkidle2')
|
|
|
|
await page.screenshot({'path': output_file})
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An error occurred: {e}")
|
|
|
|
finally:
|
|
|
|
await browser.close()
|
|
|
|
|
2023-09-14 13:01:35 +02:00
|
|
|
def main():
|
2023-09-13 18:26:51 +02:00
|
|
|
if len(sys.argv) != 5:
|
|
|
|
print("Usage: python page_screenshot.py <URL> <output_file> <width> <height>")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
url = sys.argv[1]
|
|
|
|
output_file = sys.argv[2]
|
|
|
|
width = int(sys.argv[3])
|
|
|
|
height = int(sys.argv[4])
|
|
|
|
|
2023-09-14 13:01:35 +02:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
2023-09-26 13:52:35 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(asyncio.wait_for(take_screenshot(url, output_file, width, height), timeout=30))
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
print("Timeout: The operation took too long to complete and has been terminated.")
|
|
|
|
finally:
|
|
|
|
loop.close()
|
2023-09-14 13:01:35 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|