예제 #1
0
def process(fname):
    cmd = 'file -i "{}"'.format(fname)
    res = get_simple_cmd_output(cmd).strip()
    charset = None
    if 'charset=' in res:
        charset = res.split('charset=')[1].lower()
    if not charset:
        print(
            "Error: cannot determine the character encoding of the input file.",
            file=sys.stderr)
        exit(2)
    if charset == 'utf-8':
        if not force_conversion:
            print("Warning: the input file is already in UTF-8 encoding.",
                  file=sys.stderr)
            print("Tip: use the -f option to force conversion.",
                  file=sys.stderr)
            # return
    # else
    p = Path(fname)
    ff = os.path.splitext(fname)[0]

    textToReplace = '.' + p.suffix
    textReplaceWith = '-converted.' + p.suffix
    fic = p.name.replace(p.suffix, '-converted' + p.suffix)

    output = str(p.parent) + '/' + fic

    #cmd = 'iconv --from-code={src} --to-code=utf-8 "{f}" -o "{o}"'.format(src = charset , f = fname, o=output)
    #os.system(cmd)
    return charset, output, fic
예제 #2
0
def ping(host, cnt=1):
    """Ping a URL and return the average ping time."""
    cmd = 'ping -c {cnt} {url}'.format(url=host, cnt=cnt)
    output = [x for x in process.get_simple_cmd_output(cmd).split('\n') if x]
    result = re.search('min/avg/max/mdev = (.*)/(.*)/(.*)/(.*) ms', output[-1])
    if result:
        return float('{0:.2f}'.format(float(result.group(1))))
    else:
        return None
예제 #3
0
def main():
    url = input("URL of the twitch video: ")
    cmd = 'youtube-dl -g "{}"'.format(url)
    print('#', cmd)
    url2 = get_simple_cmd_output(cmd).strip()
    print('#', url2)
    cmd = 'ffmpeg -i "{video}" -f mp3 {audio}.mp3'.format(video=url2,
                                                          audio=slugify(url))
    print('#', cmd)
    os.system(cmd)
예제 #4
0
def fping(host, cnt=1):
    """
    Get the avg ping time of a host (in msec).

    Instead of ping we use the command fping.
    """
    host = host.split(':')[0]
    cmd = "fping {host} -C {cnt} -q".format(host=host, cnt=cnt)
    res = [float(x) for x in process.get_simple_cmd_output(cmd).strip().split(':')[-1].split() if x != '-']
    if len(res) > 0:
        return sum(res) / len(res)
    else:
        return None
예제 #5
0
def process(fname):
    cmd = 'file -i "{}"'.format(fname)
    res = get_simple_cmd_output(cmd).strip()
    charset = None
    if 'charset=' in res:
        charset = res.split('charset=')[1].lower()
    if not charset:
        print(
            "Error: cannot determine the character encoding of the input file.",
            file=sys.stderr)
        exit(2)
    if charset == 'utf-8':
        if not force_conversion:
            print("Warning: the input file is already in UTF-8 encoding.",
                  file=sys.stderr)
            print("Tip: use the -f option to force conversion.",
                  file=sys.stderr)
            return
    # else
    cmd = 'iconv --from-code={src} --to-code=utf-8 "{f}"'.format(src=charset,
                                                                 f=fname)
    os.system(cmd)
예제 #6
0
def read_clipboard():
    """Read content of 'clipboard'."""
    cmd = 'xsel -bo'
    return process.get_simple_cmd_output(cmd)
예제 #7
0
def read_primary():
    """Read content of 'primary'."""
    cmd = 'xsel -po'
    return process.get_simple_cmd_output(cmd)
예제 #8
0
def is_firefox_running():
    output = get_simple_cmd_output('ps ux')
    return FIREFOX_PROCESS in output
예제 #9
0
def read_clipboard():
    """Read content of 'clipboard'."""
    cmd = 'xsel -bo'
    return process.get_simple_cmd_output(cmd)
예제 #10
0
def read_primary():
    """Read content of 'primary'."""
    cmd = 'xsel -po'
    return process.get_simple_cmd_output(cmd)
예제 #11
0
파일: ocr.py 프로젝트: raum01/Bash-Utils
def image_file_to_string(fname):
    """Convert an image file to text using OCR."""
    cmd = "{tesseract} {fname} stdout".format(tesseract=cfg.TESSERACT,
                                              fname=fname)
    return get_simple_cmd_output(cmd).rstrip('\n')