Add --regex option to only watch certain links.

This commit is contained in:
voussoir 2020-10-06 23:08:10 -07:00
parent 2412a6ee35
commit 723dcee420

View file

@ -1,12 +1,13 @@
import argparse import argparse
import pyperclip import pyperclip
import re
import sys import sys
import time import time
from voussoirkit import passwordy from voussoirkit import passwordy
from voussoirkit import pathclass from voussoirkit import pathclass
def loop_once(extension): def loop_once(extension, regex=None):
try: try:
text = pyperclip.paste() text = pyperclip.paste()
except Exception: except Exception:
@ -17,22 +18,27 @@ def loop_once(extension):
if len(text.split(sep=None, maxsplit=1)) > 1: if len(text.split(sep=None, maxsplit=1)) > 1:
return return
if 'http://' in text or 'https://' in text: if 'http://' not in text and 'https://' not in text:
path = pathclass.Path(passwordy.urandom_hex(12)).add_extension(extension) return
pyperclip.copy('')
print(path.basename, text)
h = path.open('w', encoding='utf-8')
h.write(text)
h.close()
def loop_forever(extension): 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()
def loop_forever(extension, regex):
while True: while True:
loop_once(extension=extension) loop_once(extension=extension, regex=regex)
time.sleep(1) time.sleep(1)
def watchforlinks_argparse(args): def watchforlinks_argparse(args):
try: try:
loop_forever(extension=args.extension) loop_forever(extension=args.extension, regex=args.regex)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
@ -40,6 +46,7 @@ def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('extension', nargs='?', default='generic') parser.add_argument('extension', nargs='?', default='generic')
parser.add_argument('--regex', dest='regex', default=None)
parser.set_defaults(func=watchforlinks_argparse) parser.set_defaults(func=watchforlinks_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)