View Single Post
Old 05-11-2023, 08:15 AM   #1101
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,495
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
The 'paste' button will convert the URL to an id if there's a rule for it. (The tooltip only mentions isbn and url but that's a mistake.) This would let me copy the link of a goodreads or fantasticfiction page from browser and then paste it without opening the MDE.
Here is a python template that contains a copy of the base calibre code the the MDE uses. It returns the ID:VAL pair for a URL in the clipboard, if possible. You should be able to use it in a Copy to Clipboard action.
Code:
python:


def parse_clipboard_for_identifier():
    from calibre.ebooks.metadata.sources.prefs import msprefs
    from calibre.utils.formatter import EvalFormatter
	from qt.core import QApplication
	import re
    text = str(QApplication.clipboard().text()).strip()
    if not text:
        return None

    rules = msprefs['id_link_rules']
    if rules:
        formatter = EvalFormatter()
        vals = {'id' : '__ID_REGEX_PLACEHOLDER__'}
        for key in rules.keys():
            rule = rules[key]
            for name, template in rule:
                try:
                    url_pattern = formatter.safe_format(template, vals, '', vals)
                    url_pattern = re.escape(url_pattern).replace('__ID_REGEX_PLACEHOLDER__', '(?P<new_id>.+)')
                    if url_pattern.startswith('http:') or url_pattern.startswith('https:'):
                        url_pattern = '(?:http|https):' + url_pattern.partition(':')[2]
                    new_id = re.compile(url_pattern)
                    new_id = new_id.search(text).group('new_id')
                    if new_id:
                        vals = {}
                        vals[key] = new_id
                        return vals
                except Exception:
                    import traceback
                    traceback.print_exc()
                    continue

    from calibre.customize.ui import all_metadata_plugins

    for plugin in all_metadata_plugins():
        try:
            identifier = plugin.id_from_url(text)
            if identifier:
                vals = {}
                vals[identifier[0]] = identifier[1]
                return vals
        except Exception:
            pass

    return None

def evaluate(book, context):
	x = parse_clipboard_for_identifier()
	if x:
		return ', '.join(k + ':' + v for k,v in x.items())
	return ''
chaley is offline   Reply With Quote