diff --git a/README.md b/README.md index 32b95de..8fd3b08 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Swarmsync is a Python script that provides a set of tools for managing files on - Create and manage tags for uploaded files - Encrypt and pin files during upload - Monitor and send Prometheus statistics +- Generate HTML files from JSON containing download references ## Getting Started @@ -35,6 +36,24 @@ Swarmsync is a Python script that provides a set of tools for managing files on ### Usage +For interacting with Swarm network + +```bash +python3 +``` + +In case you have multiple files uploaded to Swarm, and you would like to show them together. For example: + +- a dapp, +- earth geospatial data, +- royalty free videos,
+ + this is a convenient way to handle. Upload the created HTML via swarmsync. + +```bash +python3 +``` + #### Show You can use the `show` command to display information about the files you have uploaded and their status. @@ -44,6 +63,7 @@ python swarmsync.py show [options] ``` Options: + - `-s, --saved-tag`: Check the existing/stored tag UID. - `-t, --tag`: Enter a tag UID to fetch information about a specific tag. @@ -56,6 +76,7 @@ python swarmsync.py download [options] ``` Options: + - `-c, --count`: Number of concurrent tasks for downloading. - `-u, --beeurl`: Bee node URL(s) (comma-separated) to connect to. @@ -68,6 +89,7 @@ python swarmsync.py check [options] ``` Options: + - `-c, --count`: Number of concurrent tasks for checking. - `-u, --beeurl`: Bee node URL to connect to. @@ -80,6 +102,7 @@ python swarmsync.py upload [options] ``` Options: + - `-p, --path`: Path to the folder to be uploaded. - `-s, --search`: Search parameter (e.g., `*`, `*.jpg`, `somename.txt`). - `-P, --pin`: Should files be pinned during upload. @@ -99,6 +122,7 @@ python swarmsync.py mantaray [options] ``` Options: + - `-c, --count`: Number of concurrent tasks for uploading the Mantaray index. - `-u, --beeurl`: Bee node URL(s) (comma-separated) to connect to. @@ -106,38 +130,64 @@ Options: - To upload a folder with files and subfolders: - ```bash - python swarmsync.py upload -p /path/to/folder - ``` + ```bash + python swarmsync.py upload -p /path/to/folder + ``` - To download files from the Ethereum Swarm network: - ```bash - python swarmsync.py download -c 5 -u http://yourbeeurl:1633 - ``` + ```bash + python swarmsync.py download -c 5 -u http://yourbeeurl:1633 + ``` - To check the status of uploaded files: - ```bash - python swarmsync.py check -c 10 -u http://yourbeeurl:1633 - ``` + ```bash + python swarmsync.py check -c 10 -u http://yourbeeurl:1633 + ``` - To display information about uploaded files and their status: - ```bash - python swarmsync.py show responses - ``` + ```bash + python swarmsync.py show responses + ``` - To create and manage a Mantaray index: - ```bash - python swarmsync.py mantaray -u http://yourbeeurl:1633 - ``` + ```bash + python swarmsync.py mantaray -u http://yourbeeurl:1633 + ``` ### Additional Options Swarmsync allows you to configure additional options by modifying the script. These options include the Prometheus statistics endpoint, xBee header usage, and more. Please refer to the script's source code for these advanced configurations. +## Generate HTML + +If you want to show your set of data available on Swarm, this tool helps you organize it in a comprehensible way. + +```bash +python generate_html.py [anything.json] [footer.txt] +``` + +### Features + +- `footer.txt`: After adding the JSON file, any text file can be added as the footer.
+- `thumbnail.png`: If a file named thumbnail.png is in the working directory, it will be added to the HTML as a full-size header. + +- It will print the exact total size of all the data in megabytes, which can be downloaded. + +- The output file name matches the JSON file it was created from. + +### Example + +```bash +python3 generate_html.py Stack-Exchange-Kiwix.bzz.json footer.txt + +Stack-Exchange-Kiwix.bzz.html file has been generated successfully. +Total size of files: 330160.76 MB +``` + ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/generate_html.py b/generate_html.py new file mode 100644 index 0000000..8c04b35 --- /dev/null +++ b/generate_html.py @@ -0,0 +1,171 @@ +import sys +import re +import json +import os.path +import base64 + +def convert_bytes_to_mb(size_in_bytes): + return round(size_in_bytes / (1024 * 1024), 2) + +def generate_html(json_file, page_title="File Names and References", footer_file=None): + # Extract the filename from the JSON file path + output_filename = os.path.splitext(os.path.basename(json_file))[0] + ".html" + + # Read the JSON file + with open(json_file, 'r') as f: + json_data = f.read() + + # Parse the JSON data + data = json.loads(json_data) + + # Extract the filenames, references, and sizes + table_rows = '' + total_size_mb = 0 # New variable to hold the total size in MB + for obj in data: + file_value = obj.get('file', '') + filename = os.path.basename(file_value) + reference = obj.get('reference', 'Unknown') + size = obj.get('size', 0) + total_size_mb += size # Accumulate the size for total calculation + + link = f'https://pubee.datafund.io/bzz/{reference}' + table_rows += f'{filename}{reference}{convert_bytes_to_mb(size)}' + + # Embed the thumbnail image into base64 format + thumbnail_image = "" + if os.path.exists("thumbnail.png"): + with open("thumbnail.png", "rb") as image_file: + encoded_image = base64.b64encode(image_file.read()).decode('utf-8') + thumbnail_image = f'Thumbnail Image' + + # Read the content from the footer file + footer_content = "" + if footer_file: + with open(footer_file, 'r') as f: + footer_content = f.read() + + # Generate the HTML content + html_content = f''' + + + {page_title} + + + + + {thumbnail_image} +

{page_title}

+ + + + + + + + {table_rows} +
File NameSwarm Reference HashSize in MB
+ + + + ''' + + # Write the HTML content to the output file named after the JSON file + with open(output_filename, 'w') as f: + f.write(html_content) + + # Print the HTML generation message along with the total size in MB + print(f"{output_filename} file has been generated successfully.") + print(f"Total size of files: {convert_bytes_to_mb(total_size_mb)} MB") + +# Check if the JSON file location is provided as an argument +if len(sys.argv) < 2: + print("Please provide the location of the JSON file as an argument.") +else: + json_file_location = sys.argv[1] + page_title = sys.argv[2] if len(sys.argv) >= 3 else "File Names and References" + footer_file = sys.argv[3] if len(sys.argv) >= 4 else None + generate_html(json_file_location, page_title, footer_file)