1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{
plugins::{ImportPlugin, PluginContext, PluginInfo},
source_rank::ObjectImportSourceExt,
};
use anyhow::{Context, Result, anyhow};
use chrono::{Utc, format::Parsed};
use jellycommon::*;
use jellydb::RowNum;
use jellyremuxer::matroska::{AttachedFile, Segment};
use log::debug;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs::File, io::BufReader, path::Path};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YVideo {
pub album: Option<String>,
pub age_limit: Option<usize>,
pub alt_title: Option<String>,
pub aspect_ratio: Option<f32>,
pub automatic_captions: Option<HashMap<String, Vec<YCaption>>>,
pub availability: Option<String>, // "public" | "private" | "unlisted",
pub average_rating: Option<String>,
pub categories: Option<Vec<String>>,
pub channel_follower_count: Option<usize>,
pub channel_id: Option<String>,
pub channel_is_verified: Option<bool>,
pub channel: Option<String>,
pub chapters: Option<Vec<YChapter>>,
pub comment_count: Option<usize>,
pub description: Option<String>,
pub display_id: Option<String>,
pub duration_string: Option<String>,
pub duration: Option<f64>,
pub epoch: usize,
pub extractor_key: String,
pub extractor: String,
pub formats: Option<Vec<YFormat>>,
pub fulltitle: Option<String>,
pub heatmap: Option<Vec<YHeatmapSample>>,
pub height: Option<i32>,
pub id: String,
pub is_live: Option<bool>,
pub like_count: Option<usize>,
pub media_type: Option<String>,
pub n_entries: Option<usize>,
pub original_url: Option<String>,
pub playable_in_embed: Option<bool>,
pub playlist_count: Option<usize>,
pub playlist_id: Option<String>,
pub playlist_index: Option<usize>,
pub playlist_title: Option<String>,
pub playlist_uploader_id: Option<String>,
pub playlist_uploader: Option<String>,
pub playlist: Option<String>,
pub tags: Option<Vec<String>>,
pub thumbnail: Option<String>,
pub thumbnails: Option<Vec<YThumbnail>>,
pub title: String,
pub upload_date: Option<String>,
pub uploader_id: Option<String>,
pub uploader_url: Option<String>,
pub uploader: Option<String>,
pub view_count: Option<usize>,
pub was_live: Option<bool>,
pub webpage_url_basename: String,
pub webpage_url_domain: String,
pub webpage_url: String,
pub width: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YCaption {
pub url: Option<String>,
pub ext: String, //"vtt" | "json3" | "srv1" | "srv2" | "srv3" | "ttml",
pub protocol: Option<String>,
pub name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YFormat {
pub format_id: String,
pub format_note: Option<String>,
pub ext: String,
pub protocol: String,
pub acodec: Option<String>,
pub vcodec: Option<String>,
pub url: Option<String>,
pub width: Option<u32>,
pub height: Option<u32>,
pub fps: Option<f64>,
pub columns: Option<u32>,
pub fragments: Option<Vec<YFragment>>,
pub resolution: Option<String>,
pub dynamic_range: Option<String>,
pub aspect_ratio: Option<f64>,
pub http_headers: HashMap<String, String>,
pub audio_ext: String,
pub video_ext: String,
pub vbr: Option<f64>,
pub abr: Option<f64>,
pub format: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YFragment {
pub url: Option<String>,
pub duration: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YThumbnail {
pub url: String,
pub preference: Option<i32>,
pub id: String,
pub height: Option<u32>,
pub width: Option<u32>,
pub resolution: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YChapter {
pub start_time: f64,
pub end_time: f64,
pub title: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YHeatmapSample {
pub start_time: f64,
pub end_time: f64,
pub value: f64,
}
pub fn parse_upload_date(d: &str) -> anyhow::Result<i64> {
let (year, month, day) = (&d[0..4], &d[4..6], &d[6..8]);
let (year, month, day) = (
year.parse().context("parsing year")?,
month.parse().context("parsing month")?,
day.parse().context("parsing day")?,
);
let mut p = Parsed::new();
p.year = Some(year);
p.month = Some(month);
p.day = Some(day);
p.hour_div_12 = Some(0);
p.hour_mod_12 = Some(0);
p.minute = Some(0);
p.second = Some(0);
Ok(p.to_datetime_with_timezone(&Utc)?.timestamp_millis())
}
pub fn is_info_json(a: &&AttachedFile) -> bool {
a.name == "info.json" && a.media_type == "application/json"
}
pub struct Infojson;
impl ImportPlugin for Infojson {
fn info(&self) -> PluginInfo {
PluginInfo {
name: "infojson",
tag: MSOURCE_INFOJSON,
handle_file: true,
handle_media: true,
..Default::default()
}
}
fn file(&self, ct: &PluginContext, parent: RowNum, path: &Path) -> Result<()> {
let filename = path.file_name().unwrap().to_string_lossy();
if filename != "channel.info.json" {
return Ok(());
}
debug!("import channel info.json at {path:?}");
let data = serde_json::from_reader::<_, YVideo>(BufReader::new(File::open(path)?))?;
let title = clean_uploader_name(&data.title);
ct.ic.db.transaction(&mut |txn| {
let mut node = txn.get(parent)?.unwrap();
node = node.as_object().insert_s(ct.is, NO_KIND, KIND_CHANNEL);
node = node.as_object().insert_s(ct.is, NO_TITLE, title);
if let Some(cid) = &data.channel_id {
node = node.as_object().update(NO_IDENTIFIERS, |ids| {
ids.insert_s(ct.is, IDENT_YOUTUBE_CHANNEL, &cid)
});
}
if let Some(uid) = &data.uploader_id {
node = node.as_object().update(NO_IDENTIFIERS, |ids| {
ids.insert_s(ct.is, IDENT_YOUTUBE_CHANNEL_HANDLE, &uid)
})
}
if let Some(desc) = &data.description {
node = node.as_object().insert_s(ct.is, NO_DESCRIPTION, &desc);
}
if let Some(followers) = data.channel_follower_count {
node = node.as_object().update(NO_RATINGS, |rat| {
rat.insert_s(ct.is, RTYP_YOUTUBE_SUBSCRIBERS, followers as f64)
});
}
txn.update(parent, node)
})
}
fn media(&self, ct: &PluginContext, row: RowNum, _path: &Path, seg: &Segment) -> Result<()> {
let infojson = seg
.attachments
.iter()
.flat_map(|a| &a.files)
.find(is_info_json)
.map(|att| {
let data = ct
.ic
.cache
.read(str::from_utf8(&att.data).unwrap())?
.ok_or(anyhow!("info json cache missing"))?;
anyhow::Ok(serde_json::from_slice::<YVideo>(&data)?)
})
.transpose()
.context("infojson parsing")?;
if let Some(infojson) = infojson {
let release_date = infojson
.upload_date
.as_ref()
.map(|date| parse_upload_date(date).context("parsing upload date"))
.transpose()?;
let kind = if let Some(ty) = &infojson.media_type
&& ty == "short"
{
KIND_SHORTFORMVIDEO
} else if infojson.album.is_some() {
KIND_MUSIC
} else {
KIND_VIDEO
};
ct.ic.db.transaction(&mut |txn| {
let mut node = txn.get(row)?.unwrap();
node = node.as_object().insert_s(ct.is, NO_KIND, kind);
node = node.as_object().insert_s(ct.is, NO_TITLE, &infojson.title);
if let Some(title) = &infojson.alt_title
&& title != &infojson.title
&& !node.as_object().has(NO_SUBTITLE.0)
{
node = node.as_object().insert_s(ct.is, NO_SUBTITLE, &title);
}
if let Some(up) = &infojson.uploader
&& !node.as_object().has(NO_SUBTITLE.0)
{
node = node
.as_object()
.insert_s(ct.is, NO_SUBTITLE, &clean_uploader_name(&up));
}
if let Some(desc) = &infojson.description {
node = node.as_object().insert_s(ct.is, NO_DESCRIPTION, &desc);
}
if let Some(tag) = infojson.tags.clone() {
node = node
.as_object()
.extend(NO_TAG, tag.iter().map(String::as_str));
}
if let Some(rd) = release_date {
node = node.as_object().insert_s(ct.is, NO_RELEASEDATE, rd);
}
match infojson.extractor.as_str() {
"youtube" => {
node = node.as_object().update(NO_IDENTIFIERS, |rat| {
rat.insert_s(ct.is, IDENT_YOUTUBE_VIDEO, &infojson.id)
});
node = node.as_object().update(NO_RATINGS, |rat| {
rat.insert_s(
ct.is,
RTYP_YOUTUBE_VIEWS,
infojson.view_count.unwrap_or_default() as f64,
)
});
node = node.as_object().update(NO_RATINGS, |rat| {
rat.insert_s(
ct.is,
RTYP_YOUTUBE_LIKES,
infojson.like_count.unwrap_or_default() as f64,
)
});
}
"Bandcamp" => {
node = node.as_object().update(NO_IDENTIFIERS, |rat| {
rat.insert_s(ct.is, IDENT_BANDCAMP, &infojson.id)
});
}
_ => (),
};
txn.update(row, node)
})?;
}
Ok(())
}
}
fn clean_uploader_name(mut s: &str) -> &str {
s = s.strip_suffix(" - Videos").unwrap_or(s); // youtube
s = s.strip_suffix(" - Topic").unwrap_or(s); // youtube
s = s.strip_prefix("Uploads from ").unwrap_or(s); // youtube
s = s.strip_prefix("Discography of ").unwrap_or(s); // bandcamp
s
}
|