View Single Post
Old 08-29-2022, 07:27 PM   #19
lizzie1170
Member
lizzie1170 began at the beginning.
 
lizzie1170's Avatar
 
Posts: 12
Karma: 10
Join Date: Jul 2022
Device: none
Quote:
Originally Posted by capink View Post
I do not understand what you are trying to do with your code, and I do not have the time to debug it. If you can get a working function that returns whatever tags you want, I can help from there.
I am very sorry for taking your time, in my original code I was able to get my tags and added an additional label called PROCESSED but with the PRINT function. I don't really know how to do it with Calibre functions. This is the code I have now.

Code:
import re
import ast
import os

with open(r"D:\User\Calibre Portable\Python_tareas\docs_pys\test_dict.txt") as f:
    tags_dict = f.read()

def convert_to_text(path_to_epub):
    import os, subprocess
    from calibre.ptempfile import PersistentTemporaryDirectory
    tdir = PersistentTemporaryDirectory('_temp_convert')
    output_file = os.path.join(tdir, 'temp.txt')
    cmd = 'ebook-convert "{}" "{}"'.format(path_to_epub, output_file)
    subprocess.call(cmd, shell='true')
    return output_file

def tags_from_epub(path_to_epub):
    path_to_txt = convert_to_text(path_to_epub)
    temp = []
    res = dict()
    for line in path_to_txt:
        for key,value in tags_dict.items():
            if re.search(rf'{value}', line):
                if value not in temp:
                    temp.append(value)
                    res[key] = value
                    regex = re.compile(value)
                    match_array = regex.finditer(line)
                    match_list = list(match_array)
                    for m in match_list:
                        print(key)
    print("processed ")

def run(gui, settings, chain):
    db = gui.current_db
    for book_id in chain.scope().get_book_ids():
        fmts = [ fmt.strip() for fmt in db.formats(book_id, index_is_id=True).split(',') ]
        if 'EPUB' in fmts:
            path_to_epub = db.format_abspath(book_id, 'EPUB', index_is_id=True)
            tags_from_epub(path_to_epub)
Attached Thumbnails
Click image for larger version

Name:	code_res2.PNG
Views:	80
Size:	27.8 KB
ID:	196164  
lizzie1170 is offline   Reply With Quote