feat(backend): insert yt-dlp metadata on successful download

This commit is contained in:
Tomáš Mládek 2024-07-01 22:04:11 +02:00
parent 94464c6cd8
commit a3353cca65

View file

@ -39,6 +39,7 @@ impl Extractor for YtDlpExtractor {
"--progress", "--progress",
"--no-call-home", "--no-call-home",
"--no-playlist", "--no-playlist",
"--write-info-json",
url.as_str(), url.as_str(),
]) ])
.current_dir(temp_dir.path()) .current_dir(temp_dir.path())
@ -102,14 +103,52 @@ impl Extractor for YtDlpExtractor {
)); ));
} }
let mut result = vec![];
let files = std::fs::read_dir(temp_dir.path())?.collect::<Result<Vec<_>, _>>()?; let files = std::fs::read_dir(temp_dir.path())?.collect::<Result<Vec<_>, _>>()?;
let destination = files let destination = files
.first() .iter()
.find(|f| f.path().extension().map(|e| e != "json").unwrap_or(false))
.ok_or_else(|| { .ok_or_else(|| {
anyhow::anyhow!("yt-dlp didn't produce any files in {:?}", temp_dir.path()) anyhow::anyhow!("yt-dlp didn't produce any files in {:?}", temp_dir.path())
})? })?
.path(); .path();
let json_path = files
.iter()
.find(|f| f.path().extension().map(|e| e == "json").unwrap_or(false))
.ok_or_else(|| {
anyhow::anyhow!(
"yt-dlp didn't produce any json files in {:?}",
temp_dir.path()
)
})?
.path();
let json_text = std::fs::read_to_string(json_path)?;
let json_data = serde_json::from_str::<serde_json::Value>(&json_text)?;
for key in [
"title",
"fulltitle",
"description",
"channel",
"uploader",
"channel_url",
"upload_date",
"timestamp",
] {
if let Some(value) = json_data.get(key) {
result.push(Entry {
entity: address.clone(),
attribute: format!("YTDL_META_{}", key).parse().unwrap(),
value: EntryValue::guess_from(value.to_string()),
provenance: context.provenance.clone() + "EXTRACTOR yt-dlp",
user: context.user.clone(),
timestamp: chrono::Utc::now().naive_utc(),
});
}
}
let stored = store.store( let stored = store.store(
connection, connection,
destination.clone().into(), destination.clone().into(),
@ -119,16 +158,19 @@ impl Extractor for YtDlpExtractor {
None, None,
context.clone(), context.clone(),
)?; )?;
job_handle.update_progress(100.0)?;
Ok(vec![Entry { result.push(Entry {
entity: address.clone(), entity: address.clone(),
attribute: "YTDLD".parse().unwrap(), attribute: "YTDLD".parse().unwrap(),
value: Address::Hash(stored).into(), value: Address::Hash(stored).into(),
provenance: context.provenance.clone() + "EXTRACTOR yt-dlp", provenance: context.provenance.clone() + "EXTRACTOR yt-dlp",
user: context.user.clone(), user: context.user.clone(),
timestamp: chrono::Utc::now().naive_utc(), timestamp: chrono::Utc::now().naive_utc(),
}]) });
job_handle.update_progress(100.0)?;
Ok(result)
} else { } else {
Ok(vec![]) Ok(vec![])
} }