source: plugins/livestream/js/build/encode-images.py @ 1616

Revision 1616, 1.1 KB checked in by jeroen, 2 years ago (diff)

updated livestream plugin to be an actual 5.5 JS plugin

Line 
1import sys
2if len(sys.argv) < 3 or len(sys.argv) > 3:
3    print """
4        This script takes exactly two arguments:
5        the original file and the image path.
6        """
7else:
8    import base64, re
9
10    org = sys.argv[1]
11    org_file = open(org, 'r')
12    css = org_file.read()
13    css_base = sys.argv[2]
14    extensions = {
15        'png': 'image/png',
16        'gif': 'image/gif',
17    }
18    for ext in extensions:
19        regex = re.compile('url\((\.*\/*)([a-zA-Z0-9-_/]+)\.%s\)' % ext)
20        matches = regex.finditer(css)
21        if matches:
22            for m in matches:
23                img_name = m.group(1) + m.group(2) + '.' + ext
24                img_file = css_base + '/' + img_name
25                try:
26                    img_text = base64.b64encode(open(img_file,'rb').read())
27                    css = css.replace(img_name, 'data:%s;base64,%s' % (extensions[ext], img_text))
28                except IOError:
29                    print "%s does not exist" % img_file
30        else:
31            print "No %s images found." % ext
32
33    org_file.close()
34    dest_file = open(org, 'w')
35    dest_file.write(css)
36    dest_file.close()
Note: See TracBrowser for help on using the repository browser.