| 001 | #!/usr/bin/env python3 |
|---|
| 002 | # coding: utf-8 |
|---|
| 003 | #/com.cocolog-nifty.quicktimer.icefloe |
|---|
| 004 | import sys |
|---|
| 005 | import os |
|---|
| 006 | import subprocess |
|---|
| 007 | from AppKit import NSWorkspace |
|---|
| 008 | from datetime import datetime |
|---|
| 009 | from urllib.parse import urlparse |
|---|
| 010 | from playwright.sync_api import Page, expect |
|---|
| 011 | from playwright.sync_api import sync_playwright |
|---|
| 012 | |
|---|
| 013 | if len(sys.argv) > 1: |
|---|
| 014 | str_url = sys.argv[1] |
|---|
| 015 | else: |
|---|
| 016 | str_url = "https://note.com/" |
|---|
| 017 | |
|---|
| 018 | str_domain = urlparse(str_url).netloc |
|---|
| 019 | def get_edge_url(): |
|---|
| 020 | workspace = NSWorkspace.sharedWorkspace() |
|---|
| 021 | url = workspace.URLForApplicationWithBundleIdentifier_("com.microsoft.edgemac") |
|---|
| 022 | if url: |
|---|
| 023 | print(url.path()) |
|---|
| 024 | str_app_path = url.path() |
|---|
| 025 | return os.path.join(str_app_path,"Contents/MacOS/Microsoft Edge") |
|---|
| 026 | return "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" |
|---|
| 027 | |
|---|
| 028 | edge_path = get_edge_url() |
|---|
| 029 | |
|---|
| 030 | #ユーザーデータはテンポラリ(起動時に削除される) |
|---|
| 031 | tmp_dir_path = os.environ["TMPDIR"] |
|---|
| 032 | user_data_path = os.path.join(tmp_dir_path, "Microsoft Edge Driver") |
|---|
| 033 | if not os.path.exists(user_data_path): |
|---|
| 034 | os.makedirs(user_data_path, exist_ok=True) |
|---|
| 035 | #テンポラリーに作業ディレクトリを移動 |
|---|
| 036 | os.chdir(user_data_path) |
|---|
| 037 | |
|---|
| 038 | size_viewport={"width": 390, "height": 844} |
|---|
| 039 | |
|---|
| 040 | with sync_playwright() as p: |
|---|
| 041 | app_browser = p.chromium.launch_persistent_context( |
|---|
| 042 | user_data_dir=user_data_path, |
|---|
| 043 | executable_path=edge_path, |
|---|
| 044 | viewport=size_viewport, |
|---|
| 045 | locale='ja-JP', |
|---|
| 046 | timezone_id='Asia/Tokyo', |
|---|
| 047 | user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1", |
|---|
| 048 | device_scale_factor=3.0, |
|---|
| 049 | color_scheme='dark', |
|---|
| 050 | is_mobile=True, |
|---|
| 051 | offline=False, |
|---|
| 052 | java_script_enabled=True, |
|---|
| 053 | headless=True |
|---|
| 054 | ) |
|---|
| 055 | |
|---|
| 056 | |
|---|
| 057 | capture_dir_path_png = os.path.expanduser("~/Pictures/ScreenCapture/ScreenCapture") |
|---|
| 058 | if not os.path.exists(capture_dir_path_png): |
|---|
| 059 | os.makedirs(capture_dir_path_png, exist_ok=True) |
|---|
| 060 | |
|---|
| 061 | str_now_time = datetime.now().strftime("%Y%m%d_%H%M%S") |
|---|
| 062 | |
|---|
| 063 | str_filename_png = f"{str_domain}-{str_now_time}.png" |
|---|
| 064 | save_capture_path_png = os.path.join(capture_dir_path_png, str_filename_png) |
|---|
| 065 | |
|---|
| 066 | capture_dir_path_pdf = os.path.expanduser("~/Downloads/ScreenCapture/") |
|---|
| 067 | if not os.path.exists(capture_dir_path_pdf): |
|---|
| 068 | os.makedirs(capture_dir_path_pdf, exist_ok=True) |
|---|
| 069 | |
|---|
| 070 | str_filename = f"{str_domain}-{str_now_time}.pdf" |
|---|
| 071 | capture_dir_path_pdf = os.path.join(capture_dir_path_pdf, str_filename) |
|---|
| 072 | |
|---|
| 073 | app_page = app_browser.new_page() |
|---|
| 074 | app_page.set_viewport_size(size_viewport) |
|---|
| 075 | app_page.goto(str_url) |
|---|
| 076 | app_page.wait_for_load_state("domcontentloaded") |
|---|
| 077 | app_page.wait_for_load_state("networkidle") |
|---|
| 078 | |
|---|
| 079 | app_page.evaluate(""" |
|---|
| 080 | async () => { |
|---|
| 081 | await new Promise((resolve) => { |
|---|
| 082 | let totalHeight = 0; |
|---|
| 083 | const distance = 300; |
|---|
| 084 | const timer = setInterval(() => { |
|---|
| 085 | window.scrollBy(0, distance); |
|---|
| 086 | totalHeight += distance; |
|---|
| 087 | if (totalHeight >= document.body.scrollHeight) { |
|---|
| 088 | clearInterval(timer); |
|---|
| 089 | window.scrollTo(0, 0); |
|---|
| 090 | resolve(); |
|---|
| 091 | } |
|---|
| 092 | }, 100); |
|---|
| 093 | }); |
|---|
| 094 | } |
|---|
| 095 | """) |
|---|
| 096 | |
|---|
| 097 | app_page.wait_for_load_state("networkidle") |
|---|
| 098 | app_page.wait_for_timeout(1500) |
|---|
| 099 | |
|---|
| 100 | app_page.screenshot( |
|---|
| 101 | path=save_capture_path_png, |
|---|
| 102 | scale='device', |
|---|
| 103 | type='png', |
|---|
| 104 | full_page=False |
|---|
| 105 | ) |
|---|
| 106 | |
|---|
| 107 | app_page.emulate_media(media="screen") |
|---|
| 108 | app_page.pdf( |
|---|
| 109 | path=capture_dir_path_pdf, |
|---|
| 110 | width='390px', |
|---|
| 111 | print_background=True, |
|---|
| 112 | margin={"top": "0", "bottom": "0", "left": "0", "right": "0"}, |
|---|
| 113 | scale=1, |
|---|
| 114 | tagged=True, |
|---|
| 115 | landscape=False |
|---|
| 116 | ) |
|---|
| 117 | |
|---|
| 118 | app_browser.close() |
|---|
| 119 | subprocess.run(["open", capture_dir_path_pdf]) |
|---|
| 120 | |
|---|
| 121 | sys.exit(0) |
|---|