Created
March 18, 2026 08:27
-
-
Save FleXoft/c3d4196939f33d0c8bcb8b699d92fc51 to your computer and use it in GitHub Desktop.
My Python3 gallery generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import subprocess | |
| import time | |
| from pathlib import Path | |
| def get_gallery_data(txt_path): | |
| """Beolvassa a gallery.txt fájlt és szótárat épít belőle.""" | |
| descriptions = {} | |
| if txt_path.exists(): | |
| with open(txt_path, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| if '|' in line: | |
| parts = line.strip().split('|', 1) | |
| if len(parts) == 2: | |
| fname, desc = parts | |
| descriptions[fname] = desc | |
| return descriptions | |
| def copy_to_clipboard(text): | |
| """Kimásolja a szöveget a macOS vágólapjára.""" | |
| try: | |
| process = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE, text=True) | |
| process.communicate(input=text) | |
| print("\n✅ A HTML kód másolva a vágólapra!") | |
| except Exception as e: | |
| print(f"\n❌ Hiba a másolás során: {e}") | |
| def generate_html(directory, extension, hours): | |
| dir_path = Path(directory) | |
| if not dir_path.is_dir(): | |
| print(f"Hiba: A '{directory}' nem egy érvényes könyvtár.") | |
| return None | |
| now = time.time() | |
| seconds_limit = hours * 3600 | |
| descriptions = get_gallery_data(dir_path / "gallery.txt") | |
| gallery_items = [] | |
| # Csak azokat a fájlokat gyűjtjük össze, amik NEM _ORIGINAL-ra végződnek | |
| files = [ | |
| f for f in dir_path.glob(f"*") | |
| if f.suffix.lower() == f".{extension.lower()}" | |
| and not f.stem.endswith("_ORIGINAL") | |
| ] | |
| for file in files: | |
| file_mtime = file.stat().st_mtime | |
| # Időszűrés (0 esetén kikapcsolva) | |
| if (hours <= 0) or ((now - file_mtime) <= seconds_limit): | |
| alt_text = descriptions.get(file.name, "") | |
| # src: az alap kép (pl. kepek/kep1.jpg) | |
| img_src = f"{directory}/{file.name}" | |
| # data-full: a névhez hozzáadjuk az _ORIGINAL-t (pl. kepek/kep1_ORIGINAL.jpg) | |
| img_full = f"{directory}/{file.stem}_ORIGINAL{file.suffix}" | |
| item = ( | |
| f' <div class="gallery-item">' | |
| f'<img src="{img_src}" ' | |
| f'data-full="{img_full}" ' | |
| f'alt="{alt_text}" loading="lazy"></div>' | |
| ) | |
| gallery_items.append(item) | |
| if not gallery_items: | |
| print("Nem található a feltételeknek megfelelő fájl (vagy mind _ORIGINAL).") | |
| return None | |
| html_output = [ | |
| "<section>", | |
| "<h2>Építészet & Design</h2>", | |
| "<p>Szöveg</p>", | |
| '<div class="justified-gallery">', | |
| "\n".join(gallery_items), | |
| "</div>", | |
| "</section>" | |
| ] | |
| return "\n".join(html_output) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="HTML galéria generáló (tisztított listával).") | |
| parser.add_argument("dir", help="A forráskönyvtár útvonala") | |
| parser.add_argument("ext", help="A fájlok kiterjesztése (pl. jpg)") | |
| parser.add_argument("--hours", type=float, default=1.0, | |
| help="Hány óránál nem régebbi fájlok? (0 = összes, alapértelmezett: 1)") | |
| args = parser.parse_args() | |
| clean_dir = args.dir.rstrip('/') | |
| result = generate_html(clean_dir, args.ext, args.hours) | |
| if result: | |
| print("--- GENERÁLT HTML ---") | |
| print(result) | |
| copy_to_clipboard(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment