mirror of
https://github.com/kristoferssolo/tg-relay-rs.git
synced 2025-12-20 11:04:41 +00:00
Initial commit
This commit is contained in:
parent
5f453fa056
commit
da319b84c1
25
.gitignore
vendored
25
.gitignore
vendored
@ -1 +1,24 @@
|
||||
/target
|
||||
#--------------------------------------------------#
|
||||
# The following was generated with gitignore.nvim: #
|
||||
#--------------------------------------------------#
|
||||
# Gitignore for the following technologies: Rust
|
||||
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
# Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# Log dir
|
||||
.logs/
|
||||
|
||||
.env
|
||||
|
||||
2454
Cargo.lock
generated
Normal file
2454
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
19
Cargo.toml
@ -1,6 +1,25 @@
|
||||
[package]
|
||||
name = "tg-relay-rs"
|
||||
version = "0.1.0"
|
||||
authors = ["Kristofers Solo <dev@kristofers.xyz>"]
|
||||
license = "MIT"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
color-eyre = "0.6"
|
||||
dotenv = "0.15"
|
||||
regex = "1.11"
|
||||
teloxide = { version = "0.17", features = ["macros"] }
|
||||
tempfile = "3"
|
||||
thiserror = "2.0"
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "process"] }
|
||||
tracing = "0.1"
|
||||
tracing-appender = "0.2"
|
||||
tracing-bunyan-formatter = { version = "0.3", default-features = false }
|
||||
tracing-log = "0.2.0"
|
||||
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = "warn"
|
||||
nursery = "warn"
|
||||
unwrap_used = "warn"
|
||||
|
||||
109
src/main.rs
109
src/main.rs
@ -1,3 +1,108 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod telemetry;
|
||||
|
||||
use crate::telemetry::setup_logger;
|
||||
use color_eyre::{
|
||||
Result,
|
||||
eyre::{Context, eyre},
|
||||
};
|
||||
use dotenv::dotenv;
|
||||
use regex::Regex;
|
||||
use std::{fs::read_dir, process::Stdio};
|
||||
use teloxide::{
|
||||
Bot,
|
||||
prelude::Requester,
|
||||
respond,
|
||||
types::{ChatId, InputFile, Message},
|
||||
};
|
||||
use tempfile::tempdir;
|
||||
use tokio::process::Command;
|
||||
use tracing::error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
dotenv().ok();
|
||||
color_eyre::install()?;
|
||||
setup_logger()?;
|
||||
|
||||
let bot = Bot::from_env();
|
||||
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
if let Some(text) = msg.text() {
|
||||
if let Some(shortcode) = extract_instagram_shortcode(text) {
|
||||
let bot_cloned = bot.clone();
|
||||
let chat = msg.chat.id;
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = fetch_and_send(&bot_cloned, chat, &shortcode).await {
|
||||
error!("error fetching/sending: {:?}", e);
|
||||
let _ = bot_cloned
|
||||
.send_message(chat, "Failed to fetch Instagram media.")
|
||||
.await;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
respond(())
|
||||
})
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_instagram_shortcode(text: &str) -> Option<String> {
|
||||
let re = Regex::new(
|
||||
r"https?://(?:www\.)?(?:instagram\.com|instagr\.am)/(?:p|reel|tv)/([A-Za-z0-9_-]+)",
|
||||
)
|
||||
.unwrap();
|
||||
re.captures(text)
|
||||
.and_then(|cap| cap.get(1).map(|m| m.as_str().to_string()))
|
||||
}
|
||||
|
||||
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);
|
||||
let status = Command::new("instaloader")
|
||||
.arg(dir_path.to_string_lossy().as_ref())
|
||||
.arg("--no-metadate-json")
|
||||
.arg("--no-compress-json")
|
||||
// .arg("--quiet")
|
||||
.arg(&target)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::piped())
|
||||
.status()
|
||||
.await
|
||||
.context("runnning instaloader")?;
|
||||
|
||||
if !status.success() {
|
||||
error!("instaloader exit: {:?}", status);
|
||||
return Err(eyre!("instaloader failed"));
|
||||
}
|
||||
let mut media_files = Vec::new();
|
||||
|
||||
for entry in read_dir(&dir_path)? {
|
||||
let p = entry?.path();
|
||||
if p.is_file() {
|
||||
let ext = p.extension().and_then(|s| s.to_str()).unwrap_or("");
|
||||
if matches!(ext, "jpg" | "jpeg" | "mp4" | "webm") {
|
||||
media_files.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if media_files.is_empty() {
|
||||
return Err(eyre!("no media found"));
|
||||
}
|
||||
|
||||
let first = &media_files[0];
|
||||
let input = InputFile::file(first.clone());
|
||||
|
||||
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 {
|
||||
bot.send_video(chat_id, input).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
26
src/telemetry.rs
Normal file
26
src/telemetry.rs
Normal file
@ -0,0 +1,26 @@
|
||||
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};
|
||||
|
||||
/// # 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);
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(JsonStorageLayer)
|
||||
.with(formatter)
|
||||
.init();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user