Compare commits
2 commits
72fc6cb9f8
...
51f7696909
Author | SHA1 | Date | |
---|---|---|---|
|
51f7696909 | ||
|
23d6d6a2f6 |
3 changed files with 25 additions and 19 deletions
14
Cargo.lock
generated
14
Cargo.lock
generated
|
@ -918,9 +918,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.7"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f"
|
||||
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
@ -1115,18 +1115,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.215"
|
||||
version = "1.0.216"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
|
||||
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.215"
|
||||
version = "1.0.216"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
|
||||
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1229,7 +1229,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "sticker-usage-analyzer"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"maud",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "sticker-usage-analyzer"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -3,6 +3,7 @@ use rocket::{Rocket,Build,launch,get,routes,fs::{FileServer}};
|
|||
use std::{fs::OpenOptions, io::BufReader, collections::HashMap};
|
||||
use chrono::prelude::*;
|
||||
|
||||
const TELEGRAM_TIMESTAMP_FORMAT: &str = "%FT%X";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
|
@ -18,17 +19,19 @@ enum MediaType {
|
|||
#[derive(Debug, Clone)]
|
||||
struct ScoredChatMessage {
|
||||
pub chat_message: ChatMessage,
|
||||
pub last_used: i64,
|
||||
pub last_used: NaiveDateTime,
|
||||
pub times: u32
|
||||
}
|
||||
|
||||
impl From<ChatMessage> for ScoredChatMessage {
|
||||
fn from(item: ChatMessage) -> ScoredChatMessage {
|
||||
ScoredChatMessage {
|
||||
chat_message: item.clone(),
|
||||
last_used: item.date_unixtime.parse().expect("Could not parse date_unixtime"),
|
||||
impl TryFrom<ChatMessage> for ScoredChatMessage {
|
||||
type Error = chrono::format::ParseError;
|
||||
|
||||
fn try_from(value: ChatMessage) -> Result<Self, Self::Error> {
|
||||
Ok(ScoredChatMessage {
|
||||
chat_message: value.clone(),
|
||||
last_used: NaiveDateTime::parse_from_str(&value.date, TELEGRAM_TIMESTAMP_FORMAT)?,
|
||||
times: 1
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +40,7 @@ impl From<ScoredChatMessage> for Markup {
|
|||
fn from(item: ScoredChatMessage) -> Markup {
|
||||
if let Some(mime_type) = &item.chat_message.mime_type {
|
||||
let message_file = item.chat_message.file.expect("No sticker file");
|
||||
let message_date = DateTime::from_timestamp(item.last_used, 0).expect("Could not parse items");
|
||||
let message_date = item.last_used;
|
||||
match mime_type.as_str() {
|
||||
"image/webp" => {
|
||||
return html! {
|
||||
|
@ -84,7 +87,8 @@ struct ChatMessage {
|
|||
pub media_type: Option<MediaType>,
|
||||
pub mime_type: Option<String>,
|
||||
pub file: Option<String>,
|
||||
pub date_unixtime: String
|
||||
pub date_unixtime: String,
|
||||
pub date: String
|
||||
}
|
||||
|
||||
|
||||
|
@ -113,8 +117,10 @@ fn index() -> Markup {
|
|||
.filter(|m| matches!(m.media_type, Some(MediaType::Sticker)))
|
||||
.fold(HashMap::new(), |mut acc: HashMap<String, ScoredChatMessage>, message| {
|
||||
if let Some(ref file) = message.file {
|
||||
let message_date = (&message.date_unixtime).parse().expect("Could not parse date_unixtime");
|
||||
let entry = acc.entry(file.to_owned()).or_insert_with(|| ScoredChatMessage::from(message));
|
||||
let message_date = NaiveDateTime::parse_from_str(&message.date, TELEGRAM_TIMESTAMP_FORMAT)
|
||||
.expect("Could not parse date");
|
||||
let entry = acc.entry(file.to_owned()).or_insert_with(|| ScoredChatMessage::try_from(message)
|
||||
.expect("Could not parse date"));
|
||||
entry.last_used = entry.last_used.max(message_date);
|
||||
entry.times += 1;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue