Skip to content

Commit

Permalink
nostrs: Add metadata subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 23, 2023
1 parent 4feadde commit 35e7bb0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions nostrs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nostrs/Cargo.toml
Expand Up @@ -9,4 +9,5 @@ edition = "2021"
anyhow = "1.0.69"
clap = { version = "4.1.6", features = ["derive"] }
nostr-sdk = "0.18"
serde_json = "1.0.93"
tokio = { version = "1.25.0", features = ["full"] }
1 change: 1 addition & 0 deletions nostrs/src/handler.rs
@@ -1 +1,2 @@
pub mod metadata;
pub mod timeline;
29 changes: 29 additions & 0 deletions nostrs/src/handler/metadata.rs
@@ -0,0 +1,29 @@
use std::{env, time::Duration};

use anyhow::Context;
use nostr_sdk::{
prelude::{FromSkStr, Keys, Kind, SubscriptionFilter},
Client,
};
use serde_json::Value;

pub async fn handle() -> anyhow::Result<()> {
let my_keys = Keys::from_sk_str(env::var("SECRET_KEY")?.as_str())?;

let client = Client::new(&my_keys);
client
.add_relay("wss://nostr-pub.wellorder.net", None)
.await?;
client.connect().await;

let filter = SubscriptionFilter::new()
.kind(Kind::Metadata)
.author(my_keys.public_key())
.limit(1);
let timeout = Duration::from_secs(10);
let events = client.get_events_of(vec![filter], Some(timeout)).await?;
let event = events.first().context("metadata not found")?;
let metadata: Value = serde_json::from_str(event.content.as_str())?;
println!("{}", serde_json::to_string_pretty(&metadata)?);
Ok(())
}
2 changes: 2 additions & 0 deletions nostrs/src/main.rs
Expand Up @@ -9,13 +9,15 @@ struct Command {

#[derive(clap::Subcommand)]
enum Subcommand {
Metadata,
Timeline,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let command = <Command as clap::Parser>::parse();
match command.subcommand {
Subcommand::Metadata => handler::metadata::handle().await,
Subcommand::Timeline => handler::timeline::handle().await,
}
}

0 comments on commit 35e7bb0

Please sign in to comment.