Repository navigation
Expand file tree
/
Copy pathscrapePage.py
More file actions
executable file
·109 lines (98 loc) · 3.58 KB
/
Copy pathscrapePage.py
File metadata and controls
executable file
·109 lines (98 loc) · 3.58 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
import asyncio
import datetime
import errno
import os
from pathlib import Path
import socket
# https://pypi.org/project/pyppeteer2/
# https://miyakogi.github.io/pyppeteer/reference.html
# One time, download Chromium: $ pyppeteer-install
from pyppeteer import *
import tkinter as tk
from tkinter.messagebox import *
def display_error():
tk.messagebox.showerror(message=\
"Couldn't get FQDN.\nTypo in the FQDN entered?",
title="Message Box", icon="error")
async def scrapeAFQDN(fqdn, my_log_box, url_or_file):
# the snapshot goes to this filespec
# if on something non-Un*x-ish, remember os.path.join(dir, f)
my_log_box.insert(tk.END, "Making sure archive directory exists...\n")
my_log_box.update()
home = str(Path.home())
subdir = "snapshots"
myfilename = fqdn
utcdatetime = datetime.datetime.utcnow().isoformat() + 'Z'
myextension = "jpeg"
mydir = home + "/" + subdir
fullfilenamepart = fqdn + "_" + utcdatetime + "." + myextension
latestversion = fqdn + "_" + "latest" + "." + myextension
# ensure the directory exists
try:
os.makedirs(mydir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# ensure the timestamped file exists
if (url_or_file == "url"):
myfilespec = mydir + "/" + fullfilenamepart
# ensure the directory exists
try:
os.makedirs(mydir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
elif (url_or_file == "file"):
myfilespec = fullfilenamepart
try:
open(myfilespec, 'a').close()
except:
raise
my_log_box.insert(tk.END, "Confirming page is reachable on 443 or 80...\n")
my_log_box.update()
domain_resolve_ok = False
if (url_or_file == "url"):
# verify page exists, and figure out if https is supported
try:
# note: you do NOT need to explicitly permit inbound traffic
# on macOS (even if it asks you to allow it!)
myaddrinfo = socket.getaddrinfo(fqdn, 443)
url = "https://"+fqdn
domain_resolves_ok = True
except socket.gaierror:
# couldn't connect on default SSL/TLS port
# fall back to regular http
try:
myaddrinfo = socket.getaddrinfo(fqdn, 80)
url = "http://"+fqdn
domain_resolves_ok = True
except:
# couldn't even connect on standard port
display_error()
return(1)
elif (url_or_file == "file"):
domain_resolves_ok = True
url = "file://"+fqdn
myfilespec = fqdn + ".jpeg"
if domain_resolves_ok == True:
browser = await launch({'headless': True, 'ignoreHTTPSErrors': True,\
'defaultViewport': None, 'viewport_width': 800})
context = await browser.createIncognitoBrowserContext()
page = await browser.newPage()
await page._client.send('Animation.disable')
await page.goto(url)
await page.screenshot({'path': myfilespec, 'type': 'jpeg', \
'quality': 80, 'fullPage': True})
await browser.close()
# ensure the "latest" version is updated for this URL
if (url_or_file == "url"):
mylatest = mydir + "/" + latestversion
# print("mylatest =" + mylatest)
elif (url_or_file == "file"):
mylatest = myfilespec + "_" + "latest" + "." + myextension
try:
os.unlink(mylatest)
except:
pass
os.symlink(myfilespec, mylatest)
return(myfilespec)