-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscreenshoter.py
156 lines (132 loc) · 4.34 KB
/
screenshoter.py
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
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Screenshoter utility application.
Usage: screenshoter.py [-h] [-c <path>] [-v] [-i <path>] [-sw <int>] [-sh <int>] [-f] [-o <path>]
Example usage: python screenshoter.py -i data/screenshots/ALEXA_500/urls.csv -sw 1280 -sh 800 -f -o data/screenshots/results/
"""
# ----------------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------------
# Standard library modules
from pathlib import Path
# Third-party modules
from loguru import logger
# First-party modules
from aim.common import configmanager, constants, utils
from aim.common.constants import (
IMAGE_HEIGHT_DESKTOP,
IMAGE_WIDTH_DESKTOP,
SCREENSHOTER_INPUT_FILE,
SCREENSHOTER_OUTPUT_DIR,
)
from aim.tools import Screenshot
# ----------------------------------------------------------------------------
# Metadata
# ----------------------------------------------------------------------------
__author__ = "Markku Laine"
__date__ = "2021-11-08"
__email__ = "[email protected]"
__title__ = "Screenshoter"
__version__ = "1.1"
# ----------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------
def init():
# Add application-specific configuration options
configmanager.parser.add_argument(
"-v",
"--version",
help="print the {} version number and exit".format(__title__),
action="version",
version="{} {}".format(__title__, __version__),
)
configmanager.parser.add(
"-i",
metavar="<path>",
help="path to input file",
dest="input",
type=configmanager.readable_file,
required=False,
default=constants.SCREENSHOTER_INPUT_FILE,
)
configmanager.parser.add(
"-sw",
metavar="<int>",
help="width of screenshots",
dest="width",
type=int,
required=False,
default=constants.IMAGE_WIDTH_DESKTOP,
)
configmanager.parser.add(
"-sh",
metavar="<int>",
help="height of screenshots (minimum height with full page screenshots)",
dest="height",
type=int,
required=False,
default=constants.IMAGE_HEIGHT_DESKTOP,
)
configmanager.parser.add(
"-f",
help="whether to take full page screenshots",
dest="full_page",
required=False,
action="store_true",
default=False,
)
configmanager.parser.add(
"-o",
metavar="<path>",
help="path to output directory",
dest="output",
type=configmanager.writable_dir,
required=False,
default=constants.SCREENSHOTER_OUTPUT_DIR,
)
configmanager.options = configmanager.parser.parse_known_args()[
0
] # Get known options, i.e., Namespace from the tuple
# Configure logger
utils.configure_loguru_logger()
# Show title
utils.show_header(__title__, __version__)
# Show configurations
utils.show_configurations()
def main():
# Initialize the application
init()
try:
# Take a screenshot of URLs
logger.info(
"Take a screenshot of URLs read from '{}'.".format(
configmanager.options.input
)
)
screenshot: Screenshot = Screenshot(
input_file=Path(configmanager.options.input),
width=configmanager.options.width,
height=configmanager.options.height,
full_page=configmanager.options.full_page,
output_dir=Path(configmanager.options.output),
)
screenshot.take()
if len(screenshot.input_urls) > 0:
logger.info(
"{} out of {} screenshots were successfully taken and stored at '{}'.".format(
screenshot.success_counter,
len(screenshot.input_urls),
screenshot.output_dir,
)
)
else:
logger.info("No screenshots to be taken")
except Exception as err:
logger.error(err)
raise
# ----------------------------------------------------------------------------
# Application
# ----------------------------------------------------------------------------
if __name__ == "__main__":
main()