Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-05-09 03:54:25 +05:30
parent d170ae0e1a
commit 0113466c87
12 changed files with 2845 additions and 329 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
Cargo.nix linguist-generated

5
.gitignore vendored
View File

@@ -1,2 +1,7 @@
target/ target/
result result
\#*\#
.\#*
.*~*~
*~
result-bin

588
Cargo.lock generated

File diff suppressed because it is too large Load Diff

2239
Cargo.nix generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
cargo-features = ["edition2021"]
[package] [package]
name = "singh4" name = "singh4"

73
ci/pipeline.yml Normal file
View File

@@ -0,0 +1,73 @@
resource_types:
- name: nomad
type: registry-image
source:
repository: natto17/concourse-nomad-resource
tag: latest
resources:
- name: nomad-job
type: nomad
source:
url: https://nomad.weirdnatto.in
name: singh4
token: ((nomad.token))
consul_token: ((nomad.consul))
vault_token: ((nomad.vault))
- name: repo
type: git
source:
uri: https://git.weirdnatto.in/natto1784/singh4.git
branch: master
- name: nix
type: registry-image
source:
repository: nixos/nix
tag: latest-arm64
jobs:
- name: configure-self
public: true
plan:
- get: repo
trigger: true
- set_pipeline: self
file: repo/ci/pipeline.yml
- name: singh4
plan:
- get: repo
trigger: true
passed: [configure-self]
- get: nix
trigger: false
- task: build
image: nix
config:
params:
CACHIX_NAME: ((cachix.name))
CACHIX_AUTH_TOKEN: ((cachix.token))
inputs:
- name: repo
outputs:
- name: storepath
platform: linux
run:
path: sh
args:
- -c
- |
nix-env -iA nixpkgs.cachix
cachix use $CACHIX_NAME
cachix watch-exec $CACHIX_NAME nix -- --extra-experimental-features "nix-command flakes" build ./repo
readlink -n result-bin > storepath/value
- put: nomad-job
params:
job_path: repo/singh4.nomad
templating: true
restart: true
vars:
binary: singh4
var_files:
storepath: storepath/value

120
flake.lock generated
View File

@@ -1,12 +1,47 @@
{ {
"nodes": { "nodes": {
"cargo2nix": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
},
"locked": {
"lastModified": 1648397583,
"narHash": "sha256-g1Ej9APkfHjGd9ExBDvpmyAvsKcfMOsNH6p95xc8E/Y=",
"owner": "cargo2nix",
"repo": "cargo2nix",
"rev": "4362ae00fe824d120e94dd5d6f6e63969dc3d264",
"type": "github"
},
"original": {
"owner": "cargo2nix",
"repo": "cargo2nix",
"type": "github"
}
},
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1614513358, "lastModified": 1638122382,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=", "narHash": "sha256-sQzZzAbvKEqN9s0bzWuYmRaA03v40gaJ4+iL1LXjaeI=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3", "rev": "74f7e4319258e287b0f9cb95426c9853b282730b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"locked": {
"lastModified": 1637014545,
"narHash": "sha256-26IZAc5yzlD9FlDT54io1oqG/bBoyka+FJk5guaX4x4=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "bba5dcc8e0b20ab664967ad83d24d64cb64ec4f4",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -17,11 +52,27 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1622966049, "lastModified": 1638109994,
"narHash": "sha256-6g+28v94ISkVk9TBSsITVOnB2slK8plieWPIF2jo/l0=", "narHash": "sha256-OpA37PTiPMIqoRJbufbl5rOLII7HeeGcA0yl7FoyCIE=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "fbfb79400a08bf754e32b4d4fc3f7d8f8055cf94", "rev": "a284564b7f75ac4db73607db02076e8da9d42c9d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "release-21.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1651726670,
"narHash": "sha256-dSGdzB49SEvdOJvrQWfQYkAefewXraHIV08Vz6iDXWQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c777cdf5c564015d5f63b09cc93bef4178b19b01",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -31,39 +82,66 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs_2": { "nixpkgs_3": {
"locked": { "locked": {
"lastModified": 1617325113, "lastModified": 1637453606,
"narHash": "sha256-GksR0nvGxfZ79T91UUtWjjccxazv6Yh/MvEJ82v1Xmw=", "narHash": "sha256-Gy6cwUswft9xqsjWxFYEnx/63/qzaFUwatcbV5GF/GQ=",
"owner": "nixos", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "54c1e44240d8a527a8f4892608c4bce5440c3ecb", "rev": "8afc4e543663ca0a6a4f496262cd05233737e732",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "cargo2nix": "cargo2nix",
"rust-overlay": "rust-overlay", "nixpkgs": "nixpkgs_2",
"rust-overlay": "rust-overlay_2",
"utils": "utils" "utils": "utils"
} }
}, },
"rust-overlay": { "rust-overlay": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": [
"nixpkgs": "nixpkgs_2" "cargo2nix",
"flake-utils"
],
"nixpkgs": [
"cargo2nix",
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1623034161, "lastModified": 1638152159,
"narHash": "sha256-cbw9X+nVFcpIuBga0hkbtzXbW2fyDWBon6oUN/uQmu0=", "narHash": "sha256-Q0UHsm36cCxk16I/bF1rHJHxjIflESKk2ej76P39j90=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "0b952cdfa37f8b0fc70fc75fbd4605227cd0b272", "rev": "d9a664513558376595e838b21348cdac0ba3115e",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"rust-overlay_2": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1651977622,
"narHash": "sha256-M3vm6mz+ypilmw3hkA6/GyDHH7ID85jWqeE9rtPWe8k=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "c66fab76467a5df683ef50724a7bde54c315f967",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -74,11 +152,11 @@
}, },
"utils": { "utils": {
"locked": { "locked": {
"lastModified": 1622445595, "lastModified": 1649676176,
"narHash": "sha256-m+JRe6Wc5OZ/mKw2bB3+Tl0ZbtyxxxfnAWln8Q5qs+Y=", "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "7d706970d94bc5559077eb1a6600afddcd25a7c8", "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@@ -5,54 +5,55 @@
nixpkgs.url = github:nixos/nixpkgs/nixos-unstable; nixpkgs.url = github:nixos/nixpkgs/nixos-unstable;
utils.url = github:numtide/flake-utils; utils.url = github:numtide/flake-utils;
rust-overlay.url = github:oxalica/rust-overlay; rust-overlay.url = github:oxalica/rust-overlay;
cargo2nix.url = github:cargo2nix/cargo2nix;
}; };
outputs = { self, nixpkgs, utils, rust-overlay }: outputs = { self, nixpkgs, utils, rust-overlay, cargo2nix }:
utils.lib.eachDefaultSystem utils.lib.eachDefaultSystem
(system: (system:
let let
overlays = [ (import rust-overlay) ]; overlays =
[
(import "${cargo2nix}/overlay")
rust-overlay.overlay
];
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system overlays; inherit system overlays;
}; };
rustPkgs = pkgs.rustBuilder.makePackageSet' {
rustChannel = "latest";
packageFun = import ./Cargo.nix;
packageOverrides = pkgs: pkgs.rustBuilder.overrides.all ++ [
(pkgs.rustBuilder.rustLib.makeOverride {
name = "singh4";
overrideAttrs = drv: {
propagatedNativeBuildInputs = drv.propagatedNativeBuildInputs or [ ] ++ [
pkgs.openssl
pkgs.archiver
pkgs.pkg-config
];
};
})
];
};
in in
rec { rec {
devShells = with pkgs; { devShell = with pkgs; mkShell {
default = mkShell buildInputs = [
{
buildInputs = with gst_all_1; [
rust-bin.nightly.latest.default
rust-analyzer
ffmpeg
];
};
bare = mkShell
{
buildInputs = [
rust-bin.nightly.latest.default
];
};
withLSP = mkShell
{
buildInputs = [
rust-bin.nightly.latest.default
rust-analyzer
];
};
};
devShell = devShells.default;
defaultPackage = pkgs.rustPlatform.buildRustPackage rec {
pname = "singh4";
version = "0.1.0";
src = ./.;
nativeBuildInputs = with pkgs; [
rust-bin.nightly.latest.default rust-bin.nightly.latest.default
rust-analyzer
postgresql
]; ];
buildInputs = with pkgs; [
ffmpeg
];
cargoSha256 = "sha256-0rzz+xOQnZYwygReKobNvUAOMeRrFZj7FUVSkAObm7I=";
}; };
packages = {
default = (rustPkgs.workspace.singh4 { }).bin;
};
defaultPackage = packages.default;
} }
); );
} }

44
singh4.nomad Normal file
View File

@@ -0,0 +1,44 @@
job "singh4" {
region = "global"
datacenters = ["nazrin"]
type = "service"
group "svc" {
count = 1
network {
mode = "bridge"
}
vault {
policies = ["singh4-policy"]
}
task "bot" {
driver = "raw_exec"
config {
command = "/bin/sh"
args = [ "-c", <<EOF
nix-store --realise {{+.storepath+}}
nix shell nixpkgs#ffmpeg
{{+.storepath+}}/bin/{{+.binary+}}
EOF
]
}
template {
data = <<EOF
{{with secret "kv/data/singh4/discord"}}
DISCORD_TOKEN="{{.Data.data.token}}"
{{end}}
RUST_BACKTRACE=full
PATH=/run/current-system/sw/bin/
EOF
destination = "${NOMAD_SECRETS_DIR}/data.env"
env = true
}
}
}
}

View File

@@ -1,4 +1,4 @@
use crate::lib::ffmpeg::FfmpegTranscode; use crate::lib::{ffmpeg::FfmpegTranscode, messages::ExtractInfo};
use serenity::{ use serenity::{
framework::standard::{macros::command, Args, CommandResult}, framework::standard::{macros::command, Args, CommandResult},
http::AttachmentType, http::AttachmentType,
@@ -13,11 +13,11 @@ pub const IMAGE_EXTS: [&'static str; 6] = ["png", "gif", "bmp", "webp", "jpg", "
#[command] #[command]
#[aliases("mux")] #[aliases("mux")]
pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult { pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let q: Vec<&str> = args.raw().collect::<Vec<&str>>(); let q = msg.extract_urls().urls;
if q.len() < 2 || q.len() > 4 { if q.len() < 2 {
msg.reply( msg.reply(
ctx, ctx,
"Please use the proper syntax: `xxremux <audio> <video> [audio_codec] [video_codec]`", "Please use the proper syntax: `xxremux <audio> <video> [audio_codec] [video_codec]`"
) )
.await?; .await?;
return Ok(()); return Ok(());
@@ -25,13 +25,13 @@ pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut trans = FfmpegTranscode::default(); let mut trans = FfmpegTranscode::default();
trans.add_input(q[0]); trans.add_input(&q[0]);
trans.set_acodec("copy"); trans.set_acodec("copy");
let mut ext = Path::new(&q[1]).extension().unwrap().to_str().unwrap(); let mut ext = Path::new(&q[1]).extension().unwrap().to_str().unwrap();
if IMAGE_EXTS.contains(&ext) { if IMAGE_EXTS.contains(&ext) {
let resp = reqwest::get(q[1]).await?; let resp = reqwest::get(&q[1]).await?;
let p = format!("/tmp/{}{}-image.{}", "singh4-", msg.id, ext); let p = format!("/tmp/{}{}-image.{}", "singh4-", msg.id, ext);
let mut f = File::create(&p).await?; let mut f = File::create(&p).await?;
tokio::io::copy(&mut resp.bytes().await?.as_ref(), &mut f).await?; tokio::io::copy(&mut resp.bytes().await?.as_ref(), &mut f).await?;
@@ -44,19 +44,20 @@ pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
.add_input(&p) .add_input(&p)
.set_vcodec("h264") .set_vcodec("h264")
.add_arg("vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2") //to correct the dimensions .add_arg("vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2") //to correct the dimensions
.add_arg("tune", "stillimage"); .add_arg("tune", "stillimage")
.add_arg("pix_fmt", "yuv420p");
ext = "mp4"; ext = "mp4";
} else { } else {
trans.add_input(q[1]).set_vcodec("copy"); trans.add_input(&q[1]).set_vcodec("copy");
} }
if q.len() > 2 { /* if q.len() > 2 {
trans.set_acodec(q[2]); trans.set_acodec(q[2]);
} }
if q.len() > 3 { if q.len() > 3 {
trans.set_vcodec(q[3]); trans.set_vcodec(q[3]);
} }*/
let output = format!("/tmp/{}{}.{}", "singh4-", msg.id, ext); let output = format!("/tmp/{}{}.{}", "singh4-", msg.id, ext);

View File

@@ -3,11 +3,11 @@ use serenity::model::channel::Message;
#[derive(Default)] #[derive(Default)]
pub struct URLExtractInfo { pub struct URLExtractInfo {
urls: Vec<String>, pub urls: Vec<String>,
n_attachments: u8, pub n_attachments: u8,
n_links: u16, pub n_links: u16,
rn_attachments: Option<u8>, pub rn_attachments: Option<u8>,
rn_links: Option<u16>, pub rn_links: Option<u16>,
} }
pub trait ExtractInfo { pub trait ExtractInfo {

View File

@@ -1 +1,2 @@
pub mod ffmpeg; pub mod ffmpeg;
pub mod messages;