cmd/watchforlinks.py

58 lines
1.3 KiB
Python
Raw Normal View History

import argparse
2020-05-15 04:40:50 +00:00
import pyperclip
import re
2020-05-15 04:40:50 +00:00
import sys
import time
from voussoirkit import passwordy
from voussoirkit import pathclass
2020-05-15 04:40:50 +00:00
def loop_once(extension, regex=None):
2020-05-15 04:40:50 +00:00
try:
text = pyperclip.paste()
except Exception:
return
text = text.strip()
if len(text.split(sep=None, maxsplit=1)) > 1:
2020-05-15 04:40:50 +00:00
return
if 'http://' not in text and 'https://' not in text:
return
if regex and not re.search(regex, text):
return
path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension)
pyperclip.copy('')
print(path.basename, text)
h = path.open('w', encoding='utf-8')
h.write(text)
h.close()
2020-05-15 04:40:50 +00:00
def loop_forever(extension, regex):
pyperclip.copy('')
2020-05-15 04:40:50 +00:00
while True:
loop_once(extension=extension, regex=regex)
2020-05-15 04:40:50 +00:00
time.sleep(1)
def watchforlinks_argparse(args):
2020-05-15 04:40:50 +00:00
try:
loop_forever(extension=args.extension, regex=args.regex)
2020-05-15 04:40:50 +00:00
except KeyboardInterrupt:
pass
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('extension', nargs='?', default='generic')
parser.add_argument('--regex', dest='regex', default=None)
parser.set_defaults(func=watchforlinks_argparse)
args = parser.parse_args(argv)
return args.func(args)
2020-05-15 04:40:50 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))