560 lines
18 KiB
Rust
560 lines
18 KiB
Rust
use std::fs;
|
|
use std::fs::create_dir;
|
|
use std::path::Path;
|
|
use std::collections::HashMap;
|
|
|
|
use reqwest::header::{USER_AGENT, AUTHORIZATION};
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
use serde_json::Value;
|
|
|
|
use dirs;
|
|
|
|
const PK_URL: &str = "https://api.pluralkit.me/v2";
|
|
const SP_URL: &str = "https://api.apparyllis.com/v1";
|
|
|
|
#[derive(Debug)]
|
|
#[derive(Serialize)]
|
|
#[derive(Deserialize)]
|
|
#[derive(Clone)]
|
|
struct System {
|
|
pk_userid: String,
|
|
sp_userid: String,
|
|
members: Vec<Member>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
#[derive(Serialize)]
|
|
#[derive(Deserialize)]
|
|
#[derive(Clone)]
|
|
#[derive(PartialEq)]
|
|
struct Member {
|
|
pk_id: String,
|
|
sp_id: String,
|
|
name: String,
|
|
alias: String
|
|
}
|
|
|
|
fn main() {
|
|
if std::env::args().len() > 1 {
|
|
let augs: Vec<String> = std::env::args().collect();
|
|
let command = &augs[1];
|
|
let config_path: String;
|
|
match dirs::config_dir() {
|
|
Some(x) => {
|
|
config_path = format!("{}/pluralshit", x.display());
|
|
match command.as_str() {
|
|
"sync" => {
|
|
let _ = sync(config_path);
|
|
},
|
|
"set" => {
|
|
if std::env::args().len() > 2 {
|
|
match set_member(config_path, &augs[2..]) {
|
|
Ok(_) => (),
|
|
Err(e) => println!("{e}"),
|
|
}
|
|
} else {
|
|
//set_empty(config_path);
|
|
}
|
|
},
|
|
"add" => {
|
|
if std::env::args().len() > 2 {
|
|
match add_member(config_path, &augs[2..]) {
|
|
Ok(_) => (),
|
|
Err(e) => println!("{e}"),
|
|
}
|
|
} else {
|
|
//add_empty(config_path);
|
|
}
|
|
|
|
}
|
|
"get" => get(config_path),
|
|
"memberlist" => memberlist(config_path),
|
|
&_ => println!("PluralSync\nMade by Jvnko@lavenderfield\n\nUsage:\npluralshit [COMMAND] [member1] [member2]...\n\nCommands:\nsync - Fetches the system member information from PluralKit and SimplyPlural\nset - Sets one or more members to the front\nget - Refreshes the current front information\nadd - Adds a member to the existing front\nmemberlist - Shows loaded member list"),
|
|
}
|
|
},
|
|
None => println!("Something went wrong")
|
|
}
|
|
} else {
|
|
println!("PluralSync\nMade by Jvnko@lavenderfield\n\nUsage:\npluralshit [COMMAND] [member1] [member2]...\n\nCommands:\nsync - Fetches the system member information from PluralKit and SimplyPlural\nset - Sets one or more members to the front\nget - Refreshes the current front information\nadd - Adds a member to the existing front\nmemberlist - Shows loaded member list");
|
|
}
|
|
}
|
|
|
|
fn sync(config_path: String) -> Result<(), String>{
|
|
// Get config
|
|
let config = get_config(&config_path);
|
|
if config == Value::Null {
|
|
println!("Stopping.");
|
|
return Ok(());
|
|
}
|
|
|
|
let pk_key = &config["pk_key"].as_str().unwrap();
|
|
let sp_key = &config["sp_key"].as_str().unwrap();
|
|
|
|
// Get Pluralkit system id
|
|
let pk_sys = pk_get_system(pk_key);
|
|
let pk_sysid = pk_sys["id"].as_str().unwrap();
|
|
|
|
// Get Simplyplural user id
|
|
let sp_user_id = sp_get_userid(sp_key);
|
|
|
|
// Get Simplyplural member ids
|
|
let sp_member_ids = sp_get_memberids(sp_key, &sp_user_id);
|
|
|
|
// get members
|
|
let pk_members = pk_get_members(pk_key, pk_sysid);
|
|
let mut members: Vec<Member> = Vec::new();
|
|
for member in pk_members {
|
|
let mut m = Member {
|
|
pk_id: member["id"].as_str().unwrap().to_string(),
|
|
sp_id: String::new(),
|
|
name: member["name"].as_str().unwrap().to_string(),
|
|
alias: String::new()
|
|
};
|
|
|
|
if member["display_name"].as_str() != None {
|
|
m.alias = member["display_name"].as_str().unwrap().to_string();
|
|
} else {
|
|
m.alias = String::from(&m.name);
|
|
}
|
|
|
|
m.sp_id = get_sp_id(&m, &sp_member_ids);
|
|
|
|
members.push(m);
|
|
}
|
|
|
|
let sys = System {
|
|
pk_userid: pk_sysid.to_string(),
|
|
sp_userid: sp_user_id,
|
|
members: members.clone(),
|
|
};
|
|
|
|
let json = serde_json::to_string(&sys);
|
|
let _ = fs::write(format!("{}/system.json", config_path), &json.unwrap());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/*
|
|
TODO fn set_empty(config_path: String) {
|
|
TODO let config = load_json(format!("{}/config.json", config_path));
|
|
TODO let system = get_system(&config_path);
|
|
TODO }
|
|
*/
|
|
|
|
fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static str> {
|
|
let config = get_config(&config_path);
|
|
if config == Value::Null {
|
|
return Err("Config not found. Stopping");
|
|
}
|
|
|
|
let system: System = get_system(&config_path);
|
|
|
|
let mut to_front: Vec<Member> = Vec::new();
|
|
for member in tf_members {
|
|
for mem in &system.members {
|
|
if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() {
|
|
println!("Member {member} found");
|
|
to_front.push(mem.clone());
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if to_front.len() != 0 {
|
|
let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system);
|
|
|
|
pk_set_fronters(&config["pk_key"].as_str().unwrap(), &system, &to_front, &fronters);
|
|
sp_set_fronters(&config["sp_key"].as_str().unwrap(), &to_front, &fronters);
|
|
|
|
get(config_path);
|
|
} else {
|
|
println!("No members found. Known members:\n--------------------------");
|
|
memberlist(config_path);
|
|
println!("--------------------------\nIf a member is missing from the system try running \"pluralshit sync\" to refresh the local database");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn add_member(config_path: String, tf_members: &[String]) -> Result<(), &'static str> {
|
|
let config = get_config(&config_path);
|
|
if config == Value::Null {
|
|
return Err("Config not found. Stopping");
|
|
}
|
|
|
|
let system: System = get_system(&config_path);
|
|
|
|
let mut to_front: Vec<Member> = Vec::new();
|
|
for member in tf_members {
|
|
for mem in &system.members {
|
|
if mem.name.to_lowercase() == member.to_lowercase() || mem.alias.to_lowercase() == member.to_lowercase() {
|
|
to_front.push(mem.clone());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if to_front.len() != 0 {
|
|
let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system);
|
|
|
|
let mut aux: Vec<Member> = Vec::new();
|
|
aux.append(&mut fronters["pk"].clone());
|
|
aux.append(&mut to_front);
|
|
to_front = aux;
|
|
|
|
pk_set_fronters(&config["pk_key"].as_str().unwrap(), &system, &to_front, &fronters);
|
|
sp_set_fronters(&config["sp_key"].as_str().unwrap(), &to_front, &fronters);
|
|
|
|
get(config_path);
|
|
} else {
|
|
println!("No members found. Known members:\n--------------------------");
|
|
memberlist(config_path);
|
|
println!("--------------------------\nIf a member is missing from the system try running \"pluralshit sync\" to refresh the local database");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn memberlist(config_path: String) {
|
|
let sys = get_system(&config_path);
|
|
|
|
for mem in sys.members {
|
|
if mem.name != mem.alias {
|
|
println!("{} / {}", mem.name, mem.alias);
|
|
} else {
|
|
println!("{}", mem.name);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
fn get(config_path: String) {
|
|
let config = get_config(&config_path);
|
|
let sys = get_system(&config_path);
|
|
|
|
let f = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &sys);
|
|
let mut names = Vec::new();
|
|
for m in &f["pk"] {
|
|
names.push(String::from(&m.name));
|
|
}
|
|
let fronters = names.join(" || ");
|
|
println!("Currently fronting: {}", fronters);
|
|
let _ = fs::write(format!("{}/.front", config_path), fronters);
|
|
}
|
|
|
|
fn pk_get_system(key: &str) -> Value {
|
|
let url = format!("{}/systems/@me", PK_URL);
|
|
|
|
let res = http_get_request(url,key);
|
|
return serde_json::from_str(&res.unwrap()).unwrap();
|
|
}
|
|
|
|
fn pk_get_members(key: &str, sysid: &str) -> Vec<Value> {
|
|
let url = format!("{}/systems/{}/members", PK_URL, sysid);
|
|
|
|
let res = http_get_request(url,key);
|
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
|
|
|
return datas;
|
|
}
|
|
|
|
fn pk_get_fronters(key: &str, sys: &System) -> Vec<Member> {
|
|
let url = format!("{}/systems/{}/fronters", PK_URL, sys.pk_userid);
|
|
|
|
let res = http_get_request(url,key);
|
|
let data: Value = serde_json::from_str(&res.unwrap()).unwrap();
|
|
let memberdata = &data["members"].as_array();
|
|
|
|
let mut members: Vec<Member> = Vec::new();
|
|
for member in memberdata {
|
|
for m in member.into_iter() {
|
|
for dbmem in &sys.members {
|
|
if m["name"].as_str().unwrap() == dbmem.name {
|
|
members.push(dbmem.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return members;
|
|
}
|
|
|
|
fn pk_set_fronters(key: &str, sys: &System, to_front: &Vec<Member>, fronters: &HashMap<String, Vec<Member>>) {
|
|
let url = format!("{}/systems/{}/switches", PK_URL, sys.pk_userid);
|
|
|
|
if to_front != &fronters["pk"] {
|
|
let mut frontcodes = Vec::new();
|
|
for tf in to_front {
|
|
frontcodes.push(String::from(&tf.pk_id));
|
|
}
|
|
let mut body: HashMap<&str, Vec<String>> = HashMap::new();
|
|
body.insert("members", frontcodes);
|
|
let _ = http_post_request(url, key, &body);
|
|
} else {
|
|
println!("Members already fonting");
|
|
}
|
|
|
|
}
|
|
|
|
fn sp_get_userid(key: &str) -> String {
|
|
let url = format!("{}/me", SP_URL);
|
|
|
|
let res = http_get_request(url,key);
|
|
let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap();
|
|
return json_res["id"].as_str().unwrap().to_string();
|
|
}
|
|
|
|
fn sp_get_memberids(key: &str, system_id: &str) -> HashMap<String, String> {
|
|
let url = format!("{}/members/{}", SP_URL, system_id);
|
|
|
|
let res = http_get_request(url,key);
|
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
|
|
|
let mut sp_memberdata: HashMap<String, String> = HashMap::new();
|
|
for data in datas {
|
|
sp_memberdata.insert(String::from(data["content"]["name"].as_str().unwrap()), String::from(data["id"].as_str().unwrap()));
|
|
}
|
|
|
|
return sp_memberdata;
|
|
}
|
|
|
|
fn get_sp_id(mem: &Member, ids: &HashMap<String, String>) -> String {
|
|
|
|
let mut member_id = String::new();
|
|
|
|
for (mn, mid) in ids {
|
|
if &mem.name == mn || &mem.alias == mn {
|
|
member_id = String::from(mid);
|
|
}
|
|
}
|
|
|
|
return member_id;
|
|
}
|
|
|
|
fn sp_get_fronters(key: &str, sys: &System) -> Vec<Member> {
|
|
let url = format!("{}/fronters", SP_URL);
|
|
|
|
let res = http_get_request(url,key);
|
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
|
|
|
let mut members = Vec::new();
|
|
for data in datas {
|
|
let sp_id = &data["content"]["member"].as_str().unwrap();
|
|
for member in &sys.members {
|
|
if &member.sp_id == sp_id {
|
|
members.push(member.clone());
|
|
}
|
|
}
|
|
|
|
}
|
|
return members;
|
|
}
|
|
|
|
fn sp_get_frontids(key: &str, to_id: &Member) -> String {
|
|
let url = format!("{}/fronters", SP_URL);
|
|
|
|
let res = http_get_request(url,key);
|
|
let datas: Vec<Value> = serde_json::from_str(&res.unwrap()).unwrap();
|
|
|
|
for data in datas {
|
|
let sp_f_id = &data["id"].as_str().unwrap();
|
|
let sp_id = &data["content"]["member"].as_str().unwrap();
|
|
if sp_id.to_string() == to_id.sp_id {
|
|
return sp_f_id.to_string();
|
|
}
|
|
}
|
|
return String::new();
|
|
}
|
|
|
|
fn sp_set_fronters(key: &str, to_front: &Vec<Member>, fronters: &HashMap<String, Vec<Member>>) {
|
|
if to_front != &fronters["sp"] {
|
|
for fronting_member in &fronters["sp"] {
|
|
if !to_front.contains(&fronting_member) {
|
|
let f_id = sp_get_frontids(&key, &fronting_member);
|
|
|
|
let url = format!("{}/frontHistory/{}", SP_URL, f_id);
|
|
|
|
#[derive (Serialize)]
|
|
#[allow (non_snake_case)]
|
|
struct SpRem {
|
|
live: bool,
|
|
endTime: u64
|
|
}
|
|
|
|
let end_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs();
|
|
let rem = SpRem {
|
|
live: false,
|
|
endTime: end_time * 1000
|
|
};
|
|
|
|
let body = serde_json::to_string(&rem).expect("Error");
|
|
|
|
let _ = http_patch_request(url, key, body);
|
|
}
|
|
}
|
|
|
|
for tf_member in to_front {
|
|
if !fronters["sp"].contains(&tf_member) {
|
|
let url = format!("{}/frontHistory", SP_URL);
|
|
|
|
#[derive (Serialize)]
|
|
#[allow (non_snake_case)]
|
|
struct SpAdd {
|
|
member: String,
|
|
custom: bool,
|
|
live: bool,
|
|
startTime: u64
|
|
}
|
|
|
|
let start_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs();
|
|
let rem = SpAdd {
|
|
member: String::from(&tf_member.sp_id),
|
|
custom: false,
|
|
live: true,
|
|
startTime: start_time * 1000
|
|
};
|
|
|
|
let body = serde_json::to_string(&rem).expect("Error");
|
|
|
|
let _ = http_post_request_st(url, key, body);
|
|
}
|
|
}
|
|
|
|
/*
|
|
let mut frontcodes = Vec::new();
|
|
for tf in to_front {
|
|
frontcodes.push(tf.sp_id);
|
|
}
|
|
let mut body: HashMap<&str, Vec<String>> = HashMap::new();
|
|
body.insert("members", frontcodes);
|
|
*/
|
|
//let _ = http_post_request(url, key, &body);
|
|
} else {
|
|
println!("Members already fonting");
|
|
}
|
|
|
|
}
|
|
|
|
fn load_json(path: String) -> Value {
|
|
if Path::new(&path).exists() {
|
|
let config_data = fs::read_to_string(&path).expect("File not found");
|
|
return serde_json::from_str(&config_data).unwrap();
|
|
} else {
|
|
println!("Config file in {path} not found");
|
|
return Value::Null;
|
|
}
|
|
}
|
|
|
|
fn get_config(config_path: &str) -> Value {
|
|
let path = format!("{}/config.json", config_path);
|
|
if Path::new(config_path).exists() {
|
|
|
|
let result = load_json(String::from(&path));
|
|
|
|
if result == Value::Null {
|
|
println!("Config file missing, creating template in {path}");
|
|
let _ = fs::write(path, r#"
|
|
{
|
|
"pk_key": "// Pluralkit token",
|
|
"sp_key": "// Simplplural token"
|
|
}
|
|
"#);
|
|
}
|
|
|
|
return result;
|
|
} else {
|
|
println!("Directory {config_path} does not exist. Creating with template config");
|
|
let _ = create_dir(config_path);
|
|
let _ = fs::write(path, r#"
|
|
{
|
|
"pk_key": "// Pluralkit token",
|
|
"sp_key": "// Simplplural token"
|
|
}
|
|
"#);
|
|
return Value::Null;
|
|
}
|
|
}
|
|
|
|
fn get_system(config_path: &str) -> System {
|
|
let path = format!("{}/system.json", config_path);
|
|
|
|
let mut result = load_json(String::from(&path));
|
|
|
|
if result == Value::Null {
|
|
println!("Syncing system config");
|
|
let _ = sync(String::from(config_path));
|
|
result = load_json(String::from(&path));
|
|
}
|
|
|
|
let vec = serde_json::to_vec(&result).unwrap();
|
|
let sys = serde_json::from_slice::<System>(&vec).unwrap();
|
|
|
|
return sys;
|
|
|
|
}
|
|
|
|
fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap<String, Vec<Member>> {
|
|
let mut fronters: HashMap<String, Vec<Member>> = HashMap::new();
|
|
fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys));
|
|
fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys));
|
|
|
|
// if fronters["pk"] != fronters["sp"] {
|
|
// fronters.insert(String::from("pk"), fronters["sp"].clone());
|
|
// }
|
|
|
|
return fronters;
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn http_get_request(url: String, key: &str) -> Result<String, Box<dyn std::error::Error>> {
|
|
let client = reqwest::Client::new();
|
|
let res = client
|
|
.get(url)
|
|
.header(USER_AGENT, "Pluralsync")
|
|
.header(AUTHORIZATION, key)
|
|
.send()
|
|
.await?
|
|
.text()
|
|
.await?;
|
|
Ok(res)
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
|
|
let client = reqwest::Client::new();
|
|
let _ = client
|
|
.post(url)
|
|
.json(body)
|
|
.header(USER_AGENT, "Pluralsync")
|
|
.header(AUTHORIZATION, key)
|
|
.send()
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn http_post_request_st(url: String, key: &str, body: String) -> Result<(), Box<dyn std::error::Error>> {
|
|
let client = reqwest::Client::new();
|
|
let c = client
|
|
.post(url)
|
|
.body(body)
|
|
.header("content-type", "application/json; charset=utf-8")
|
|
.header(USER_AGENT, "Pluralsync")
|
|
.header(AUTHORIZATION, key);
|
|
let _res = c.send()
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn http_patch_request(url: String, key: &str, body: String ) -> Result<(), Box<dyn std::error::Error>> {
|
|
let client = reqwest::Client::new();
|
|
let c = client
|
|
.patch(url)
|
|
.body(body)
|
|
.header("content-type", "application/json; charset=utf-8")
|
|
.header(USER_AGENT, "Pluralsync")
|
|
.header(AUTHORIZATION, key);
|
|
let _res = c.send()
|
|
.await?;
|
|
Ok(())
|
|
}
|