add date to playlist

This commit is contained in:
ThetaDev 2022-09-08 23:01:31 +02:00
parent 9ddf9a3ac4
commit 6bb0c3792e
14 changed files with 129 additions and 100 deletions

View file

@ -5,7 +5,7 @@ use std::{
};
use anyhow::{anyhow, bail, Result};
use chrono::{NaiveDateTime, NaiveTime, TimeZone, Utc};
use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
use fancy_regex::Regex;
use log::{error, warn};
use once_cell::sync::Lazy;
@ -367,7 +367,7 @@ fn map_player_data(response: response::Player, deobf: &Deobfuscator) -> Result<V
},
publish_date: microformat.as_ref().map(|m| {
let ndt = NaiveDateTime::new(m.publish_date, NaiveTime::from_hms(0, 0, 0));
Utc.from_local_datetime(&ndt).unwrap()
Local.from_local_datetime(&ndt).unwrap()
}),
view_count: video_details.view_count,
keywords: video_details
@ -526,7 +526,19 @@ mod tests {
let resp: response::Player = serde_json::from_reader(BufReader::new(json_file)).unwrap();
let player_data = map_player_data(resp, &DEOBFUSCATOR).unwrap();
insta::assert_yaml_snapshot!(format!("map_player_data_{}", name), player_data)
let is_desktop = name == "desktop" || name == "desktopmusic";
insta::assert_yaml_snapshot!(format!("map_player_data_{}", name), player_data, {
".info.publish_date" => insta::dynamic_redaction(move |value, _path| {
if is_desktop {
assert!(value.as_str().unwrap().starts_with("2019-05-30T00:00:00"));
"2019-05-30T00:00:00"
} else {
assert_eq!(value, insta::internals::Content::None);
"~"
}
}),
});
}
/// Assert equality within 10% margin
@ -572,10 +584,7 @@ mod tests {
assert_eq!(player_data.info.is_live_content, false);
if client_type == ClientType::Desktop || client_type == ClientType::DesktopMusic {
assert_eq!(
player_data.info.publish_date.unwrap().to_string(),
"2013-05-05 00:00:00 UTC"
);
assert!(player_data.info.publish_date.unwrap().to_string().starts_with("2013-05-05 00:00:00"));
assert_eq!(player_data.info.category.unwrap(), "Music");
assert_eq!(player_data.info.is_family_safe.unwrap(), true);
}

View file

@ -3,9 +3,9 @@ use reqwest::Method;
use serde::Serialize;
use crate::{
model::{Channel, Playlist, Thumbnail, Video},
model::{Channel, Language, Playlist, Thumbnail, Video},
serializer::text::{PageType, TextLink},
util,
timeago, util,
};
use super::{response, ClientType, ContextYT, RustyTube};
@ -46,7 +46,7 @@ impl RustyTube {
let playlist_response =
serde_json::from_str::<response::Playlist>(&resp_body).context(resp_body)?;
map_playlist(&playlist_response)
map_playlist(&playlist_response, self.localization.language)
}
pub async fn get_playlist_cont(&self, playlist: &mut Playlist) -> Result<()> {
@ -95,7 +95,7 @@ impl RustyTube {
}
}
fn map_playlist(response: &response::Playlist) -> Result<Playlist> {
fn map_playlist(response: &response::Playlist, lang: Language) -> Result<Playlist> {
let video_items = &some_or_bail!(
some_or_bail!(
some_or_bail!(
@ -228,7 +228,10 @@ fn map_playlist(response: &response::Playlist) -> Result<Playlist> {
thumbnails,
description,
channel,
last_update: None,
last_update: match &last_update_txt {
Some(textual_date) => timeago::parse_textual_date_to_dt(lang, textual_date),
None => None,
},
last_update_txt,
})
}
@ -383,15 +386,17 @@ mod tests {
#[case::long("long")]
#[case::short("short")]
#[case::nomusic("nomusic")]
fn t_map_player_data(#[case] name: &str) {
fn t_map_playlist_data(#[case] name: &str) {
let filename = format!("testfiles/playlist/playlist_{}.json", name);
let json_path = Path::new(&filename);
let json_file = File::open(json_path).unwrap();
let playlist: response::Playlist =
serde_json::from_reader(BufReader::new(json_file)).unwrap();
let playlist_data = map_playlist(&playlist).unwrap();
insta::assert_yaml_snapshot!(format!("map_playlist_data_{}", name), playlist_data);
let playlist_data = map_playlist(&playlist, Language::En).unwrap();
insta::assert_yaml_snapshot!(format!("map_playlist_data_{}", name), playlist_data, {
".last_update" => "[date]"
});
}
#[test_log::test(tokio::test)]

View file

@ -23,7 +23,7 @@ info:
channel:
id: UCbxxEi-ImPlbLx5F-fHetEg
name: RomanSenykMusic - Royalty Free Music
publish_date: ~
publish_date: "~"
view_count: 426567
keywords:
- no copyright music

View file

@ -26,7 +26,7 @@ info:
channel:
id: UCbxxEi-ImPlbLx5F-fHetEg
name: RomanSenykMusic - Royalty Free Music
publish_date: "2019-05-30T00:00:00Z"
publish_date: "2019-05-30T00:00:00"
view_count: 426567
keywords:
- no copyright music

View file

@ -20,7 +20,7 @@ info:
channel:
id: UCbxxEi-ImPlbLx5F-fHetEg
name: Romansenykmusic
publish_date: "2019-05-30T00:00:00Z"
publish_date: "2019-05-30T00:00:00"
view_count: 426583
keywords:
- no copyright music

View file

@ -20,7 +20,7 @@ info:
channel:
id: UCbxxEi-ImPlbLx5F-fHetEg
name: RomanSenykMusic - Royalty Free Music
publish_date: ~
publish_date: "~"
view_count: 426567
keywords:
- no copyright music

View file

@ -26,7 +26,7 @@ info:
channel:
id: UCbxxEi-ImPlbLx5F-fHetEg
name: RomanSenykMusic - Royalty Free Music
publish_date: ~
publish_date: "~"
view_count: 426567
keywords:
- no copyright music

View file

@ -1924,6 +1924,6 @@ description: ~
channel:
id: UCIekuFeMaV78xYfvpmoCnPg
name: Best Music
last_update: ~
last_update: "[date]"
last_update_txt: "Last updated on Aug 7, 2022"

View file

@ -1278,6 +1278,6 @@ description: "SHINE - Survival Hardcore in New Environment: Auf einem Server mac
channel:
id: UCQM0bS4_04-Y4JuYrgmnpZQ
name: Chaosflo44
last_update: ~
last_update: "[date]"
last_update_txt: "Last updated on Jul 2, 2014"

View file

@ -1862,6 +1862,6 @@ thumbnails:
height: 1200
description: ~
channel: ~
last_update: ~
last_update: "[date]"
last_update_txt: Updated today