-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
83 lines (70 loc) · 1.85 KB
/
main.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
# pylint: disable=missing-module-docstring
import sys
import argparse
from argparse import Namespace
import logging
from rich.traceback import install
from rich.console import Console
from src.api import PexelsAPI
from src.banner import cli_banner
from src.args import PayloadCheck
console = Console()
install()
def cli_args() -> Namespace:
"""Parse command line arguments"""
parser = argparse.ArgumentParser(
description="A python command-line tool for scraping images from Pexels.com by topic search.",
epilog="Repository: https://github.com/ToshY/pexels-scraper",
)
parser.add_argument(
"-k",
"--key",
help="API key.",
dest="api_key",
type=str,
required=True,
)
parser.add_argument(
"-p",
"--payload",
help="Payload in JSON format.",
dest="payload",
action=PayloadCheck,
type=str,
required=True,
)
parser.add_argument(
"-d",
"--debug",
help="Debug loglevel; Fallback to WARNING.",
dest="loglevel",
action="store_const",
const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
"-v",
"--verbose",
help="Info loglevel.",
dest="loglevel",
action="store_const",
const=logging.INFO,
)
user_args = parser.parse_args()
logging.basicConfig(
format="%(levelname)s: %(message)s",
level=user_args.loglevel,
)
return user_args
def main():
"""Get Pexels images"""
args = cli_args()
pexels_api = PexelsAPI(args.api_key)
pexels_api.search_for_photos(args.payload)
if __name__ == "__main__":
cli_banner("Pexels Scraper")
try:
main()
except KeyboardInterrupt:
console.print("\r\n[red]Execution cancelled by user[/red]")
sys.exit()