-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_rarjpeg.py
59 lines (43 loc) · 1.62 KB
/
find_rarjpeg.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
import argparse
import logging
import pathlib
import sys
from rarjpeg import Rarjpeg
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S %d.%m.%Y')
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser(usage="%(prog)s TARGET [-e]",
description="Checks TARGET image or folder for rarjpeg(-s).")
parser.add_argument('target', help='name of the FILE or DIRECTORY to check')
parser.add_argument('-e', '--extract', action='store_true', default=False,
help='extract files from found rarjpegs')
def check(rarjpeg_path, extract=False):
rarjpeg = Rarjpeg(rarjpeg_path)
valid = rarjpeg.is_valid
logging.info(f"{rarjpeg.name} is {'valid' if valid else 'invalid'}")
if extract:
extracted, msg = rarjpeg.extract()
logging.info(
f"{rarjpeg.archive} {'was extracted' if extracted else 'was not extracted,'} {msg}\n")
def main():
if len(sys.argv) <= 1:
parser.print_help()
args = parser.parse_args()
if args.target:
path = pathlib.Path.cwd() / args.target.lstrip('/')
if not path.exists():
logger.error(f"Can not find '{path}', please check again!")
return
elif path.is_file():
check(path, args.extract)
elif path.is_dir():
for image in path.iterdir():
if image.suffix == '.jpg':
check(image, args.extract)
else:
logger.error('Something went wrong!\n')
parser.print_help()
if __name__ == '__main__':
main()