Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <path-to-swarmsync.py> <command>
```

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,<br>

this is a convenient way to handle. Upload the created HTML via swarmsync.

```bash
python3 <path-to-generate_html.py> <name-of-json-file> <name-of-footer.txt>
```

#### Show

You can use the `show` command to display information about the files you have uploaded and their status.
Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -99,45 +122,72 @@ 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.

### Examples

- 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. <br>
- `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.
171 changes: 171 additions & 0 deletions generate_html.py
Original file line number Diff line number Diff line change
@@ -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'<tr><td>{filename}</td><td><a href="{link}" target="_blank">{reference}</a></td><td style="text-align: right;">{convert_bytes_to_mb(size)}</td></tr>'

# 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'<img src="data:image/png;base64,{encoded_image}" alt="Thumbnail Image" style="width: 100%; height: auto;">'

# 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'''
<html>
<head>
<title>{page_title}</title>
<style>
/* CSS styles go here */
body {{
font-family: Arial, sans-serif;
background-color: #f5f5f5;
color: #333;
}}

h1 {{
text-align: center;
color: #007bff;
margin-top: 30px;
}}

table {{
width: 90%;
margin: 20px auto;
border-collapse: collapse;
background-color: #fff;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}}

table th, table td {{
border: 1px solid #ddd;
padding: 10px;
}}

table th {{
background-color: #007bff;
color: #fff;
text-align: left;
font-weight: bold;
}}

table td:first-child {{
color: #007bff;
}}

input[type="text"] {{
width: 90%;
padding: 12px;
margin: 20px auto;
display: block;
border: 2px solid #007bff;
border-radius: 5px;
box-sizing: border-box;
}}

input[type="text"]:focus {{
outline: none;
border-color: #0056b3;
}}

footer {{
background-color: #007bff;
color: #fff;
text-align: center;
padding: 5px 0;
width: 100%;
position: fixed;
bottom: 0;
}}
</style>
<script>
function searchFile() {{
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("searchInput");
filter = input.value.toUpperCase();
table = document.getElementById("fileTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {{
td = tr[i].getElementsByTagName("td")[0];
if (td) {{
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {{
tr[i].style.display = "";
return;
}} else {{
tr[i].style.display = "none";
}}
}}
}}
}}
</script>
</head>
<body>
{thumbnail_image}
<h1>{page_title}</h1>
<input type="text" id="searchInput" onkeyup="searchFile()" placeholder="Search for file names...">
<table id="fileTable">
<tr>
<th>File Name</th>
<th>Swarm Reference Hash</th>
<th style="text-align: right;">Size in MB</th>
</tr>
{table_rows}
</table>
<footer>
{footer_content}
</footer>
</body>
</html>
'''

# 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)