Skip to content

Commit dc4749f

Browse files
committed
v0.0.111
1 parent 728d0d5 commit dc4749f

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gnostr"
3-
version = "0.0.110"
3+
version = "0.0.111"
44
edition = "2021"
55
description = "gnostr:a git+nostr workflow utility"
66
authors = [

src/bin/gnostr-query.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use gnostr_query::cli::cli;
2+
use gnostr_query::ConfigBuilder;
3+
use log::{debug, trace};
4+
use serde_json::{json, to_string};
5+
use url::Url;
6+
7+
/// Usage
8+
/// nip-0034 kinds
9+
/// gnostr-query -k 1630,1632,1621,30618,1633,1631,1617,30617
10+
#[tokio::main]
11+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
12+
let matches = cli().await?;
13+
let mut filt = serde_json::Map::new();
14+
15+
if let Some(authors) = matches.get_one::<String>("authors") {
16+
filt.insert(
17+
"authors".to_string(),
18+
json!(authors.split(',').collect::<Vec<&str>>()),
19+
);
20+
}
21+
22+
if let Some(ids) = matches.get_one::<String>("ids") {
23+
filt.insert(
24+
"ids".to_string(),
25+
json!(ids.split(',').collect::<Vec<&str>>()),
26+
);
27+
}
28+
29+
let mut limit_check: i32 = 0;
30+
if let Some(limit) = matches.get_one::<i32>("limit") {
31+
// ["EOSE","gnostr-query"] counts as a message! + 1
32+
filt.insert("limit".to_string(), json!(limit.clone() /*+ 1*/));
33+
limit_check = *limit;
34+
}
35+
36+
if let Some(generic) = matches.get_many::<String>("generic") {
37+
let generic_vec: Vec<&String> = generic.collect();
38+
if generic_vec.len() == 2 {
39+
let tag = format!("#{}", generic_vec[0]);
40+
let val = generic_vec[1].split(',').collect::<Vec<&str>>();
41+
filt.insert(tag, json!(val));
42+
}
43+
}
44+
45+
if let Some(hashtag) = matches.get_one::<String>("hashtag") {
46+
filt.insert(
47+
"#t".to_string(),
48+
json!(hashtag.split(',').collect::<Vec<&str>>()),
49+
);
50+
}
51+
52+
if let Some(mentions) = matches.get_one::<String>("mentions") {
53+
filt.insert(
54+
"#p".to_string(),
55+
json!(mentions.split(',').collect::<Vec<&str>>()),
56+
);
57+
}
58+
59+
if let Some(references) = matches.get_one::<String>("references") {
60+
filt.insert(
61+
"#e".to_string(),
62+
json!(references.split(',').collect::<Vec<&str>>()),
63+
);
64+
}
65+
66+
if let Some(kinds) = matches.get_one::<String>("kinds") {
67+
if let Ok(kind_ints) = kinds
68+
.split(',')
69+
.map(|s| s.parse::<i64>())
70+
.collect::<Result<Vec<i64>, _>>()
71+
{
72+
filt.insert("kinds".to_string(), json!(kind_ints));
73+
} else {
74+
eprintln!("Error parsing kinds. Ensure they are integers.");
75+
std::process::exit(1);
76+
}
77+
}
78+
79+
let config = ConfigBuilder::new()
80+
.host("localhost")
81+
.port(8080)
82+
.use_tls(true)
83+
.retries(5)
84+
.authors("")
85+
.ids("")
86+
.limit(limit_check)
87+
.generic("", "")
88+
.hashtag("")
89+
.mentions("")
90+
.references("")
91+
.kinds("")
92+
.build()?;
93+
94+
debug!("{config:?}");
95+
let q = json!(["REQ", "gnostr-query", filt]);
96+
let query_string = to_string(&q)?;
97+
let relay_url_str = matches.get_one::<String>("relay").clone().unwrap();
98+
let relay_url = Url::parse(relay_url_str)?;
99+
let vec_result = gnostr_query::send(query_string.clone(), relay_url, Some(limit_check)).await;
100+
//trace
101+
trace!("{:?}", vec_result);
102+
103+
let mut json_result: Vec<String> = vec![];
104+
for element in vec_result.unwrap() {
105+
println!("{}", element);
106+
json_result.push(element);
107+
}
108+
//trace
109+
for element in json_result {
110+
trace!("json_result={}", element);
111+
}
112+
Ok(())
113+
}

0 commit comments

Comments
 (0)