diff --git a/make_post_sell/lib/torrent.py b/make_post_sell/lib/torrent.py index 5d47c56..70c4573 100644 --- a/make_post_sell/lib/torrent.py +++ b/make_post_sell/lib/torrent.py @@ -156,6 +156,7 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description, os.makedirs(bundle_dir) webseeds = [] + downloaded = set() # filenames that actually landed on disk # 1. Download each file from S3 into the bundle directory. for f in files: @@ -163,6 +164,7 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description, log.info("torrent: downloading s3://%s/%s → %s", bucket, f["s3_key"], f["filename"]) try: s3_client.download_file(bucket, f["s3_key"], local_path) + downloaded.add(f["filename"]) except Exception: log.warning("torrent: skipping missing file s3://%s/%s", bucket, f["s3_key"]) continue @@ -170,25 +172,65 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description, webseeds.append(f["webseed_url"]) # 2. Write index.md + index.html into the bundle directory. + # Separate the main file (content/preview) from thumbnails using the + # filename prefix — build_bundle_files names them content.*, preview.*, + # and thumbnail1..4.*. + main_file = next( + (f for f in files + if f["filename"].startswith(("content.", "preview.")) + and f["filename"] in downloaded), + None, + ) + thumb_files = [ + f for f in files + if f["filename"].startswith("thumbnail") + and f["filename"] in downloaded + ] + if description: with open(os.path.join(bundle_dir, "index.md"), "w", encoding="utf-8") as fh: fh.write(description) - try: - import markdown - html_body = markdown.markdown(description, extensions=["fenced_code", "tables"]) - except ImportError: - # Fallback: wrap raw markdown in a
if markdown lib not installed. - html_body = f"{description}" - html = ( - "\n\n" - "" - f"{bundle_name} " - "\n\n" - + html_body - + "\n\n\n" + + try: + import markdown as _md + html_body = _md.markdown(description or "", extensions=["fenced_code", "tables"]) + except ImportError: + html_body = f"{description}" if description else "" + + # Thumbnail gallery — relative links, rendered inline. + thumb_html = "" + if thumb_files: + imgs = "".join( + f'' + f'' + for f in thumb_files ) - with open(os.path.join(bundle_dir, "index.html"), "w", encoding="utf-8") as fh: - fh.write(html) + thumb_html = f'
{imgs}\n' + + # Main file link — download anchor. + main_html = "" + if main_file: + label = "Download" if main_file["filename"].startswith("content.") else "Preview" + main_html = ( + f'' + f'{label}: {main_file["filename"]}
\n' + ) + + html = ( + "\n\n" + "" + f"{bundle_name} " + "" + "\n\n" + f"{bundle_name}
\n" + + thumb_html + + main_html + + html_body + + "\n\n\n" + ) + with open(os.path.join(bundle_dir, "index.html"), "w", encoding="utf-8") as fh: + fh.write(html) # 3. Build multi-file .torrent from the bundle directory. t = torf.Torrent(