Compare commits

...

43 Commits

Author SHA1 Message Date
LordMZTE fc28712e9f
fix libjens URL
continuous-integration/drone/push Build encountered an error Details
2022-02-13 23:31:39 +01:00
LordMZTE 0855af438e cli now shows meme id in list and search commands 2022-01-20 22:43:34 +01:00
LordMZTE 676f5713ed fix upload endpoint 2022-01-09 00:40:44 +01:00
LordMZTE 11a22df7ea bump version 2021-12-26 14:38:56 +01:00
LordMZTE 9a4a5c5578 added rand command and aliases 2021-12-26 14:37:50 +01:00
LordMZTE e2ce737497 update changelog 2021-12-26 14:13:23 +01:00
LordMZTE e5a51be87e improve tables and remove fzf nonsense 2021-12-26 14:12:36 +01:00
LordMZTE 6e3086c460 upgrade to new libjens to work with new api 2021-12-25 21:51:41 +01:00
LordMZTE 506fa02cd4 add spinner
continuous-integration/drone/push Build is passing Details
2021-08-21 22:12:26 +02:00
LordMZTE 92e452c473 move jm_client_core to new libjens repo
continuous-integration/drone/push Build is failing Details
https://tilera.xyz/git/LordMZTE/libjens/
2021-08-08 14:05:21 +02:00
LordMZTE 0d3a772432 upgrade tokio
continuous-integration/drone/push Build is passing Details
2021-06-18 21:52:35 +02:00
LordMZTE a5629ea741 add clear_cache function to JMClient
continuous-integration/drone/push Build is passing Details
2021-06-18 16:20:28 +02:00
LordMZTE 1d685eb772 memes now print out ID
continuous-integration/drone/push Build is passing Details
2021-06-17 12:02:39 +02:00
LordMZTE a4d66b1fbe added errors to upload when tilera breaks the api and makes it return nonsense
continuous-integration/drone/push Build is passing Details
2021-06-07 11:18:51 +02:00
LordMZTE d7c0831c85 use HRTB to get rid of DeserializeOwned trait bound in get_api_json
continuous-integration/drone/push Build is passing Details
2021-06-02 20:27:22 +02:00
LordMZTE df7d39971b client is now not static anymore
continuous-integration/drone/push Build is passing Details
2021-05-27 18:01:30 +02:00
LordMZTE 3c395a34a7 refactors and add timestamp to meme display
continuous-integration/drone/push Build is passing Details
2021-05-12 20:13:06 +02:00
LordMZTE 1463a3cf0d Revert "add windows tests to CI"
continuous-integration/drone/push Build is passing Details
I guess this isn't a good idea...

This reverts commit 831e971914.
2021-04-14 21:21:59 +02:00
LordMZTE 831e971914 add windows tests to CI
continuous-integration/drone/push Build is failing Details
2021-04-14 21:13:15 +02:00
LordMZTE 8078fffc5d update to new API endpoint
continuous-integration/drone/push Build is passing Details
2021-04-14 20:17:47 +02:00
LordMZTE 010f6fef15 added constant for API endpoint for easy updating.
continuous-integration/drone/push Build is passing Details
2021-04-14 20:15:31 +02:00
LordMZTE 929c38bfed update changelog
continuous-integration/drone/push Build is passing Details
2021-04-12 17:29:28 +02:00
LordMZTE 118d4902bd bump version
continuous-integration/drone/push Build is passing Details
2021-04-12 17:06:24 +02:00
LordMZTE 64ef178558 add fzf integration
continuous-integration/drone/push Build is passing Details
2021-04-12 17:04:56 +02:00
LordMZTE 00ed1609ba remove windows drone tests
continuous-integration/drone/push Build is passing Details
2021-04-06 21:34:37 +02:00
LordMZTE 5231aa396c bump version and update changelog
continuous-integration/drone/push Build is failing Details
2021-04-06 21:33:16 +02:00
LordMZTE a8582bf1a3 fix and parallelize ci
continuous-integration/drone/push Build is failing Details
2021-04-06 21:27:37 +02:00
LordMZTE f38ea4d9c1 update CI config with more tests
continuous-integration/drone/push Build is failing Details
2021-04-06 20:55:37 +02:00
LordMZTE b0e97574ec add --firsturl and --cat options to cli
continuous-integration/drone/push Build is passing Details
2021-04-06 20:46:21 +02:00
LordMZTE ad4289546a fix ci attempt 3
continuous-integration/drone/push Build is passing Details
2021-04-01 21:55:49 +02:00
LordMZTE cd7a8c75f9 fix ci attempt 2
continuous-integration/drone/push Build is failing Details
2021-04-01 21:53:46 +02:00
LordMZTE 9af8243cb5 try to fix ci attempt 1
continuous-integration/drone/push Build is failing Details
2021-04-01 21:47:04 +02:00
LordMZTE 172aff4ef4 first gui prototype
continuous-integration/drone/push Build is failing Details
2021-04-01 21:41:34 +02:00
LordMZTE a046dd2118 remove unused cli dependencies
continuous-integration/drone/push Build is failing Details
2021-04-01 19:30:26 +02:00
LordMZTE a3677bcf3c move client core to seperate crate 2021-04-01 19:12:23 +02:00
LordMZTE de2f0a12b5 update tokencracker for new API 2021-03-10 20:43:42 +01:00
LordMZTE 0dc649cb16 adjust debian package names and bump version for next release
continuous-integration/drone/push Build is passing Details
2021-01-05 21:40:21 +01:00
LordMZTE 4e41e10fee bump version
continuous-integration/drone/push Build is passing Details
2021-01-05 20:56:53 +01:00
LordMZTE 336a3c0ecb add deb packages to CD
continuous-integration/drone/push Build is passing Details
2021-01-05 20:50:19 +01:00
LordMZTE e5299cb8ec update cargo.toml files 2021-01-05 20:27:15 +01:00
LordMZTE 10c5cb88db improved error messages
continuous-integration/drone/push Build is passing Details
2021-01-05 20:05:13 +01:00
LordMZTE bb33d3b015 category list improvements and cleanups
continuous-integration/drone/push Build is passing Details
2021-01-05 15:13:19 +01:00
LordMZTE 34bc07c3c4 fix meme response cache to include filters
continuous-integration/drone/push Build is passing Details
2020-12-30 16:01:22 +01:00
21 changed files with 486 additions and 352 deletions

View File

@ -1,10 +1,21 @@
kind: pipeline
name: tests
steps:
- name: test-linux
- name: test-linux-debug
image: rust
depends_on: [ clone ]
commands:
- apt update
- apt install -y libgtk-3-dev
- cargo test -v
- name: test-linux-release
image: rust
depends_on: [ clone ]
commands:
- apt update
- apt install -y libgtk-3-dev
- cargo test -v --release
---
kind: pipeline
name: release
@ -12,6 +23,8 @@ steps:
- name: release-linux
image: rust
commands:
- apt update
- apt install -y libgtk-3-dev
- cargo build --release -v
- name: release-win
@ -19,6 +32,17 @@ steps:
commands:
- cargo build --release --target x86_64-pc-windows-gnu -v
- name: release-debian
image: rust
commands:
- apt update
- apt install -y libgtk-3-dev
# install tool required for building debian packages
- cargo install cargo-deb
- cargo deb -v -p cli -o target/debian/jm.deb
- cargo deb -v -p tokencracker -o target/debian/jmtoken.deb
- cargo deb -v -p gui -o target/debian/jmg.deb
- name: publish
image: plugins/gitea-release
settings:
@ -30,11 +54,17 @@ steps:
files:
- target/release/jm
- target/release/jmtoken
- target/release/jmg
- target/x86_64-pc-windows-gnu/release/jm.exe
- target/x86_64-pc-windows-gnu/release/jmtoken.exe
- target/x86_64-pc-windows-gnu/release/jmg.exe
- target/debian/jm.deb
- target/debian/jmtoken.deb
- target/debian/jmg.deb
when:
event: tag
depends_on:
- release-linux
- release-win
- release-debian

View File

@ -1,4 +1,4 @@
# v0.1.2
# v1.1.2
## cli
- optimized listing by not searching on the client but on the server
- fixed upload endpoint

View File

@ -1,5 +1,6 @@
[workspace]
members = [
"tokencracker",
"cli",
"gui",
"tokencracker",
]

View File

@ -1,8 +1,13 @@
[package]
name = "cli"
version = "0.1.2"
version = "1.1.2"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
license = "GPL-3.0"
description = "Command-Line-Interface for JensMemes"
[package.metadata.deb]
name = "jensmemes-cli"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -11,19 +16,23 @@ name = "jm"
path = "src/main.rs"
[dependencies]
anyhow = "1.0.34"
clap = "2.33.3"
env_logger = "0.8.2"
fuzzy-matcher = "0.3.7"
log = "0.4.11"
once_cell = "1.5.2"
opener = "0.4.1"
reqwest = { version = "0.10.9", features = ["stream"] }
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.60"
structopt = "0.3.21"
term-table = "1.3.0"
term_size = "0.3.2"
tokio = { version = "0.2.23", features = ["macros", "fs", "process"] }
url = "2.2.0"
anyhow = "1.0.52"
chrono = "0.4.19"
clap = "3.0.10"
comfy-table = "5.0.0"
env_logger = "0.9.0"
fuzzy-matcher = "0.3.7"
indicatif = "0.16.2"
libjens = { git = "https://git.tilera.org/LordMZTE/libjens.git", rev = "1.1.0" }
log = "0.4.14"
opener = "0.5.0"
reqwest = { version = "0.11.9", features = ["stream", "multipart"] }
serde_json = "1.0.75"
structopt = "0.3.26"
term_size = "0.3.2"
tokio = { version = "1.15.0", features = ["macros", "fs", "process", "rt-multi-thread"] }
tokio-util = { version = "0.6.9", features = ["codec"] }
url = "2.2.2"
[features]

View File

@ -1,85 +0,0 @@
use anyhow::{anyhow, Result};
use serde::Deserialize;
use term_table::{row::Row, table_cell::TableCell};
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct UpResp {
pub files: Vec<String>,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct CatsResp {
pub categories: Vec<Category>,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct UsersResp {
pub users: Vec<User>,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct MemesResp {
pub memes: Vec<Meme>,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Category {
pub id: String,
pub name: String,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Meme {
pub id: String,
pub link: String,
pub path: String,
pub category: String,
pub user: String,
}
impl Meme {
pub fn file_name(&self) -> Result<&str> {
self.path
.split('/')
.last()
.ok_or_else(|| anyhow!("failed to get file name. server response invalid"))
}
}
impl Into<Row<'_>> for &Meme {
fn into(self) -> Row<'static> {
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
TableCell::new(&self.user),
])
}
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct User {
pub name: String,
pub id: Option<String>,
pub tokenhash: Option<String>,
pub userdir: Option<String>,
pub dayuploads: String,
}
impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
}
}
impl Into<Row<'_>> for &User {
fn into(self) -> Row<'static> {
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
TableCell::new(&self.dayuploads),
])
}
}

17
cli/src/commands/cats.rs Normal file
View File

@ -0,0 +1,17 @@
use crate::table::{list_table, JMTableEntry, TableExt};
use libjens::{api::Category, JMClient};
pub async fn run(client: &JMClient) -> anyhow::Result<()> {
// clone required, because for sorting the immutable reference will not work
let mut cats = client.get_cats().await?.as_ref().clone();
cats.sort_by(|a, b| a.id.cmp(&b.id));
println!(
"{}",
list_table()
.type_header::<Category>()
.add_rows(cats.into_iter().map(JMTableEntry))
);
Ok(())
}

View File

@ -1,10 +1,12 @@
use crate::{
table::{list_table, JMTableEntry, TableExt},
util,
};
use anyhow::Result;
use reqwest::Client;
use crate::util::{self, api, MemeSorting};
use libjens::{api::Meme, util::MemeSorting, JMClient};
pub async fn run(
http: &Client,
client: &JMClient,
cat: Option<String>,
user: Option<String>,
sorting: Option<MemeSorting>,
@ -12,40 +14,39 @@ pub async fn run(
// This needs to be done so both users, memes and categories will be requested
// at once
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::as_ref),
user.as_ref().map(String::as_ref)
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
util::assert_category_exists(client, c).await
} else {
Ok(())
}
},
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
)?;
let mut memes = memes.iter().collect::<Vec<_>>();
let mut memes = memes
.iter()
.filter(|m| cat.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true))
.collect::<Vec<_>>();
if let Some(s) = sorting {
s.sort_with(&mut memes);
}
let mut table = util::list_table();
for m in memes {
table.add_row(m.into());
}
println!("{}", table.render());
println!(
"{}",
list_table()
.type_header::<Meme>()
.add_rows(memes.into_iter().cloned().map(JMTableEntry))
);
Ok(())
}

View File

@ -1,4 +1,6 @@
pub mod cats;
pub mod list;
pub mod rand;
pub mod search;
pub mod up;
pub mod users;

21
cli/src/commands/rand.rs Normal file
View File

@ -0,0 +1,21 @@
use anyhow::Result;
use chrono::{Local, TimeZone};
use libjens::JMClient;
pub async fn run(client: &JMClient) -> Result<()> {
let meme = client.get_random().await?;
println!(
"\
Link: {}
Category: {}
User: {}
Timestamp: {}",
meme.link,
meme.category,
meme.user,
Local.timestamp(meme.timestamp, 0).format("%F %R")
);
Ok(())
}

View File

@ -1,31 +1,33 @@
use anyhow::Result;
use anyhow::{Context, Result};
use log::info;
use reqwest::Client;
use std::io::Write;
use crate::util::{self, api};
use crate::{
table::{list_table, JMTableEntry, TableExt},
util,
};
use libjens::{api::Meme, JMClient};
pub async fn run(
http: &Client,
client: &JMClient,
query: String,
user: Option<String>,
cat: Option<String>,
category: Option<String>,
firsturl: bool,
cat: bool,
) -> Result<()> {
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::as_ref),
user.as_ref().map(String::as_ref),
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
async {
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
if let Some(c) = category.as_ref() {
util::assert_category_exists(client, c).await
} else {
Ok(())
}
@ -36,6 +38,11 @@ pub async fn run(
info!("Starting search with query '{}'", query);
let memes = memes
.iter()
.filter(|m| category.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true));
for meme in memes {
let file_name = meme.file_name()?;
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
@ -45,14 +52,29 @@ pub async fn run(
}
matches.sort_by(|a, b| b.1.cmp(&a.1));
let res = match (firsturl, cat) {
(false, false) => list_table()
.type_header::<Meme>()
.add_rows(matches.into_iter().map(|(m, _)| JMTableEntry(m.clone())))
.to_string()
.into_bytes(),
let mut table = util::list_table();
(true, _) => matches
.first()
.context("No matches found")?
.0
.link
.clone()
.into_bytes(),
(_, true) => {
let url = &matches.first().context("No results found")?.0.link;
client.http.get(url).send().await?.bytes().await?.to_vec()
},
};
for m in matches {
table.add_row(m.0.into());
}
println!("{}", table.render());
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(&res)?;
Ok(())
}

View File

@ -1,41 +1,56 @@
use crate::util;
use anyhow::{bail, Result};
use indicatif::{ProgressBar, ProgressStyle};
use libjens::{api::UpResp, JMClient};
use log::info;
use reqwest::{
multipart::{Form, Part},
Body,
Client,
};
use tokio::{fs::File, io::reader_stream};
use crate::{
api::UpResp,
util::{api, open_link},
};
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
pub async fn run(
http: &Client,
client: &JMClient,
token: String,
path: String,
name: String,
category: String,
open: bool,
) -> Result<()> {
if !api::cats(http).await?.contains(&category) {
bail!(r#"category is invalid. use "cats" to list categories"#);
}
util::assert_category_exists(client, &category).await?;
let res = http
.post("https://data.tilera.xyz/api/jensmemes/upload")
let spinner = ProgressBar::new_spinner();
spinner.enable_steady_tick(50);
spinner.set_style(ProgressStyle::default_spinner().tick_strings(&[
"🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎JM",
"▪▪▪▪▪▪▪▪▪▪",
]));
spinner.set_message("Uploading...");
let res = client
.http
.post("https://api.tilera.xyz/jensmemes/v1/upload")
.multipart(
Form::new()
.text("category", category)
.text("token", token)
.part(
"file",
Part::stream(Body::wrap_stream(reader_stream({
info!("Opening file {}", &path);
File::open(path).await?
})))
Part::stream(Body::wrap_stream(FramedRead::new(
{
info!("Opening file {}", &path);
File::open(path).await?
},
BytesCodec::new(),
)))
.file_name(name),
),
)
@ -43,7 +58,18 @@ pub async fn run(
.await?;
let status = res.status();
let res = serde_json::from_slice::<UpResp>(&res.bytes().await?)?;
// TODO move into JMClient
let bytes = &res.bytes().await?;
spinner.finish_with_message("Done!");
let res = if let Ok(res) = serde_json::from_slice::<UpResp>(bytes) {
res
} else if let Ok(s) = std::str::from_utf8(bytes) {
bail!("Server responded with unexpected response: {}", s);
} else {
bail!("Server responded with invalid utf8 bytes: {:?}", bytes);
};
println!("Server responded with code {}", status);
@ -53,7 +79,7 @@ pub async fn run(
for f in res.files {
if open {
open_link(&f).await?;
util::open_link(&f).await?;
} else {
println!("{}", f);
}

View File

@ -1,16 +1,17 @@
use crate::util::{self, api};
use anyhow::Result;
use reqwest::Client;
use libjens::{api::User, JMClient};
pub async fn run(http: &Client) -> Result<()> {
let users = api::users(http).await?;
let mut table = util::list_table();
use crate::table::{list_table, JMTableEntry, TableExt};
for u in users {
table.add_row(u.into())
}
pub async fn run(client: &JMClient) -> Result<()> {
let users = client.get_users().await?;
println!("{}", table.render());
println!(
"{}",
list_table()
.type_header::<User>()
.add_rows(users.iter().cloned().map(JMTableEntry))
);
Ok(())
}

View File

@ -1,10 +1,9 @@
use crate::util::MemeSorting;
use anyhow::Result;
use reqwest::Client;
use libjens::{util::MemeSorting, JMClient};
use structopt::StructOpt;
mod api;
mod commands;
mod table;
mod util;
#[derive(StructOpt)]
@ -40,10 +39,10 @@ enum Cmd {
open: bool,
},
#[structopt(about = "lists the available categories")]
#[structopt(about = "lists the available categories", alias = "c")]
Cats,
#[structopt(about = "searches for a meme")]
#[structopt(about = "searches for a meme", alias = "s")]
Search {
query: String,
@ -52,9 +51,19 @@ enum Cmd {
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
#[structopt(long, help = "print the URL of the frst result")]
firsturl: bool,
#[structopt(
long,
help = "print the first search result's data to stdout",
conflicts_with = "firsturl"
)]
cat: bool,
},
#[structopt(about = "lists all memes")]
#[structopt(about = "lists all memes", alias = "l")]
List {
#[structopt(long, short, help = "filter by category")]
category: Option<String>,
@ -65,13 +74,16 @@ enum Cmd {
#[structopt(
long,
short,
help = "how to sort the results. can be id, user, category or link"
help = "how to sort the results. can be id, user, category, timestamp or link"
)]
sort: Option<MemeSorting>,
},
#[structopt(about = "Lists all users")]
#[structopt(about = "Lists all users", alias = "u")]
Users,
#[structopt(about = "Gets a random meme", alias = "r")]
Rand,
}
#[tokio::main]
@ -79,7 +91,7 @@ async fn main() -> Result<()> {
env_logger::init();
let Opts { cmd } = Opts::from_args();
let http = Client::new();
let client = JMClient::new();
match cmd {
Cmd::Up {
@ -90,23 +102,23 @@ async fn main() -> Result<()> {
open,
} => {
let name = name.unwrap_or_else(|| file.clone());
commands::up::run(&http, token, file, name, category, open).await?;
commands::up::run(&client, token, file, name, category, open).await?;
},
Cmd::Cats => util::api::cats(&http)
.await?
.iter()
.for_each(|c| println!("{}", c)),
Cmd::Cats => commands::cats::run(&client).await?,
Cmd::Search {
query,
user,
category,
} => commands::search::run(&http, query, user, category).await?,
firsturl,
cat,
} => commands::search::run(&client, query, user, category, firsturl, cat).await?,
Cmd::List {
category,
user,
sort,
} => commands::list::run(&http, category, user, sort).await?,
Cmd::Users => commands::users::run(&http).await?,
} => commands::list::run(&client, category, user, sort).await?,
Cmd::Users => commands::users::run(&client).await?,
Cmd::Rand => commands::rand::run(&client).await?,
}
Ok(())

81
cli/src/table.rs Normal file
View File

@ -0,0 +1,81 @@
use chrono::{Local, TimeZone};
use comfy_table::{presets::UTF8_NO_BORDERS, ContentArrangement, Row, Table};
use libjens::api::{Category, Meme, User};
pub fn list_table() -> Table {
let mut table = Table::new();
table
.load_preset(UTF8_NO_BORDERS)
.set_content_arrangement(ContentArrangement::Dynamic);
table
}
pub trait TableHeader {
fn header() -> Row;
}
macro_rules! impl_table_header {
($t:ident, $e:expr) => {
impl TableHeader for $t {
fn header() -> Row {
$e.into()
}
}
};
}
impl_table_header!(User, vec!["Name", "ID"]);
impl_table_header!(Category, vec!["Name", "ID"]);
impl_table_header!(Meme, vec!["Link", "Category", "User", "Timestamp", "ID"]);
/// a newtype wrapper to convert libjens types to table rows
pub struct JMTableEntry<T>(pub T);
impl From<JMTableEntry<User>> for Row {
fn from(e: JMTableEntry<User>) -> Self {
vec![e.0.name, e.0.id.unwrap_or_else(String::new)].into()
}
}
impl From<JMTableEntry<Category>> for Row {
fn from(e: JMTableEntry<Category>) -> Self {
vec![e.0.name, e.0.id].into()
}
}
impl From<JMTableEntry<Meme>> for Row {
fn from(e: JMTableEntry<Meme>) -> Self {
vec![
e.0.link,
e.0.category,
e.0.user,
Local
.timestamp(e.0.timestamp, 0)
.format("%F %R")
.to_string(),
e.0.id,
]
.into()
}
}
pub trait TableExt {
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self;
fn type_header<T: TableHeader>(&mut self) -> &mut Self;
}
impl TableExt for Table {
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self {
for i in iter.into_iter() {
self.add_row(i);
}
self
}
fn type_header<T: TableHeader>(&mut self) -> &mut Self {
self.set_header(T::header())
}
}

View File

@ -1,93 +1,24 @@
use crate::api::Meme;
use anyhow::bail;
use reqwest::Client;
use std::str::FromStr;
use term_table::{Table, TableBuilder, TableStyle};
use libjens::JMClient;
use tokio::process::Command;
#[macro_export]
macro_rules! init_once_cell {
($cell:ident, $init_fn:expr) => {
match $cell.get() {
Some(x) => x,
None => {
let x = $init_fn;
$cell.get_or_init(|| x)
},
}
};
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
pub async fn assert_category_exists(client: &JMClient, cat: &str) -> anyhow::Result<()> {
if !client.get_cats().await?.iter().any(|c| c.id == cat) {
bail!(NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}
pub mod consts {
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
}
/// ways to communicyte with the JM API
pub mod api {
use crate::api::{CatsResp, Meme, MemesResp, User, UsersResp};
use anyhow::Result;
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Client;
use url::Url;
// cached api responses
static CATS: OnceCell<Vec<String>> = OnceCell::new();
static MEMES: OnceCell<Vec<Meme>> = OnceCell::new();
static USERS: OnceCell<Vec<User>> = OnceCell::new();
pub async fn cats(http: &Client) -> Result<&Vec<String>> {
Ok(init_once_cell!(CATS, {
info!("Requesting categories from server");
let res = http
.get("https://data.tilera.xyz/api/jensmemes/categories")
.send()
.await?;
let cats = serde_json::from_slice::<CatsResp>(&res.bytes().await?)?;
cats.categories.into_iter().map(|c| c.id).collect()
}))
pub async fn assert_user_exists(client: &JMClient, user: &str) -> anyhow::Result<()> {
if !client.get_users().await?.iter().any(|u| u.name == user) {
bail!(NO_SUCH_USER_ERROR);
}
pub async fn memes<'a>(
http: &Client,
cat_filter: Option<&str>,
usr_filter: Option<&str>,
) -> Result<&'a Vec<Meme>> {
Ok(init_once_cell!(MEMES, {
let mut url = Url::options().parse("https://data.tilera.xyz/api/jensmemes/memes")?;
let mut pairs = url.query_pairs_mut();
if let Some(cat) = cat_filter {
pairs.append_pair("category", cat);
}
if let Some(usr) = usr_filter {
pairs.append_pair("user", usr);
}
// drop required in order to move the URL into the request
drop(pairs);
info!("Requesting memes from server");
let res = http.get(url).send().await?;
let memes = serde_json::from_slice::<MemesResp>(&res.bytes().await?)?;
memes.memes
}))
}
pub async fn users(http: &Client) -> Result<&Vec<User>> {
Ok(init_once_cell!(USERS, {
info!("Requesting users from server");
let res = http
.get("https://data.tilera.xyz/api/jensmemes/users")
.send()
.await?;
let users = serde_json::from_slice::<UsersResp>(&res.bytes().await?)?;
users.users
}))
}
Ok(())
}
pub async fn open_link(url: &str) -> anyhow::Result<()> {
@ -100,68 +31,3 @@ pub async fn open_link(url: &str) -> anyhow::Result<()> {
Ok(())
}
/// returns an empty table with the correct format settings for lists
pub fn list_table<'a>() -> Table<'a> {
TableBuilder::new()
.style(TableStyle::simple())
.separate_rows(false)
.has_top_boarder(false)
.has_bottom_boarder(false)
.build()
}
pub enum MemeSorting {
Id,
Link,
Category,
User,
}
impl MemeSorting {
pub fn sort_with(&self, memes: &mut [&Meme]) {
macro_rules! sort {
($list:ident, $field:ident) => {
$list.sort_by(|a, b| {
a.$field
.to_ascii_lowercase()
.cmp(&b.$field.to_ascii_lowercase())
});
};
}
match self {
Self::Id => sort!(memes, id),
Self::Link => sort!(memes, link),
Self::Category => sort!(memes, category),
Self::User => sort!(memes, user),
}
}
}
impl FromStr for MemeSorting {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"id" => Ok(Self::Id),
"link" => Ok(Self::Link),
"category" => Ok(Self::Category),
"user" => Ok(Self::User),
_ => bail!("Invalid Meme sorting! options are id, link, category and user!"),
}
}
}
pub async fn assert_user_exists(http: &Client, user: &str) -> anyhow::Result<()> {
if !api::users(http).await?.iter().any(|u| u.name == user) {
bail!(consts::NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn assert_category_exists(http: &Client, cat: &str) -> anyhow::Result<()> {
if !api::cats(http).await?.iter().any(|c| c == cat) {
bail!(consts::NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}

21
gui/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "gui"
version = "0.1.6"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
[package.metadata.deb]
name = "jensmemes-gui"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "jmg"
path = "src/main.rs"
[dependencies]
anyhow = "1.0.40"
druid = "0.7.0"
libjens = { git = "https://git.tilera.org/LordMZTE/libjens.git", rev = "d4a8b3" }
reqwest = "0.10"
tokio = { version = "0.2.23", features = ["macros"] }

80
gui/src/main.rs Normal file
View File

@ -0,0 +1,80 @@
use std::{cmp::Ordering, sync::Arc};
use crate::util::EqData;
use druid::{
widget::{Flex, Label, List, Scroll},
AppLauncher,
Color,
Data,
Lens,
Widget,
WidgetExt,
WindowDesc,
};
use libjens::{api::Meme, JMClient};
pub(crate) mod util;
const LIST_COLS: &[(&str, f64)] = &[("Link", 1000.), ("User", 50.)];
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = JMClient::new();
let mut memes = client
.get_memes()
.await?
.iter()
.map(|m| EqData(m.clone()))
.collect::<Vec<_>>();
memes.sort_by(|a, b| {
if let Ok((a, b)) =
a.0.file_name()
.and_then(|a| b.0.file_name().map(|b| (a, b)))
{
a.cmp(b)
} else {
Ordering::Equal
}
});
let main_gui = WindowDesc::new(build_root_widget).title("JensMemes GUI");
AppLauncher::with_window(main_gui).launch(State {
memes: Arc::new(memes),
})?;
Ok(())
}
#[derive(Debug, Clone, Data, Lens)]
pub struct State {
pub memes: Arc<Vec<EqData<Meme>>>,
}
fn build_root_widget() -> impl Widget<State> {
Scroll::new(
Flex::column()
.with_child(
Flex::row()
.with_child(Label::new(LIST_COLS[0].0).fix_width(LIST_COLS[0].1))
.with_child(Label::new(LIST_COLS[1].0).fix_width(LIST_COLS[1].1))
.background(Color::grey8(0x33)),
)
.with_child(
List::new(|| {
Flex::row()
.with_child(
Label::dynamic(|d: &EqData<Meme>, _| d.0.link.clone())
.fix_width(LIST_COLS[0].1),
)
.with_child(
Label::dynamic(|d: &EqData<Meme>, _| d.0.user.clone())
.fix_width(LIST_COLS[1].1),
)
})
.lens(State::memes),
),
)
.padding(5.)
}

10
gui/src/util.rs Normal file
View File

@ -0,0 +1,10 @@
use druid::Data;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EqData<T: 'static + PartialEq + Clone>(pub T);
impl<T: 'static + PartialEq + Clone> Data for EqData<T> {
fn same(&self, other: &Self) -> bool {
self == other
}
}

View File

@ -1,8 +1,13 @@
[package]
name = "tokencracker"
version = "0.1.0"
version = "0.1.6"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
license = "GPL-3.0"
description = "Cracks JensMemes Tokens"
[package.metadata.deb]
name = "jensmemes-tokencracker"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -11,10 +16,10 @@ name = "jmtoken"
path = "src/main.rs"
[dependencies]
anyhow = "1.0.34"
clap = "2.33.3"
anyhow = "1.0.52"
clap = "2.34.0"
md5 = "0.7.0"
reqwest = "0.10.8"
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.59"
tokio = { version = "~0.2", features = ["full"] }
reqwest = "0.11.8"
serde = { version = "1.0.132", features = ["derive"] }
serde_json = "1.0.73"
tokio = { version = "1.15.0", features = ["full"] }

View File

@ -1,6 +1,9 @@
use serde::de::{Error, Unexpected, Visitor};
use core::fmt::Formatter;
use serde::{Deserialize, Deserializer};
use serde::{
de::{Error, Unexpected, Visitor},
Deserialize,
Deserializer,
};
use std::convert::TryInto;
#[derive(Deserialize, Debug)]
@ -36,7 +39,8 @@ where
where
E: Error,
{
v.parse().map_err(|_| E::invalid_type(Unexpected::Str(v), &"a u32"))
v.parse()
.map_err(|_| E::invalid_type(Unexpected::Str(v), &"a u32"))
}
/// implementing u64 instead of 32 because it is used as fallback
@ -44,7 +48,8 @@ where
where
E: Error,
{
v.try_into().map_err(|_| E::invalid_type(Unexpected::Unsigned(v), &"a u32"))
v.try_into()
.map_err(|_| E::invalid_type(Unexpected::Unsigned(v), &"a u32"))
}
}

View File

@ -1,7 +1,10 @@
use anyhow::Result;
use clap::{App, Arg};
use reqwest::{Client, Url};
use tokencracker::{api::{JensmemesUser, UserResponse}, hex_string_hash};
use tokencracker::{
api::{JensmemesUser, UserResponse},
hex_string_hash,
};
#[tokio::main]
async fn main() -> Result<()> {
@ -33,8 +36,13 @@ async fn main() -> Result<()> {
let (username, userdir) =
if let (200..=210, Ok(usr)) = (response.status().as_u16(), response.bytes().await) {
let UserResponse { user: JensmemesUser {name, userdir, ..}, .. } = serde_json::from_slice::<UserResponse>(&usr)?;
(name, userdir)
let UserResponse {
user: JensmemesUser {
name, tokenhash, ..
},
..
} = serde_json::from_slice::<UserResponse>(&usr)?;
(name, tokenhash)
} else {
("Not in Database".into(), public.clone())
};
@ -43,7 +51,8 @@ async fn main() -> Result<()> {
"Username: {}
Public Token: {}
Private Token: {}
User: https://data.tilera.xyz/file/jensmemes/images/{}",
User: https://data.tilera.xyz/file/jensmemes/images/{}\
",
username, public, private, userdir
);