# Input DOI / URL

import re
import sys

# Pyperclip is not built-in, check and download if needed
try:
    import pyperclip
except (ImportError, ModuleNotFoundError):
    print('Pyperclip module not found. Please download it.')
    sys.exit(0)

# Regex for links
link_regex = re.compile(r'''(
    http[s]?://
    (?:[a-zA-Z]|
    [0-9]|
    [$-_@.&+]|
    [!*\(\),]|
    (?:%[0-9a-fA-F][0-9a-fA-F]))+
    )''', re.IGNORECASE | re.VERBOSE)

# Get DOI / URL using different methods

# Method 1: argument
try:
    input_link = sys.argv[1]
# Method 2: clipboard
except IndexError:
    input_link = pyperclip.paste()

# Method 3: manual input
def regex_check(regex, link):
    """
    Check using regex. If DOI/URL are not in the right format,
    require manual input until correct or Enter to quit.
    """
    while True:
        match = re.match(regex, link)
        if match == None:
            link = str(input('''Enter valid DOI / URL or press Enter to quit: > '''))
            if link == '':
                exit()
            else:
                continue
        else:
            return link

url = regex_check(link_regex, input_link)
[((14, 13, 21, 37), 're.compile', 're.compile', ({(14, 24, 21, 8): '"""(\n    http[s]?://\n    (?:[a-zA-Z]|\n    [0-9]|\n    [$-_@.&+]|\n    [!*\\\\(\\\\),]|\n    (?:%[0-9a-fA-F][0-9a-fA-F]))+\n    )"""', (21, 10, 21, 36): 're.IGNORECASE | re.VERBOSE'}, {}), '(\n    """(\n    http[s]?://\n    (?:[a-zA-Z]|\n    [0-9]|\n    [$-_@.&+]|\n    [!*\\\\(\\\\),]|\n    (?:%[0-9a-fA-F][0-9a-fA-F]))+\n    )"""\n    , re.IGNORECASE | re.VERBOSE)', False, 'import re\n'), ((11, 4, 11, 15), 'sys.exit', 'sys.exit', ({(11, 13, 11, 14): '(0)'}, {}), '(0)', False, 'import sys\n'), ((30, 17, 30, 34), 'pyperclip.paste', 'pyperclip.paste', ({}, {}), '()', False, 'import pyperclip\n'), ((39, 16, 39, 37), 're.match', 're.match', ({(39, 25, 39, 30): 'regex', (39, 32, 39, 36): 'link'}, {}), '(regex, link)', False, 'import re\n')]