File: href.py 1 #!/usr/bin/python3 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the “Software”), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 from re import compile, IGNORECASE, Pattern 27 from sys import argv, exit, stderr, stdin, stdout 28 29 30 info = ''' 31 href [options...] [filepaths/URIs...] 32 33 This script finds all (hyper)links inside `href` attributes in the input(s) 34 given, showing each match on its own output line. It can match multiple links 35 on each input line. 36 ''' 37 38 # no args or a leading help-option arg means show the help message and quit 39 if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'): 40 print(info.strip()) 41 exit(0) 42 43 44 def seems_url(s: str) -> bool: 45 protocols = ('https://', 'http://', 'file://', 'ftp://', 'data:') 46 return any(s.startswith(p) for p in protocols) 47 48 49 def handle_lines(w, src, links: Pattern) -> None: 50 extra = len('href="') 51 for line in src: 52 for match in links.finditer(line): 53 start = match.start() + extra 54 end = match.end() - 1 55 w.write(line[start:end]) 56 w.write('\n') 57 w.flush() 58 59 60 if any(seems_url(e) for e in argv): 61 from io import TextIOWrapper 62 from urllib.request import urlopen 63 64 links = compile( 65 'href="[^"]+"', 66 flags=IGNORECASE, 67 ) 68 69 try: 70 if argv.count('-') > 1: 71 msg = 'reading from `-` (standard input) more than once not allowed' 72 raise ValueError(msg) 73 74 # handle all named inputs given 75 for path in argv[1:]: 76 if path == '-': 77 handle_lines(stdout, stdin, links) 78 continue 79 80 if seems_url(path): 81 with urlopen(path) as inp: 82 with TextIOWrapper(inp, encoding='utf-8') as txt: 83 handle_lines(stdout, txt, links) 84 continue 85 86 with open(path, encoding='utf-8') as inp: 87 handle_lines(stdout, inp, links) 88 89 if len(argv) == 1: 90 handle_lines(stdout, stdin, links) 91 except BrokenPipeError: 92 # quit quietly, instead of showing a confusing error message 93 stderr.close() 94 exit(0) 95 except KeyboardInterrupt: 96 stderr.close() 97 exit(2) 98 except Exception as e: 99 print(f'\x1b[31m{e}\x1b[0m', file=stderr) 100 exit(1)