2021-11-09 08:34:32 +00:00
|
|
|
'''
|
|
|
|
watchforlinks
|
|
|
|
=============
|
|
|
|
|
|
|
|
This program will continuously watch your clipboard for URLs and save them to
|
|
|
|
individual files. The files will have randomly generated names. The current
|
|
|
|
contents of your clipboard will be erased.
|
|
|
|
|
|
|
|
> watchforlinks [extension] <flags>
|
|
|
|
|
|
|
|
extension:
|
|
|
|
The saved files will have this extension.
|
|
|
|
If not provided, the default is "generic".
|
|
|
|
|
|
|
|
flags:
|
|
|
|
--regex X:
|
|
|
|
A regex pattern. Only URLs that match this pattern will be saved.
|
|
|
|
'''
|
2020-08-20 17:57:32 +00:00
|
|
|
import argparse
|
2020-05-15 04:40:50 +00:00
|
|
|
import pyperclip
|
2020-10-07 06:08:10 +00:00
|
|
|
import re
|
2020-05-15 04:40:50 +00:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2021-11-09 08:34:32 +00:00
|
|
|
from voussoirkit import betterhelp
|
2020-05-15 04:40:50 +00:00
|
|
|
from voussoirkit import passwordy
|
2020-08-20 17:57:32 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-11-09 08:34:32 +00:00
|
|
|
from voussoirkit import pipeable
|
2020-05-15 04:40:50 +00:00
|
|
|
|
2020-10-07 06:08:10 +00:00
|
|
|
def loop_once(extension, regex=None):
|
2020-05-15 04:40:50 +00:00
|
|
|
try:
|
|
|
|
text = pyperclip.paste()
|
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
|
2020-08-20 17:57:32 +00:00
|
|
|
text = text.strip()
|
|
|
|
|
|
|
|
if len(text.split(sep=None, maxsplit=1)) > 1:
|
2020-05-15 04:40:50 +00:00
|
|
|
return
|
|
|
|
|
2020-10-07 06:08:10 +00:00
|
|
|
if 'http://' not in text and 'https://' not in text:
|
|
|
|
return
|
|
|
|
|
|
|
|
if regex and not re.search(regex, text):
|
|
|
|
return
|
|
|
|
|
2022-01-10 01:08:56 +00:00
|
|
|
path = pathclass.Path(passwordy.random_hex(12)).add_extension(extension)
|
2020-10-07 06:08:10 +00:00
|
|
|
pyperclip.copy('')
|
2021-12-27 03:18:15 +00:00
|
|
|
pipeable.stdout(f'{path.basename} {text}')
|
2021-10-05 00:21:14 +00:00
|
|
|
path.write('w', text, encoding='utf-8')
|
2020-05-15 04:40:50 +00:00
|
|
|
|
2020-10-07 06:08:10 +00:00
|
|
|
def loop_forever(extension, regex):
|
2020-12-15 21:09:03 +00:00
|
|
|
pyperclip.copy('')
|
2020-05-15 04:40:50 +00:00
|
|
|
while True:
|
2020-10-07 06:08:10 +00:00
|
|
|
loop_once(extension=extension, regex=regex)
|
2020-05-15 04:40:50 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
2020-08-20 17:57:32 +00:00
|
|
|
def watchforlinks_argparse(args):
|
2021-11-09 08:34:32 +00:00
|
|
|
pipeable.stderr('Watching clipboard... Press Ctrl+C to stop.')
|
2020-05-15 04:40:50 +00:00
|
|
|
try:
|
2020-10-07 06:08:10 +00:00
|
|
|
loop_forever(extension=args.extension, regex=args.regex)
|
2020-05-15 04:40:50 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2021-09-24 06:42:34 +00:00
|
|
|
return 0
|
2020-05-15 04:40:50 +00:00
|
|
|
|
2020-08-20 17:57:32 +00:00
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('extension', nargs='?', default='generic')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--regex', default=None)
|
2020-08-20 17:57:32 +00:00
|
|
|
parser.set_defaults(func=watchforlinks_argparse)
|
|
|
|
|
2021-11-09 08:34:32 +00:00
|
|
|
return betterhelp.single_main(argv, parser, __doc__)
|
2020-05-15 04:40:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|