예제 #1
0
def get_shrink_file_info(in_filepath, api_key=None, out_filepath=None):
    with open(in_filepath, 'rb') as f:
        info = get_shrink_data_info(f.read(), api_key)

    if out_filepath is None:
        out_filepath = in_filepath
        prefix, ext = out_filepath.rsplit(".", 1)
        out_filepath = prefix + ".tiny." + ext

    info['output']['filepath'] = abspath(out_filepath)
    return info
예제 #2
0
def get_shrink_file_info(in_filepath, api_key=None, out_filepath=None):
    with open(in_filepath, 'rb') as f:
        info = get_shrink_data_info(f.read(), api_key)

    if out_filepath is None:
        out_filepath = in_filepath
        prefix, ext = out_filepath.rsplit(".", 1)
        out_filepath = prefix + ".tiny." + ext

    info['output']['filepath'] = abspath(out_filepath)
    return info
예제 #3
0
def read_keyfile(filepath):
    with open(filepath, 'r') as kf:
        return set([k.strip() for k in kf.readlines() if k.strip()])
예제 #4
0
def write_shrunk_file(info):
    out_filepath = info['output']['filepath']
    with open(out_filepath, 'wb') as f:
        f.write(get_shrunk_data(info))
예제 #5
0
def read(fname):
    with open(join(dirname(__file__), fname), 'r') as f:
        return f.read()
예제 #6
0
def read_keyfile(filepath):
    with open(filepath, 'r') as kf:
        return set([k.strip() for k in kf.readlines() if k.strip()])
예제 #7
0
def write_shrunk_file(info):
    out_filepath = info['output']['filepath']
    with open(out_filepath, 'wb') as f:
        f.write(get_shrunk_data(info))
예제 #8
0
def main(argv=sys.argv[1:]):
    def pout(s):
        if args['--quiet']:
            return
        sys.stdout.write(s)
        sys.stdout.flush()

    version = "TinyPNG API " + __version__
    args = docopt(__doc__, argv=argv, version=version)
    keys = find_keys(args)

    if not keys:
        print("tinypng: No API key found\n"
              "  You can obtain a key from https://tinypng.com/developers")
        key = input("  Enter API key: ").strip()
        if not key:
            return 1

        keypath = os.path.join(os.path.expanduser("~"), ".tinypng.keys")

        with open(keypath, "w") as f:
            f.write(key + "\n")

        pout("tinypng: Created keyfile at {0}".format(keypath))

        keys = set([key])

    for fpath in args['<png_files>']:
        outpath = fpath if args['--replace'] else None
        cur_keys = keys.copy()
        for key in cur_keys:
            try:
                pout("tinypng: Shrinking '{0}' .. ".format(fpath))
                info = get_shrink_file_info(fpath,
                                            api_key=key,
                                            out_filepath=outpath)
                pout("ok.\n")
                out_info = info['output']
                in_info = info['input']
                pout("tinypng: Saved {0:02d}% ({1} byte -> {2} byte)\n".format(
                    int(100 - (out_info['ratio'] * 100)),
                    in_info['size'],
                    out_info['size'],
                ))
                pout("tinypng: {0} compressions this month\n".format(
                    info['compression_count'], ))

                if out_info['ratio'] < 1:
                    pout("tinypng: Writing to '{0}' .. ".format(
                        out_info['filepath']))
                    write_shrunk_file(info)
                    pout("done.\n")

                break
            except TinyPNGException as e:
                pout("fail!\n")
                pout("\ntinypng: {0}\t\t{1}\n".format(e.error, e.message))

                if e.error in MISC_ERRORS:
                    break  # try other files which may be valid

                if e.error in API_KEY_ERRORS:
                    pout("tinypng: Invalid API key \"{0}\"\n".format(key))

                if e.error in RATE_LIMIT_ERRORS:
                    msg_fmt = "tinypng: API Limit exceeded for \"{0}\"\n"
                    pout(msg_fmt.format(key))

                if e.error in API_KEY_ERRORS + RATE_LIMIT_ERRORS:
                    keys.remove(key)
                    continue  # try again if other keys are available

            return 1
    return 0
예제 #9
0
def main(argv=sys.argv[1:]):
    def pout(s):
        if args['--quiet']:
            return
        sys.stdout.write(s)
        sys.stdout.flush()

    version = "TinyPNG API " + __version__
    args = docopt(__doc__, argv=argv, version=version)
    keys = find_keys(args)

    if not keys:
        print(
            "tinypng: No API key found\n"
            "  You can obtain a key from https://tinypng.com/developers"
        )
        key = input("  Enter API key: ").strip()
        if not key:
            return 1

        keypath = os.path.join(os.path.expanduser("~"), ".tinypng.keys")

        with open(keypath, "w") as f:
            f.write(key +  "\n")

        pout("tinypng: Created keyfile at {0}".format(keypath))

        keys = set([key])

    for fpath in args['<png_files>']:
        outpath = fpath if args['--replace'] else None
        cur_keys = keys.copy()
        for key in cur_keys:
            try:
                pout("tinypng: Shrinking '{0}' .. ".format(fpath))
                info = get_shrink_file_info(
                    fpath, api_key=key, out_filepath=outpath
                )
                pout("ok.\n")
                out_info = info['output']
                in_info = info['input']
                pout("tinypng: Saved {0:02d}% ({1} byte -> {2} byte)\n".format(
                    int(100 - (out_info['ratio'] * 100)),
                    in_info['size'], out_info['size'],
                ))
                pout("tinypng: {0} compressions this month\n".format(
                    info['compression_count'],
                ))

                if out_info['ratio'] < 1:
                    pout("tinypng: Writing to '{0}' .. ".format(
                        out_info['filepath']
                    ))
                    write_shrunk_file(info)
                    pout("done.\n")

                break
            except TinyPNGException as e:
                pout("fail!\n")
                pout("\ntinypng: {0}\t\t{1}\n".format(e.error, e.message))

                if e.error in MISC_ERRORS:
                    break # try other files which may be valid

                if e.error in API_KEY_ERRORS:
                    pout("tinypng: Invalid API key \"{0}\"\n".format(key))

                if e.error in RATE_LIMIT_ERRORS:
                    msg_fmt = "tinypng: API Limit exceeded for \"{0}\"\n"
                    pout(msg_fmt.format(key))

                if e.error in API_KEY_ERRORS + RATE_LIMIT_ERRORS:
                    keys.remove(key)
                    continue # try again if other keys are available

            return 1
    return 0