From 140790db602ffc952889d344825849f306a72007 Mon Sep 17 00:00:00 2001 From: Juraj Bednar Date: Sun, 28 Jul 2024 18:44:11 +0200 Subject: [PATCH] initial commit --- LICENSE | 26 ++++++++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ nostr-backup.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ slow-post-all.sh | 11 +++++++++++ slow-post.py | 26 ++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 nostr-backup.sh create mode 100644 slow-post-all.sh create mode 100644 slow-post.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..33850ee --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ + GLWTS(Good Luck With That Shit) Public License + Copyright (c) Every-fucking-one, except the Author + +Everyone is permitted to copy, distribute, modify, merge, sell, publish, +sublicense or whatever the fuck they want with this software but at their +OWN RISK. + + Preamble + +The author has absolutely no fucking clue what the code in this project +does. It might just fucking work or not, there is no third option. + + + GOOD LUCK WITH THAT SHIT PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION + + 0. You just DO WHATEVER THE FUCK YOU WANT TO as long as you NEVER LEAVE +A FUCKING TRACE TO TRACK THE AUTHOR of the original product to blame for +or hold responsible. + +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +Good luck and Godspeed. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fd71b42 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# nostr-scripts + +These are a few scripts that I use for Nostr. + +They usually need [nak](https://github.com/fiatjaf/nak) in PATH. + +## nostr-backup.sh + +Used to backup my notes to my own relay. You need to edit the script to configure everything, +it's well commented. + +I run it from cron. + +## slow-post.py + +Used to slowly post events to a nostr relay. Many relays have rate limits, so I want to delay +posting, so I don't hammer them with events. You can adjust the speed of posting by changing the sleep time in the source code. + +## slow-post-all.sh + +Micro script that orchestrates posting to many relays at once. Uses slow-post.py. diff --git a/nostr-backup.sh b/nostr-backup.sh new file mode 100644 index 0000000..60a7c4f --- /dev/null +++ b/nostr-backup.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Gets the events authored by users mentioned in the for cycle +# and publishes them to a target relay + +# Use when you deploy your own relay and want to use it as a backup +# for your events + +# See comments for what and how to adjust + +# Where to publish it to (f.e. your own relay) +TARGET_RELAY="wss://nostr.my.relay" +# Since when to start getting events. If you run this from cron, can be +# for example 7 days ago. +SINCE=$(date -d "7 days ago" +%s) +# For first run: +# SINCE=0 + +# Make sure ~/tmp exists, or else it will fail +> ~/tmp/nostr-backup.json + +# hex of the nostr account you want to backup (get hex from npub +# at https://nostrcheck.me/converter/ ) +# You can add as many as you want, add \ to the end of each but the last line + +for author in \ + "dab6c6065c439b9bafb0b0f1ff5a0c68273bce5c1959a4158ad6a70851f507b6" +do + echo "Getting notes for author ${author}..." + # Here is the set of source relays (where we want to get events from) + for relay in \ + "wss://nostr.mom" \ + 'wss://nos.lol' \ + 'wss://nostr.bitcoiner.social' \ + 'wss://relay.nostr.band' \ + 'wss://relay.damus.io' \ + 'wss://relay.nostrplebs.com' + do + echo " --> from relay ${relay}..." + nak req --author "${author}" --since "${SINCE}" "${relay}" >> ~/tmp/nostr-backup.json + done +done + +echo "Publishing to ${TARGET_RELAY}..." +sort -u ~/tmp/nostr-backup.json | nak event ${TARGET_RELAY} diff --git a/slow-post-all.sh b/slow-post-all.sh new file mode 100644 index 0000000..3563837 --- /dev/null +++ b/slow-post-all.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Take a file as argument and publish it to relays mentioned in the for cycle below +# File should be a jsonl file with one json object per line + +for RELAY in wss://relay.nostrplebs.com wss://relay.nostr.band +do + echo "Publishing to ${RELAY}" + # We can post to different relays in parallel, they don't know about each other + do python3 slow-post.py "${RELAY}" < "${1}";done ) & +done diff --git a/slow-post.py b/slow-post.py new file mode 100644 index 0000000..52843b1 --- /dev/null +++ b/slow-post.py @@ -0,0 +1,26 @@ +import subprocess +import sys +import time +import json + +if len(sys.argv) < 2: + print("Error: Not enough arguments provided, I need a relay.") + sys.exit(1) + +relay = sys.argv[1] + +data = json.loads(sys.stdin.read()) + +# Execute the binary for each element in the JSON array +for item in data: + # Replace 'binary_path' with the actual path to your binary executable + process = subprocess.Popen(['nak','event', relay], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + process.stdin.write(json.dumps(item).encode('utf-8')) # Send the item as a JSON string to the process's stdin + process.stdin.flush() + output, error = process.communicate() + + # Print the output and error (if any) for debugging purposes + print('Output:', output.decode('utf-8')) + print('Error:', error.decode('utf-8')) + + time.sleep(1)