├── .gitignore ├── add-space-between-latin-and-cjk ├── backup-icloud-photos ├── btd.sh ├── check-domain.sh ├── decrypt.sh ├── ec ├── encrypt.sh ├── macos-proxy-env ├── noproxy ├── print_macos_proxy ├── restic-backup ├── resticw └── test-port /.gitignore: -------------------------------------------------------------------------------- 1 | .mypy_cache 2 | -------------------------------------------------------------------------------- /add-space-between-latin-and-cjk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Add space between CJK and Latin characters according to 4 | # https://open.leancloud.cn/copywriting-style-guide.html. Mainly used for 5 | # cleanning up Markdown documents. 6 | # 7 | # SYNOPSIS 8 | # add-space-between-latin-and-cjk input_file [out_file] 9 | # 10 | # CAUTION 11 | # If output_file is not provided, input_file is changed in-place. 12 | 13 | import sys 14 | import functools 15 | 16 | def is_latin(c): 17 | return ord(c) < 256 18 | 19 | # Some characters should not have space on either side. 20 | def allow_space(c): 21 | return not c.isspace() and not (c in '。?!,、;:「」《》『』<>[]()()*_-=#".@\'') 22 | 23 | def add_space_at_boundry(prefix, next_char): 24 | if len(prefix) == 0: 25 | return next_char 26 | if is_latin(prefix[-1]) != is_latin(next_char) and \ 27 | allow_space(next_char) and allow_space(prefix[-1]): 28 | return prefix + ' ' + next_char 29 | else: 30 | return prefix + next_char 31 | 32 | if len(sys.argv) < 2: 33 | print('A minimum of one argument is required!\n') 34 | exit() 35 | 36 | infile = open(sys.argv[1], 'r') 37 | instr = infile.read() 38 | infile.close() 39 | 40 | outstr = functools.reduce(add_space_at_boundry, instr, '') 41 | with open(sys.argv[2 if len(sys.argv) > 2 else 1], 'w') as outfile: 42 | outfile.write(outstr) 43 | -------------------------------------------------------------------------------- /backup-icloud-photos: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | restic.sh backup --cleanup-cache --tag keep "$HOME/Pictures/*.photoslibrary" 6 | -------------------------------------------------------------------------------- /btd.sh: -------------------------------------------------------------------------------- 1 | all_proxy=http://127.0.0.1:6152 aria2c "$@" 2 | -------------------------------------------------------------------------------- /check-domain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Adopted from http://linuxconfig.org/check-domain-name-availability-with-bash-and-whois 4 | # Feel free to fork and improve. 5 | 6 | if [ "$#" == "0" ]; then 7 | echo "You need tu supply at least one argument!" 8 | exit 1 9 | fi 10 | 11 | DOMAINS=( '.com' '.co.uk' '.net' '.info' '.mobi' \ 12 | '.org' '.tel' '.biz' '.tv' '.cc' '.eu' '.ru' \ 13 | '.in' '.it' '.sk' '.com.au' '.io' '.xyz' ) 14 | 15 | ELEMENTS=${#DOMAINS[@]} 16 | 17 | while (( "$#" )); do 18 | 19 | for (( i=0;i<$ELEMENTS;i++)); do 20 | whois $1${DOMAINS[${i}]} | egrep -q \ 21 | '^No match|^NOT FOUND|^Not fo|AVAILABLE|available for purchase|^No Data Fou|has not been regi|No entri|^DOMAIN NOT FOUND' 22 | if [ $? -eq 0 ]; then 23 | echo "$1${DOMAINS[${i}]} : available" 24 | fi 25 | done 26 | 27 | shift 28 | 29 | done -------------------------------------------------------------------------------- /decrypt.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | base64 -d | gpg -d -q 4 | -------------------------------------------------------------------------------- /ec: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -n "$SUDO_COMMAND" ]; then 4 | vim "$@" 5 | fi 6 | 7 | if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then 8 | emacsclient -a "" -t "$@" 9 | else 10 | emacsclient -a "" -c "$@" 11 | fi 12 | -------------------------------------------------------------------------------- /encrypt.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | gpg -c -q -o - | base64 4 | -------------------------------------------------------------------------------- /macos-proxy-env: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import os 4 | import subprocess 5 | 6 | proxy_types = ['http', 'https', 'socks'] 7 | proxy_vars = {'http': 'http_proxy', 8 | 'https': 'https_proxy', 9 | 'socks': 'all_proxy'} 10 | proxy_protocols = {'http': 'http', 11 | 'https': 'http', 12 | 'socks': 'socks5'} 13 | 14 | result = subprocess.run(['scutil', '--proxy'], stdout=subprocess.PIPE) 15 | lines = result.stdout.decode('utf-8').splitlines() 16 | 17 | for proxy_type in proxy_types: 18 | addrkey = proxy_type.upper() + 'Proxy' 19 | portkey = proxy_type.upper() + 'Port' 20 | 21 | addr = '' 22 | port = '' 23 | 24 | for line in lines: 25 | if addrkey in line: 26 | addr = line.split()[-1] 27 | if portkey in line: 28 | port = line.split()[-1] 29 | 30 | if addr != '' and port != '': 31 | shell = os.environ['SHELL'] 32 | if shell.endswith('fish'): 33 | print('set -xg %s "%s://%s:%s"' % (proxy_vars[proxy_type], 34 | proxy_protocols[proxy_type], 35 | addr, 36 | port)) 37 | else: 38 | print('export %s="%s://%s:%s"' % (proxy_vars[proxy_type], 39 | proxy_protocols[proxy_type], 40 | addr, 41 | port)) 42 | -------------------------------------------------------------------------------- /noproxy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | old_http_proxy=$http_proxy 6 | old_https_proxy=$https_proxy 7 | old_all_proxy=$all_proxy 8 | 9 | export http_proxy= 10 | export https_proxy= 11 | export all_proxy= 12 | 13 | $@ 14 | 15 | export http_proxy=$old_http_proxy 16 | export https_proxy=$old_https_proxy 17 | export all_proxy=$old_all_proxy 18 | -------------------------------------------------------------------------------- /print_macos_proxy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import argparse 4 | import subprocess 5 | 6 | parser = argparse.ArgumentParser(description='Print macOS proxy settings.') 7 | parser.add_argument('type', default='http', nargs='?', 8 | choices=['http', 'https', 'socks'], help='proxy type') 9 | 10 | args = parser.parse_args() 11 | 12 | result = subprocess.run(['scutil', '--proxy'], stdout=subprocess.PIPE) 13 | lines = result.stdout.decode('utf-8').splitlines() 14 | 15 | addrkey = args.type.upper() + 'Proxy' 16 | portkey = args.type.upper() + 'Port' 17 | 18 | addr = '' 19 | port = '' 20 | 21 | for line in lines: 22 | if addrkey in line: 23 | addr = line.split()[-1] 24 | if portkey in line: 25 | port = line.split()[-1] 26 | 27 | if addr != '' and port != '': 28 | print(addr + ':' + port) 29 | -------------------------------------------------------------------------------- /restic-backup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | PATH=/opt/homebrew/bin:$PATH 5 | 6 | source $HOME/.config/restic/env.sh 7 | 8 | FILE_LIST=$HOME/.config/restic/files.txt 9 | 10 | if [[ -f $FILE_LIST ]]; then 11 | restic backup --quiet --cleanup-cache \ 12 | --exclude-file=$HOME/.config/restic/exclude.txt \ 13 | --files-from=$FILE_LIST > /dev/null 14 | else 15 | restic backup --quiet --cleanup-cache \ 16 | --exclude-file=$HOME/.config/restic/exclude.txt $HOME > /dev/null 17 | fi 18 | restic forget --quiet --keep-last 3 --keep-hourly 72 --keep-daily 30 \ 19 | --keep-weekly 25 --keep-monthly 36 --keep-yearly 20 --host "$(hostname)" \ 20 | --keep-tag keep --cleanup-cache --prune > /dev/null 21 | -------------------------------------------------------------------------------- /resticw: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | source $HOME/.config/restic/env.sh 5 | 6 | restic $@ 7 | -------------------------------------------------------------------------------- /test-port: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import socket 4 | import sys 5 | 6 | host = sys.argv[1] 7 | port = int(sys.argv[2]) 8 | 9 | s = socket.socket( 10 | socket.AF_INET, socket.SOCK_STREAM) 11 | s.settimeout(0.2) 12 | 13 | r = 1 14 | 15 | try: 16 | r = s.connect((host, port)) 17 | except OSError: 18 | r = 1 19 | finally: 20 | s.close() 21 | 22 | sys.exit(r) 23 | --------------------------------------------------------------------------------