1
0
Fork 0
This commit is contained in:
ChaotiCryptidz 2022-08-22 21:09:57 +01:00
parent bb3a54a012
commit bde32f5dd7
No known key found for this signature in database
8 changed files with 262 additions and 47 deletions

38
src/args.rs Normal file
View file

@ -0,0 +1,38 @@
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[clap()]
pub struct CLIArgs {
#[clap(subcommand)]
pub command: Commands,
}
#[derive(Debug, Subcommand)]
pub enum Commands {
Deploy(DeployArgs),
CreateTelegramStickerPack(CreateTelegramStickerPackArgs),
CheckMissing(CheckMissingArgs),
}
#[derive(Debug, Args)]
pub struct DeployArgs {
pub folder: String,
pub deploy_id: String,
}
#[derive(Debug, Args)]
pub struct CreateTelegramStickerPackArgs {
pub token: String,
pub user_id: u64,
pub name: String,
pub title: String,
pub emojis: String,
pub r#type: String,
pub filename: String,
}
#[derive(Debug, Args)]
pub struct CheckMissingArgs {
pub folder: String,
pub sticker_sets: Vec<String>,
}

57
src/cmd_check_missing.rs Normal file
View file

@ -0,0 +1,57 @@
use std::{collections::HashMap, fs::File, path::PathBuf};
use crate::{args::CheckMissingArgs, sticker_config::StickerConfig};
pub async fn check_missing(args: CheckMissingArgs) {
let base_path = PathBuf::from(&args.folder);
let config_path = base_path.join("stickers.yml");
let config_file = File::open(config_path).expect("could not open stickers.yml");
let config: StickerConfig =
serde_yaml::from_reader(config_file).expect("could not parse stickers.yml");
let stickers_for_sticker_sets: Vec<(String, Vec<String>)> = args
.sticker_sets
.iter()
.map(|pack_id| {
return (
pack_id.clone(),
config.sticker_sets.get(pack_id).unwrap().clone(),
);
})
.collect();
let missing_stickers: Vec<(String, Vec<String>)> = stickers_for_sticker_sets
.iter()
.map(|(sticker_set_name, sticker_set_stickers)| {
return (
sticker_set_name.clone(),
config
.stickers
.iter()
.filter(|all_stickers_sticker| {
!sticker_set_stickers.contains(all_stickers_sticker.0)
})
.map(|b| b.0.clone())
.collect::<Vec<String>>(),
);
})
.collect();
let total_missing: Vec<String> = HashMap::<String, bool>::from_iter(
missing_stickers
.iter()
.map(|(_, stickers)| stickers.clone())
.collect::<Vec<Vec<String>>>()
.concat()
.iter()
.map(|sticker_name| (sticker_name.clone(), true)),
)
.into_keys()
.collect();
for (pack_name, stickers) in missing_stickers.iter() {
println!("Pack: {}\nMissing: {:#?}\n", pack_name, stickers);
}
println!("Total Missing {:#?}", total_missing);
}

45
src/cmd_createtg.rs Normal file
View file

@ -0,0 +1,45 @@
use crate::args::CreateTelegramStickerPackArgs;
use crate::tg_api::TelegramAPI;
use std::fs::File;
use std::io::Read;
pub async fn create_telegram_sticker_pack(args: CreateTelegramStickerPackArgs) {
let bot = TelegramAPI::new(args.token);
let mut file = File::open(args.filename).unwrap();
let mut file_data: Vec<u8> = Vec::new();
file.read_to_end(&mut file_data)
.expect("could not read file");
let mut png_sticker: Option<Vec<u8>> = None;
let mut tgs_sticker: Option<Vec<u8>> = None;
let mut webm_sticker: Option<Vec<u8>> = None;
match args.r#type.as_str() {
"regular" => {
png_sticker = Some(file_data);
}
"animated" => {
tgs_sticker = Some(file_data);
}
"video" => {
webm_sticker = Some(file_data);
}
_ => {
panic!("wrong type, use regular, animated or video")
}
}
bot.create_sticker_pack(
args.user_id,
args.name,
args.title,
args.emojis,
png_sticker,
tgs_sticker,
webm_sticker,
)
.await
.expect("could not create sticker pack");
}

40
src/cmd_deploy.rs Normal file
View file

@ -0,0 +1,40 @@
use crate::creds::Creds;
use crate::deploy_discord::deploy_discord;
use crate::deploy_telegram::deploy_telegram;
use crate::sticker_config::StickerConfig;
use std::fs::File;
use std::path::PathBuf;
pub async fn deploy(args: crate::args::DeployArgs) {
let base_path = PathBuf::from(&args.folder);
let config_path = base_path.join("stickers.yml");
let creds_path = base_path.join("../stickerdb/creds.yml");
let config_file = File::open(config_path).expect("could not open stickers.yml");
let creds_file = File::open(creds_path).expect("could not open creds.yml");
let config: StickerConfig =
serde_yaml::from_reader(config_file).expect("could not parse stickers.yml");
let creds: Creds = serde_yaml::from_reader(creds_file).expect("could not parse creds.yml");
match config
.deploy_where
.get(&args.deploy_id)
.expect("no deploy config with id specified found")
.deploy_to
.as_str()
{
"discord" => {
println!("deploying {} to discord", &args.deploy_id);
deploy_discord(args.deploy_id, config, creds, args.folder).await;
}
"telegram" => {
println!("deploying {} to telegram", &args.deploy_id);
deploy_telegram(args.deploy_id, config, creds, args.folder).await;
}
_ => {
panic!("deploy_to not set")
}
}
}

View file

@ -97,6 +97,7 @@ pub async fn deploy_discord(
.sticker_sets .sticker_sets
.get(&deploy_where.pack_id) .get(&deploy_where.pack_id)
.unwrap(); .unwrap();
let pack_emojis: HashMap<String, Sticker> = pack_contents let pack_emojis: HashMap<String, Sticker> = pack_contents
.iter() .iter()
.map(|emoji_name| { .map(|emoji_name| {
@ -210,11 +211,6 @@ pub async fn deploy_discord(
.expect("could not upload emoji"); .expect("could not upload emoji");
} }
} }
println!(
"Missing: {:#?}\nInvalid: {:#?}",
missing_emojis, invalid_emojis
);
} }
//println!("{:#?}", emojis_per_server); //println!("{:#?}", emojis_per_server);

View file

@ -93,6 +93,20 @@ pub async fn deploy_telegram(
panic!("pack contains a invalid type of emoji for pack type") panic!("pack contains a invalid type of emoji for pack type")
} }
let max = match deploy_location.r#type {
StickerType::Regular => 120,
StickerType::TelegramVideo => 50,
StickerType::TelegramAnimated => 50,
_ => {
unreachable!();
}
};
if pack_contents.len() >= max - 1 {
panic!("too many stickers in pack");
}
println!("{}", pack_contents.len());
let tg_bot = TelegramAPI::new(creds.telegram_bot_token); let tg_bot = TelegramAPI::new(creds.telegram_bot_token);
let tg_sticker_set = tg_bot let tg_sticker_set = tg_bot
.get_sticker_set(&deploy_location.name) .get_sticker_set(&deploy_location.name)
@ -210,8 +224,6 @@ pub async fn deploy_telegram(
new_stickers.push(sticker_name.clone()); new_stickers.push(sticker_name.clone());
} }
println!("{:#?}", missing_stickers);
} }
// Update tg_stickers with current sticker contents // Update tg_stickers with current sticker contents

View file

@ -1,57 +1,32 @@
pub mod args;
pub mod cmd_check_missing;
pub mod cmd_createtg;
pub mod cmd_deploy;
pub mod creds; pub mod creds;
pub mod deploy_discord; pub mod deploy_discord;
pub mod deploy_telegram; pub mod deploy_telegram;
pub mod sticker_config; pub mod sticker_config;
pub mod tg_api; pub mod tg_api;
use creds::Creds;
use deploy_discord::deploy_discord;
use deploy_telegram::deploy_telegram;
use sticker_config::StickerConfig;
use clap::Parser; use clap::Parser;
use std::fs::File;
use std::path::PathBuf;
#[derive(Debug, Parser)] use args::{CLIArgs, Commands};
#[clap()] use cmd_check_missing::check_missing;
pub struct CLIArgs { use cmd_createtg::create_telegram_sticker_pack;
pub folder: String, use cmd_deploy::deploy;
pub deploy_id: String,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let args = CLIArgs::parse(); let args = CLIArgs::parse();
match args.command {
let base_path = PathBuf::from(&args.folder); Commands::Deploy(subcommand_args) => {
let config_path = base_path.join("stickers.yml"); deploy(subcommand_args).await;
let creds_path = base_path.join("../stickerdb/creds.yml");
let config_file = File::open(config_path).expect("could not open stickers.yml");
let creds_file = File::open(creds_path).expect("could not open creds.yml");
let config: StickerConfig =
serde_yaml::from_reader(config_file).expect("could not parse stickers.yml");
let creds: Creds = serde_yaml::from_reader(creds_file).expect("could not parse creds.yml");
match config
.deploy_where
.get(&args.deploy_id)
.expect("no deploy config with id specified found")
.deploy_to
.as_str()
{
"discord" => {
println!("deploying {} to discord", &args.deploy_id);
deploy_discord(args.deploy_id, config, creds, args.folder).await;
} }
"telegram" => { Commands::CreateTelegramStickerPack(subcommand_args) => {
println!("deploying {} to telegram", &args.deploy_id); create_telegram_sticker_pack(subcommand_args).await;
deploy_telegram(args.deploy_id, config, creds, args.folder).await;
} }
_ => { Commands::CheckMissing(subcommand_args) => {
panic!("deploy_to not set") check_missing(subcommand_args).await;
} }
} }
} }

View file

@ -116,6 +116,58 @@ impl TelegramAPI {
Ok(res_json.result.unwrap()) Ok(res_json.result.unwrap())
} }
#[allow(clippy::too_many_arguments)]
pub async fn create_sticker_pack(
&self,
user_id: u64,
name: String,
title: String,
emojis: String,
png_sticker: Option<Vec<u8>>,
tgs_sticker: Option<Vec<u8>>,
webm_sticker: Option<Vec<u8>>,
) -> Result<bool, Box<dyn Error>> {
let url = format!("{}/bot{}/createNewStickerSet", API_BASE, self.token);
let mut form = reqwest::multipart::Form::new();
form = form.text("user_id", user_id.to_string());
form = form.text("name", name);
form = form.text("title", title);
form = form.text("emojis", emojis);
if png_sticker.is_some() {
form = form.part(
"png_sticker",
reqwest::multipart::Part::bytes(png_sticker.unwrap()).file_name(""),
);
}
if tgs_sticker.is_some() {
form = form.part(
"tgs_sticker",
reqwest::multipart::Part::bytes(tgs_sticker.unwrap()),
);
}
if webm_sticker.is_some() {
form = form.part(
"webm_sticker",
reqwest::multipart::Part::bytes(webm_sticker.unwrap()),
);
}
let res = self.client.get(url).multipart(form).send().await?;
let res_json = res.json::<TelegramAPIResponse<bool>>().await?;
if !res_json.ok {
return Err(Box::new(TelegramError::new(
res_json.error_code.unwrap(),
res_json.description.unwrap(),
)));
}
Ok(res_json.result.unwrap())
}
pub async fn upload_sticker( pub async fn upload_sticker(
&self, &self,
user_id: u64, user_id: u64,