-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinypng.py
128 lines (99 loc) · 3.58 KB
/
tinypng.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
# coding : utf-8
from os.path import dirname
from urllib2 import Request, urlopen
from base64 import b64encode
import json
import threading
import os
import time
key = ""
outputdir = ""
inputdir = ""
filecount = 0
isize = 0
osize = 0
class shrink_thread(threading.Thread):
def __init__(self, path, filename):
threading.Thread.__init__(self)
self.filename = filename
self.path = path
def run(self):
tmppath = os.path.join(outputdir, self.path)
if not os.path.exists(tmppath):
try:
os.makedirs(tmppath)
except Exception as e:
pass
#inputfile = self.path + os.sep + self.filename
inputfile = os.path.join(self.path, self.filename)
#outputfile = tmppath + os.sep + self.filename
outputfile = os.path.join(tmppath, self.filename)
global isize
global osize
global filecount
if not os.path.exists(outputfile):
print "The input file : " + inputfile
request = Request("https://api.tinypng.com/shrink", open(inputfile, "rb").read())
# add the auth
auth = b64encode(bytes("api:" + key)).decode("ascii")
request.add_header("Authorization", "Basic %s" % auth)
# http post request
response = urlopen(request)
if response.getcode() == 201:
# get the json result
rejson = json.loads(response.read())
# get the url
result = urlopen(rejson["output"]["url"]).read()
print "The output file : " + outputfile
open(outputfile, "wb").write(result)
isize += rejson["input"]["size"]
osize += rejson["output"]["size"]
else:
# Something went wrong! You can parse the JSON body for details.
print "Error Code : " + response.getcode()
print "Compression failed"
else:
pass
#print "_______________Exists File______________ " + inputfile
# end the thread
#self.thread_stop = True
#filecount = filecount - 1
self.stop()
#if filecount <= 0:
#print "====================================="
#print "input size : " + str(isize)
#print "output size : " + str(osize)
#print "rate : " + str(1.0 * osize / isize)
def stop(self):
global filecount
self.thread_stop = True
filecount = filecount - 1
def shrink_png_by_path(path):
global filecount
if os.path.isdir(path):
#print "current path : " + path
for curfile in os.listdir(path):
#nextpath = path + os.sep + curfile
nextpath = os.path.join(path, curfile)
if os.path.isdir(nextpath):
shrink_png_by_path(nextpath)
elif curfile.lower().endswith(".png") or curfile.lower().endswith(".jpg"):
filecount = filecount + 1
#print "the input file : " + nextpath
shrink_thread(path, curfile).start()
#time.sleep(0.1)
else:
print "The path is invalid"
def load_config():
import ConfigParser
global key
global outputdir
global inputdir
config = ConfigParser.RawConfigParser()
config.read("etiny.cfg")
key = config.get("etiny", "key")
inputdir = config.get("etiny", "inputdir")
outputdir = "__" + config.get("etiny", "outputdir") + "__" + inputdir
if __name__ == "__main__":
load_config();
shrink_png_by_path(inputdir)