feat: MVP

This commit is contained in:
Kristofers Solo 2025-09-19 17:12:27 +03:00
parent da319b84c1
commit a35b255d67
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
4 changed files with 118 additions and 26 deletions

28
Cargo.lock generated
View File

@ -113,6 +113,12 @@ version = "1.23.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3995eaeebcdf32f91f980d360f78732ddc061097ab4e39991ae7a6ace9194677"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.10.1"
@ -129,6 +135,17 @@ dependencies = [
"shlex",
]
[[package]]
name = "cfb"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
dependencies = [
"byteorder",
"fnv",
"uuid",
]
[[package]]
name = "cfg-if"
version = "1.0.3"
@ -814,6 +831,15 @@ dependencies = [
"serde_core",
]
[[package]]
name = "infer"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a588916bfdfd92e71cacef98a63d9b1f0d74d6599980d11894290e7ddefffcf7"
dependencies = [
"cfb",
]
[[package]]
name = "io-uring"
version = "0.7.10"
@ -1665,6 +1691,8 @@ version = "0.1.0"
dependencies = [
"color-eyre",
"dotenv",
"infer",
"once_cell",
"regex",
"teloxide",
"tempfile",

View File

@ -8,6 +8,8 @@ edition = "2024"
[dependencies]
color-eyre = "0.6"
dotenv = "0.15"
infer = "0.19"
once_cell = "1.21.3"
regex = "1.11"
teloxide = { version = "0.17", features = ["macros"] }
tempfile = "3"

View File

@ -7,7 +7,11 @@ use color_eyre::{
};
use dotenv::dotenv;
use regex::Regex;
use std::{fs::read_dir, process::Stdio};
use std::{
fs::{File, read_dir},
io::Read,
path::Path,
};
use teloxide::{
Bot,
prelude::Requester,
@ -18,8 +22,18 @@ use tempfile::tempdir;
use tokio::process::Command;
use tracing::error;
static VIDEO_EXTS: &[&str] = &["mp4", "webm"];
static IMAGE_EXTS: &[&str] = &["jpg", "jpeg", "png"];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MediaKind {
Video,
Image,
Unknown,
}
#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> color_eyre::Result<()> {
dotenv().ok();
color_eyre::install()?;
setup_logger()?;
@ -62,15 +76,17 @@ async fn fetch_and_send(bot: &Bot, chat_id: ChatId, shortcode: &str) -> Result<(
let dir = tempdir().context("create tempdir")?;
let dir_path = dir.path().to_path_buf();
let target = format!("--{}", shortcode);
dbg!(&dir_path);
let target = format!("-{}", shortcode);
dbg!(&target);
let status = Command::new("instaloader")
.arg("--dirname-pattern")
.arg(dir_path.to_string_lossy().as_ref())
.arg("--no-metadate-json")
.arg("--no-metadata-json")
.arg("--no-compress-json")
// .arg("--quiet")
.arg("--quiet")
.arg("--")
.arg(&target)
.stdout(Stdio::null())
.stderr(Stdio::piped())
.status()
.await
.context("runnning instaloader")?;
@ -95,14 +111,69 @@ async fn fetch_and_send(bot: &Bot, chat_id: ChatId, shortcode: &str) -> Result<(
return Err(eyre!("no media found"));
}
let first = &media_files[0];
let input = InputFile::file(first.clone());
dbg!(&media_files);
let ext = first.extension().and_then(|s| s.to_str()).unwrap_or("");
if matches!(ext, "jpg" | "jpeg") {
bot.send_photo(chat_id, input).await?;
} else {
if let Some(video_path) = media_files.iter().find(|p| is_video(p)) {
let input = InputFile::file(video_path.clone());
bot.send_video(chat_id, input).await?;
return Ok(());
}
if let Some(image_path) = media_files.iter().find(|p| is_image(p)) {
let input = InputFile::file(image_path.clone());
bot.send_photo(chat_id, input).await?;
return Ok(());
}
bot.send_message(chat_id, "No supported media found")
.await?;
Ok(())
}
fn ext_lower(path: &Path) -> Option<String> {
path.extension()
.and_then(|s| s.to_str())
.map(|s| s.to_ascii_lowercase())
}
fn kind_by_magic(path: &Path) -> Option<MediaKind> {
let mut f = File::open(path).ok()?;
let mut buf = [0u8; 8192];
let n = f.read(&mut buf).ok()?;
if n == 0 {
return None;
}
if let Some(kind) = infer::get(&buf[..n]) {
let mt = kind.mime_type();
if mt.starts_with("video/") {
return Some(MediaKind::Video);
}
if mt.starts_with("image/") {
return Some(MediaKind::Image);
}
}
None
}
fn detect_media_kind(path: &Path) -> MediaKind {
if let Some(ext) = ext_lower(path) {
if VIDEO_EXTS.iter().any(|e| e.eq_ignore_ascii_case(&ext)) {
return MediaKind::Video;
}
if IMAGE_EXTS.iter().any(|e| e.eq_ignore_ascii_case(&ext)) {
return MediaKind::Image;
}
}
kind_by_magic(path).unwrap_or(MediaKind::Unknown)
}
fn is_video(path: &Path) -> bool {
detect_media_kind(path) == MediaKind::Video
}
fn is_image(path: &Path) -> bool {
detect_media_kind(path) == MediaKind::Image
}

View File

@ -1,23 +1,14 @@
use color_eyre::Result;
use std::{fs::create_dir_all, path::PathBuf};
use tracing_appender::rolling;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
/// # Errors
pub fn setup_logger() -> Result<()> {
let log_dir_path = PathBuf::from(".logs");
create_dir_all(&log_dir_path)?;
let logfile = if cfg!(debug_assertions) {
rolling::daily(log_dir_path, "traxor.log")
} else {
rolling::never(log_dir_path, "traxor.log")
};
let formatter = BunyanFormattingLayer::new("traxor".into(), logfile);
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into());
let formatter = BunyanFormattingLayer::new("tg-relay-rs".into(), std::io::stdout);
tracing_subscriber::registry()
.with(env_filter)
.with(JsonStorageLayer)
.with(formatter)
.init();