Compare commits
11
Commits
e6c2eb76e9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
312f2aec4e | ||
|
|
784c30f9a5 | ||
|
|
31a9fa64e2 | ||
|
|
3d08a04b06 | ||
|
|
d777104332 | ||
|
|
b5d71f3418 | ||
|
|
fe4dac9fd2 | ||
|
|
24e826cd85 | ||
|
|
edd049a839 | ||
|
|
09d811a62a | ||
|
|
5b1442285c |
Generated
+1199
-799
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -14,6 +14,7 @@ resvg = "*"
|
|||||||
oauth1 = "*"
|
oauth1 = "*"
|
||||||
clap = { version = "*", features = ["derive"] }
|
clap = { version = "*", features = ["derive"] }
|
||||||
webbrowser = "*"
|
webbrowser = "*"
|
||||||
|
#webbrowser = { path = "../webbrowser-rs" }
|
||||||
itertools = "*"
|
itertools = "*"
|
||||||
tracing = "0.1.36"
|
tracing = "0.1.36"
|
||||||
tracing-subscriber = "0.3.15"
|
tracing-subscriber = "0.3.15"
|
||||||
@@ -21,9 +22,10 @@ regex = "1.6.0"
|
|||||||
image = "0.24.3"
|
image = "0.24.3"
|
||||||
viuer = "0.6.1"
|
viuer = "0.6.1"
|
||||||
url = { version = "2.3.1", features = ["serde"] }
|
url = { version = "2.3.1", features = ["serde"] }
|
||||||
megalodon = { git = "https://github.com/wlcx/megalodon-rs.git" }
|
megalodon = "*"
|
||||||
tokio = "*"
|
tokio = "*"
|
||||||
futures-util = "*"
|
futures-util = "*"
|
||||||
|
bsky-sdk = "0.1.13"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
toml = "*"
|
toml = "*"
|
||||||
|
|||||||
Generated
+58
-107
@@ -2,15 +2,16 @@
|
|||||||
"nodes": {
|
"nodes": {
|
||||||
"devshell": {
|
"devshell": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": "nixpkgs",
|
"nixpkgs": [
|
||||||
"systems": "systems"
|
"nixpkgs"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1686680692,
|
"lastModified": 1768818222,
|
||||||
"narHash": "sha256-SsLZz3TDleraAiJq4EkmdyewSyiv5g0LZYc6vaLZOMQ=",
|
"narHash": "sha256-460jc0+CZfyaO8+w8JNtlClB2n4ui1RbHfPTLkpwhU8=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "devshell",
|
"repo": "devshell",
|
||||||
"rev": "fd6223370774dd9c33354e87a007004b5fd36442",
|
"rev": "255a2b1725a20d060f566e4755dbf571bbbb5f76",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -19,34 +20,41 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"flake-utils": {
|
"fenix": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"systems": "systems_2"
|
"nixpkgs": [
|
||||||
|
"naersk",
|
||||||
|
"nixpkgs"
|
||||||
|
],
|
||||||
|
"rust-analyzer-src": "rust-analyzer-src"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1681202837,
|
"lastModified": 1752475459,
|
||||||
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
|
"narHash": "sha256-z6QEu4ZFuHiqdOPbYss4/Q8B0BFhacR8ts6jO/F/aOU=",
|
||||||
"owner": "numtide",
|
"owner": "nix-community",
|
||||||
"repo": "flake-utils",
|
"repo": "fenix",
|
||||||
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
|
"rev": "bf0d6f70f4c9a9cf8845f992105652173f4b617f",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "numtide",
|
"owner": "nix-community",
|
||||||
"repo": "flake-utils",
|
"repo": "fenix",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"naersk": {
|
"naersk": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": "nixpkgs_2"
|
"fenix": "fenix",
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1686572087,
|
"lastModified": 1782220280,
|
||||||
"narHash": "sha256-jXTut7ZSYqLEgm/nTk7TuVL2ExahTip605bLINklAnQ=",
|
"narHash": "sha256-thLTFbp9D5Qknmh8q/v4FRpLGphUSijT3E86cbLYTXo=",
|
||||||
"owner": "nix-community",
|
"owner": "nix-community",
|
||||||
"repo": "naersk",
|
"repo": "naersk",
|
||||||
"rev": "8507af04eb40c5520bd35d9ce6f9d2342cea5ad1",
|
"rev": "9aa07bb0256d300219b30622d2454e85f7f3667e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -57,26 +65,10 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1677383253,
|
"lastModified": 1764831616,
|
||||||
"narHash": "sha256-UfpzWfSxkfXHnb4boXZNaKsAcUrZT9Hw+tao1oZxd08=",
|
"narHash": "sha256-OtzF5wBvO0jgW1WW1rQU9cMGx7zuvkF7CAVJ1ypzkxA=",
|
||||||
"owner": "NixOS",
|
"path": "/nix/store/hv1dx0bah4m1mw2asfrg29wl17by9ngd-source",
|
||||||
"repo": "nixpkgs",
|
"rev": "c97c47f2bac4fa59e2cbdeba289686ae615f8ed4",
|
||||||
"rev": "9952d6bc395f5841262b006fbace8dd7e143b634",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "NixOS",
|
|
||||||
"ref": "nixpkgs-unstable",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nixpkgs_2": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1685789966,
|
|
||||||
"narHash": "sha256-pyqctu5Cq1jwymO3Os0/RNj5Nm3q5kmRCT24p7gtG70=",
|
|
||||||
"path": "/nix/store/hnkjxwx9zv2k0gkiznbpkrsvyrzaz6w1-source",
|
|
||||||
"rev": "4eaa9e3eb36386de0c6a268ba5da72cafc959619",
|
|
||||||
"type": "path"
|
"type": "path"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -84,55 +76,44 @@
|
|||||||
"type": "indirect"
|
"type": "indirect"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nixpkgs_3": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1685789966,
|
|
||||||
"narHash": "sha256-pyqctu5Cq1jwymO3Os0/RNj5Nm3q5kmRCT24p7gtG70=",
|
|
||||||
"path": "/nix/store/hnkjxwx9zv2k0gkiznbpkrsvyrzaz6w1-source",
|
|
||||||
"rev": "4eaa9e3eb36386de0c6a268ba5da72cafc959619",
|
|
||||||
"type": "path"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"id": "nixpkgs",
|
|
||||||
"type": "indirect"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nixpkgs_4": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1681358109,
|
|
||||||
"narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=",
|
|
||||||
"owner": "NixOS",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "NixOS",
|
|
||||||
"ref": "nixpkgs-unstable",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"devshell": "devshell",
|
"devshell": "devshell",
|
||||||
"naersk": "naersk",
|
"naersk": "naersk",
|
||||||
"nixpkgs": "nixpkgs_3",
|
"nixpkgs": "nixpkgs",
|
||||||
"rust-overlay": "rust-overlay",
|
"rust-overlay": "rust-overlay",
|
||||||
"utils": "utils"
|
"utils": "utils"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"rust-analyzer-src": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1752428706,
|
||||||
|
"narHash": "sha256-EJcdxw3aXfP8Ex1Nm3s0awyH9egQvB2Gu+QEnJn2Sfg=",
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"rev": "591e3b7624be97e4443ea7b5542c191311aa141d",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"ref": "nightly",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"rust-overlay": {
|
"rust-overlay": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-utils": "flake-utils",
|
"nixpkgs": [
|
||||||
"nixpkgs": "nixpkgs_4"
|
"nixpkgs"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1686795910,
|
"lastModified": 1787834454,
|
||||||
"narHash": "sha256-jDa40qRZ0GRQtP9EMZdf+uCbvzuLnJglTUI2JoHfWDc=",
|
"narHash": "sha256-jXSvqn04IOecJCvSvl8+g3PsbDVNP6qKs6dKUs/b16k=",
|
||||||
"owner": "oxalica",
|
"owner": "oxalica",
|
||||||
"repo": "rust-overlay",
|
"repo": "rust-overlay",
|
||||||
"rev": "5c2b97c0a9bc5217fc3dfb1555aae0fb756d99f9",
|
"rev": "dc2fd1acc537f3583744e1373597a5731ff7a6e3",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -156,46 +137,16 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"systems_2": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1681028828,
|
|
||||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"systems_3": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1681028828,
|
|
||||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"utils": {
|
"utils": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"systems": "systems_3"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1685518550,
|
"lastModified": 1731533236,
|
||||||
"narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=",
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef",
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
@@ -3,9 +3,18 @@
|
|||||||
|
|
||||||
inputs = {
|
inputs = {
|
||||||
utils.url = "github:numtide/flake-utils";
|
utils.url = "github:numtide/flake-utils";
|
||||||
devshell.url = "github:numtide/devshell";
|
devshell = {
|
||||||
naersk.url = "github:nix-community/naersk";
|
url = "github:numtide/devshell";
|
||||||
rust-overlay.url = "github:oxalica/rust-overlay";
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
naersk = {
|
||||||
|
url = "github:nix-community/naersk";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
rust-overlay = {
|
||||||
|
url = "github:oxalica/rust-overlay";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = {
|
outputs = {
|
||||||
@@ -32,7 +41,7 @@
|
|||||||
packig = naersk-lib.buildPackage {
|
packig = naersk-lib.buildPackage {
|
||||||
pname = "iso7010-a-day";
|
pname = "iso7010-a-day";
|
||||||
root = ./.;
|
root = ./.;
|
||||||
buildInputs = [pkgs.openssl pkgs.pkgconfig];
|
buildInputs = [pkgs.openssl pkgs.pkg-config];
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
packages.default = packig;
|
packages.default = packig;
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 17 KiB |
+70
@@ -0,0 +1,70 @@
|
|||||||
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
io::{Cursor, Seek, SeekFrom},
|
||||||
|
};
|
||||||
|
|
||||||
|
use bsky_sdk::{
|
||||||
|
api::{
|
||||||
|
app::bsky::{
|
||||||
|
embed::images::{ImageData, MainData},
|
||||||
|
feed::post::{RecordData, RecordEmbedRefs},
|
||||||
|
},
|
||||||
|
types::string::Datetime,
|
||||||
|
},
|
||||||
|
BskyAgent,
|
||||||
|
};
|
||||||
|
|
||||||
|
const BSKY_IDENT_ENV_VAR: &str = "BSKY_IDENTIFIER";
|
||||||
|
const BSKY_PASSWORD_ENV_VAR: &str = "BSKY_PASSWORD";
|
||||||
|
|
||||||
|
fn getenv(name: &str) -> String {
|
||||||
|
std::env::var(name).expect(&format!("Please specify {} in the env", name))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn post(
|
||||||
|
text: &str,
|
||||||
|
img: Option<Box<Cursor<Vec<u8>>>>,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let agent = BskyAgent::builder().build().await?;
|
||||||
|
agent
|
||||||
|
.login(getenv(BSKY_IDENT_ENV_VAR), getenv(BSKY_PASSWORD_ENV_VAR))
|
||||||
|
.await?;
|
||||||
|
let embed = if let Some(mut i) = img {
|
||||||
|
i.as_mut().seek(SeekFrom::Start(0))?;
|
||||||
|
let out = agent
|
||||||
|
.api
|
||||||
|
.com
|
||||||
|
.atproto
|
||||||
|
.repo
|
||||||
|
.upload_blob(i.into_inner())
|
||||||
|
.await?;
|
||||||
|
let embed = RecordEmbedRefs::AppBskyEmbedImagesMain(Box::new(
|
||||||
|
MainData {
|
||||||
|
images: vec![ImageData {
|
||||||
|
alt: "".into(),
|
||||||
|
aspect_ratio: None,
|
||||||
|
image: out.data.blob,
|
||||||
|
}
|
||||||
|
.into()],
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
));
|
||||||
|
Some(bsky_sdk::api::types::Union::Refs(embed))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
agent
|
||||||
|
.create_record(RecordData {
|
||||||
|
created_at: Datetime::now(),
|
||||||
|
embed,
|
||||||
|
entities: None,
|
||||||
|
facets: None,
|
||||||
|
labels: None,
|
||||||
|
langs: None,
|
||||||
|
reply: None,
|
||||||
|
tags: None,
|
||||||
|
text: text.into(),
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+29
-66
@@ -1,17 +1,16 @@
|
|||||||
use mastodon::authorize_fedi;
|
use mastodon::authorize_fedi;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::io::Cursor;
|
use std::fs::File;
|
||||||
|
use std::io::{BufWriter, Cursor, Write};
|
||||||
use tracing::{event, Level};
|
use tracing::{event, Level};
|
||||||
|
mod bsky;
|
||||||
mod mastodon;
|
mod mastodon;
|
||||||
|
mod svg;
|
||||||
mod twitter;
|
mod twitter;
|
||||||
mod wiki;
|
mod wiki;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use image::{DynamicImage, RgbaImage};
|
|
||||||
use resvg::tiny_skia::{Paint, PathBuilder, Pixmap, PixmapPaint, Stroke};
|
|
||||||
use resvg::usvg::TreeParsing;
|
|
||||||
use resvg::usvg::{Options, Transform};
|
|
||||||
use resvg::Tree;
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use twitter::*;
|
use twitter::*;
|
||||||
use wiki::*;
|
use wiki::*;
|
||||||
@@ -27,63 +26,6 @@ static APP_USER_AGENT: &str = concat!(
|
|||||||
"reqwest",
|
"reqwest",
|
||||||
);
|
);
|
||||||
|
|
||||||
// Render the raw SVG data to an image
|
|
||||||
fn render_svg(data: &[u8], height: u32, with_border: bool) -> StdError<DynamicImage> {
|
|
||||||
let opt = Options::default();
|
|
||||||
let rtree = resvg::usvg::Tree::from_data(data, &opt).expect("couldn't parse");
|
|
||||||
let svg_size = rtree.size;
|
|
||||||
// Work out how wide the pixmap of height `height` needs to be to entirely fit the SVG.
|
|
||||||
let scale_factor = height as f32 / svg_size.height();
|
|
||||||
let pm_width = (scale_factor * svg_size.width()).ceil() as u32;
|
|
||||||
let mut pixmap = Pixmap::new(pm_width, height).ok_or("Error creating pixmap")?;
|
|
||||||
// Render the svg into a pixmap.
|
|
||||||
Tree::from_usvg(&rtree).render(
|
|
||||||
Transform::from_scale(scale_factor, scale_factor),
|
|
||||||
&mut pixmap.as_mut(),
|
|
||||||
);
|
|
||||||
// Make a wider pixmap with a 16:9 AR and the same height. This is a blesséd ratio by twitter
|
|
||||||
// and means we see the whole image nicely in the timeline with no truncation.
|
|
||||||
let mut bigger_pixmap =
|
|
||||||
Pixmap::new(height / 9 * 16, height).ok_or("Error creating bigger pixmap")?;
|
|
||||||
// Then draw our freshly rendered SVG into the middle of the bigger pixmap.
|
|
||||||
bigger_pixmap.draw_pixmap(
|
|
||||||
((bigger_pixmap.width() - pm_width) / 2).try_into().unwrap(),
|
|
||||||
0,
|
|
||||||
pixmap.as_ref(),
|
|
||||||
&PixmapPaint::default(),
|
|
||||||
Transform::identity(),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let (w, h) = (bigger_pixmap.width(), bigger_pixmap.height());
|
|
||||||
// Render a red border for debug purposes
|
|
||||||
if with_border {
|
|
||||||
let mut paint = Paint::default();
|
|
||||||
paint.set_color_rgba8(255, 0, 0, 255);
|
|
||||||
let stroke = Stroke {
|
|
||||||
width: 1.0,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let path = {
|
|
||||||
let mut pb = PathBuilder::new();
|
|
||||||
pb.move_to(0.0, 0.0);
|
|
||||||
pb.line_to(0.0, h as f32 - stroke.width);
|
|
||||||
pb.line_to(w as f32, h as f32 - stroke.width);
|
|
||||||
pb.line_to(w as f32 - stroke.width, 0.0);
|
|
||||||
pb.line_to(0.0, 0.0);
|
|
||||||
pb.finish().unwrap()
|
|
||||||
};
|
|
||||||
bigger_pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
|
|
||||||
}
|
|
||||||
let img = RgbaImage::from_raw(
|
|
||||||
bigger_pixmap.width(),
|
|
||||||
bigger_pixmap.height(),
|
|
||||||
bigger_pixmap.data().to_vec(),
|
|
||||||
)
|
|
||||||
.ok_or("Error creating image from pixmap")?;
|
|
||||||
Ok(DynamicImage::ImageRgba8(img))
|
|
||||||
}
|
|
||||||
|
|
||||||
type StdError<T> = Result<T, Box<dyn std::error::Error>>;
|
type StdError<T> = Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -114,6 +56,8 @@ enum Commands {
|
|||||||
},
|
},
|
||||||
/// Print details about the currently authed user
|
/// Print details about the currently authed user
|
||||||
Whoami,
|
Whoami,
|
||||||
|
/// Download and dump all images to disk
|
||||||
|
Dump,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -128,6 +72,7 @@ async fn main() -> StdError<()> {
|
|||||||
Commands::Whoami => do_whoami().await,
|
Commands::Whoami => do_whoami().await,
|
||||||
Commands::AuthorizeFedi => authorize_fedi().await,
|
Commands::AuthorizeFedi => authorize_fedi().await,
|
||||||
Commands::RunBot { dry_run, target } => run_bot(*dry_run, target.to_owned()).await,
|
Commands::RunBot { dry_run, target } => run_bot(*dry_run, target.to_owned()).await,
|
||||||
|
Commands::Dump => do_dump().await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +182,22 @@ fn get_client(headers: Option<reqwest::header::HeaderMap>) -> StdError<reqwest::
|
|||||||
Ok(c.build()?)
|
Ok(c.build()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn do_dump() -> StdError<()> {
|
||||||
|
let all = scrape_web().await?;
|
||||||
|
let filenames: Vec<_> = all.iter().map(|(_, filename)| filename.as_str()).collect();
|
||||||
|
let metas = get_file_metadata(&filenames).await?;
|
||||||
|
// for m in &metas {
|
||||||
|
// let bytes = client.get(m.url.to_string()).await?.bytes().await?;
|
||||||
|
// }
|
||||||
|
let file = File::create("dump.json")?;
|
||||||
|
let mut writer = BufWriter::new(file);
|
||||||
|
serde_json::to_writer_pretty(&mut writer, &metas)?;
|
||||||
|
writer.flush()?;
|
||||||
|
let count = metas.len();
|
||||||
|
event!(Level::INFO, count, "Dumped records to json file");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn run_bot(dry_run: bool, target: Option<String>) -> StdError<()> {
|
async fn run_bot(dry_run: bool, target: Option<String>) -> StdError<()> {
|
||||||
let all = scrape_web().await?;
|
let all = scrape_web().await?;
|
||||||
let (title, filename) = if let Some(target) = target {
|
let (title, filename) = if let Some(target) = target {
|
||||||
@@ -267,13 +228,15 @@ async fn run_bot(dry_run: bool, target: Option<String>) -> StdError<()> {
|
|||||||
|
|
||||||
if !dry_run {
|
if !dry_run {
|
||||||
// Render the image nice and big for twitter
|
// Render the image nice and big for twitter
|
||||||
let img = render_svg(&svg, 1000, false)?;
|
let img = svg::render_svg(&svg, 1000, false)?;
|
||||||
let mut buf = Cursor::new(Vec::new());
|
let mut buf = Cursor::new(Vec::new());
|
||||||
img.write_to(&mut buf, image::ImageFormat::Png)?;
|
img.write_to(&mut buf, image::ImageFormat::Png)?;
|
||||||
toot(&text, Some(buf.into_inner().into())).await?;
|
let the_img = Some(Box::new(buf));
|
||||||
|
toot(&text, the_img.clone()).await?;
|
||||||
|
bsky::post(&text, the_img).await?;
|
||||||
} else {
|
} else {
|
||||||
// Render the image smaller for output to terminal
|
// Render the image smaller for output to terminal
|
||||||
let img = render_svg(&svg, 128, true)?;
|
let img = svg::render_svg(&svg, 128, true)?;
|
||||||
println!("Dry run - would tweet:\n \"{}\"", text);
|
println!("Dry run - would tweet:\n \"{}\"", text);
|
||||||
viuer::print(
|
viuer::print(
|
||||||
&img,
|
&img,
|
||||||
|
|||||||
+5
-9
@@ -1,18 +1,17 @@
|
|||||||
// Interface to mastodon (etc) instances
|
// Interface to mastodon (etc) instances
|
||||||
|
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use megalodon::entities::UploadMedia;
|
use megalodon::entities::UploadMedia;
|
||||||
use megalodon::generator;
|
use megalodon::generator;
|
||||||
use megalodon::megalodon::PostStatusInputOptions;
|
use megalodon::megalodon::PostStatusInputOptions;
|
||||||
use megalodon::{self, megalodon::UploadMediaInputOptions};
|
use megalodon::{self, megalodon::UploadMediaInputOptions};
|
||||||
|
use std::io::{Cursor, Seek, SeekFrom};
|
||||||
|
|
||||||
use crate::{StdError, APP_USER_AGENT};
|
use crate::{StdError, APP_USER_AGENT};
|
||||||
|
|
||||||
const FEDI_ACCESS_TOKEN_ENV_VAR: &str = "FEDI_ACCESS_TOKEN";
|
const FEDI_ACCESS_TOKEN_ENV_VAR: &str = "FEDI_ACCESS_TOKEN";
|
||||||
const FEDI_INSTANCE_ENV_VAR: &str = "FEDI_INSTANCE";
|
const FEDI_INSTANCE_ENV_VAR: &str = "FEDI_INSTANCE";
|
||||||
|
|
||||||
pub async fn toot(text: &str, img: Option<Cow<'static, [u8]>>) -> StdError<()> {
|
pub async fn toot(text: &str, img: Option<Box<Cursor<Vec<u8>>>>) -> StdError<()> {
|
||||||
let client = megalodon::generator(
|
let client = megalodon::generator(
|
||||||
megalodon::SNS::Pleroma,
|
megalodon::SNS::Pleroma,
|
||||||
std::env::var(FEDI_INSTANCE_ENV_VAR)
|
std::env::var(FEDI_INSTANCE_ENV_VAR)
|
||||||
@@ -26,13 +25,10 @@ pub async fn toot(text: &str, img: Option<Cow<'static, [u8]>>) -> StdError<()> {
|
|||||||
Some(APP_USER_AGENT.into()),
|
Some(APP_USER_AGENT.into()),
|
||||||
);
|
);
|
||||||
let mut ops = PostStatusInputOptions::default();
|
let mut ops = PostStatusInputOptions::default();
|
||||||
if let Some(img) = img {
|
if let Some(mut img) = img {
|
||||||
|
img.as_mut().seek(SeekFrom::Start(0))?;
|
||||||
let media = client
|
let media = client
|
||||||
.upload_media_bytes(
|
.upload_media_reader(img, Some(&UploadMediaInputOptions::default()))
|
||||||
// TODO: get better at lifetimes
|
|
||||||
Box::leak(Box::new(img)),
|
|
||||||
Some(&UploadMediaInputOptions::default()),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
ops.media_ids = Some(vec![match media.json {
|
ops.media_ids = Some(vec![match media.json {
|
||||||
UploadMedia::Attachment(a) => a.id,
|
UploadMedia::Attachment(a) => a.id,
|
||||||
|
|||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
use image::{DynamicImage, RgbaImage};
|
||||||
|
use resvg::tiny_skia::{Paint, PathBuilder, Pixmap, PixmapPaint, Stroke};
|
||||||
|
use resvg::usvg::{fontdb, Options, PostProcessingSteps, Transform, TreeParsing, TreePostProc};
|
||||||
|
|
||||||
|
use crate::StdError;
|
||||||
|
|
||||||
|
// Render the raw SVG data to an image
|
||||||
|
pub fn render_svg(data: &[u8], height: u32, with_border: bool) -> StdError<DynamicImage> {
|
||||||
|
let opt = Options::default();
|
||||||
|
let mut rtree = resvg::usvg::Tree::from_data(data, &opt).expect("couldn't parse");
|
||||||
|
rtree.postprocess(PostProcessingSteps::default(), &fontdb::Database::new());
|
||||||
|
let svg_size = rtree.size;
|
||||||
|
// Work out how wide the pixmap of height `height` needs to be to entirely fit the SVG.
|
||||||
|
let scale_factor = height as f32 / svg_size.height();
|
||||||
|
let pm_width = (scale_factor * svg_size.width()).ceil() as u32;
|
||||||
|
let mut pixmap = Pixmap::new(pm_width, height).ok_or("Error creating pixmap")?;
|
||||||
|
// Render the svg into a pixmap.
|
||||||
|
resvg::render(
|
||||||
|
&rtree,
|
||||||
|
Transform::from_scale(scale_factor, scale_factor),
|
||||||
|
&mut pixmap.as_mut(),
|
||||||
|
);
|
||||||
|
// Make a wider pixmap with a 16:9 AR and the same height. This is a blesséd ratio by twitter
|
||||||
|
// and means we see the whole image nicely in the timeline with no truncation.
|
||||||
|
let mut bigger_pixmap =
|
||||||
|
Pixmap::new(height / 9 * 16, height).ok_or("Error creating bigger pixmap")?;
|
||||||
|
// Then draw our freshly rendered SVG into the middle of the bigger pixmap.
|
||||||
|
bigger_pixmap.draw_pixmap(
|
||||||
|
((bigger_pixmap.width() - pm_width) / 2).try_into().unwrap(),
|
||||||
|
0,
|
||||||
|
pixmap.as_ref(),
|
||||||
|
&PixmapPaint::default(),
|
||||||
|
Transform::identity(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
let (w, h) = (bigger_pixmap.width(), bigger_pixmap.height());
|
||||||
|
// Render a red border for debug purposes
|
||||||
|
if with_border {
|
||||||
|
let mut paint = Paint::default();
|
||||||
|
paint.set_color_rgba8(255, 0, 0, 255);
|
||||||
|
let stroke = Stroke {
|
||||||
|
width: 1.0,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let path = {
|
||||||
|
let mut pb = PathBuilder::new();
|
||||||
|
pb.move_to(0.0, 0.0);
|
||||||
|
pb.line_to(0.0, h as f32 - stroke.width);
|
||||||
|
pb.line_to(w as f32, h as f32 - stroke.width);
|
||||||
|
pb.line_to(w as f32 - stroke.width, 0.0);
|
||||||
|
pb.line_to(0.0, 0.0);
|
||||||
|
pb.finish().unwrap()
|
||||||
|
};
|
||||||
|
bigger_pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
|
||||||
|
}
|
||||||
|
let img = RgbaImage::from_raw(
|
||||||
|
bigger_pixmap.width(),
|
||||||
|
bigger_pixmap.height(),
|
||||||
|
bigger_pixmap.data().to_vec(),
|
||||||
|
)
|
||||||
|
.ok_or("Error creating image from pixmap")?;
|
||||||
|
Ok(DynamicImage::ImageRgba8(img))
|
||||||
|
}
|
||||||
+5
-4
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use crate::{get_client, StdError};
|
use crate::{get_client, StdError};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use tracing::{event, instrument, Level};
|
use tracing::{event, instrument, Level};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
@@ -19,8 +19,9 @@ pub async fn scrape_web() -> StdError<Vec<(String, String)>> {
|
|||||||
// Parse CSS selectors to scrape elements
|
// Parse CSS selectors to scrape elements
|
||||||
let gallerybox_sel =
|
let gallerybox_sel =
|
||||||
scraper::Selector::parse("li.gallerybox").map_err(|e| format!("{:?}", e))?;
|
scraper::Selector::parse("li.gallerybox").map_err(|e| format!("{:?}", e))?;
|
||||||
let link_sel = scraper::Selector::parse("a.image").map_err(|e| format!("{:?}", e))?;
|
let link_sel =
|
||||||
let title_sel = scraper::Selector::parse(".gallerytext p").map_err(|e| format!("{:?}", e))?;
|
scraper::Selector::parse("a.mw-file-description").map_err(|e| format!("{:?}", e))?;
|
||||||
|
let title_sel = scraper::Selector::parse(".gallerytext").map_err(|e| format!("{:?}", e))?;
|
||||||
|
|
||||||
// Fetch stuff!
|
// Fetch stuff!
|
||||||
let client = get_client(None)?;
|
let client = get_client(None)?;
|
||||||
@@ -94,7 +95,7 @@ pub async fn get_files_in_category(category: &str) -> StdError<Vec<String>> {
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct FileMeta {
|
pub struct FileMeta {
|
||||||
pub url: url::Url,
|
pub url: url::Url,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user