-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_process.py
executable file
·45 lines (39 loc) · 1.52 KB
/
local_process.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --------------------------------------
# Created by Konfido on 2020-08-30
# --------------------------------------
import datetime
import json
import os
import shutil
import subprocess as sp
import sys
my_env = os.environ.copy()
my_env["PATH"] = "/usr/local/bin:" + my_env["PATH"]
def rename(src):
file_dir = os.path.dirname(arg)
file_id = datetime.datetime.now().strftime(format="%Y%m%d%H%M%S%f")[:-3]
file_type = (os.path.splitext(arg)[-1]).lower()
file_path = os.path.join(file_dir, file_id+file_type)
os.rename(arg, file_path)
return file_type, file_path
for arg in sys.argv[1:]:
file_type, file_path = rename(arg)
try:
if file_type == ".png":
my_command = ["pngquant", file_path, "--quality",
"70-95", "--ext=.png", "--force"]
sp.check_output(my_command, env=my_env)
elif file_type == ".jpg" or file_type == ".jpeg":
my_command = ["jpegoptim", "--max=90", file_path]
sp.check_output(my_command, env=my_env)
elif file_type == ".gif":
my_command = ["gifsicle", "-i", file_path, "--optimize=3", "-o", file_path]
sp.check_output(my_command, env=my_env)
except Exception as e:
print(e)
# sys.stdout.write("All image compressed!")
os.system("""osascript -e 'tell application id "com.runningwithcrayons.Alfred" \
to run trigger "notify" in workflow "com.konfido.images-utils" \
with argument "All images compressed!"'""")