crispy-catchup 0.1.1

Catchup and timeshift URL utilities for IPTV channels
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Template variable substitution engine for catchup URLs.
//!
//! Translated from the anonymous namespace functions in `CatchupController.cpp`:
//! - `FormatDateTime()` — full template processing
//! - `FormatTime()` — single-char time format specifiers (`{Y}`, `{m}`, etc.)
//! - `FormatUnits()` — divisible time units (`{duration:N}`, `{offset:N}`)
//! - `FormatUtc()` — absolute timestamp substitution

use chrono::{DateTime, Datelike, Local, TimeZone, Timelike, Utc};
use regex::Regex;

/// Format a catchup URL template by substituting all time-related placeholders.
///
/// This is the main entry point, translated from `FormatDateTime()` in
/// `CatchupController.cpp`.
///
/// # Arguments
/// * `template` - URL template with placeholders
/// * `start` - Programme start time (UTC epoch seconds)
/// * `duration_secs` - Programme duration in seconds
/// * `catchup_id` - Optional programme catchup ID for `{catchup-id}` substitution
/// * `timezone_shift_secs` - Timezone offset to apply (channel tvg-shift + correction)
pub fn format_catchup_url(
    template: &str,
    start: i64,
    duration_secs: i64,
    catchup_id: Option<&str>,
    timezone_shift_secs: i32,
) -> String {
    format_catchup_url_with_granularity(
        template,
        start,
        duration_secs,
        catchup_id,
        timezone_shift_secs,
        1,
    )
}

/// Format a catchup URL template with granularity clamping.
///
/// When `granularity_secs > 1`, the effective duration is clamped (rounded down)
/// to the nearest multiple of `granularity_secs`. This matches Kodi's behaviour
/// where `FindCatchupSourceGranularitySeconds()` controls time precision.
///
/// # Arguments
/// * `template` - URL template with placeholders
/// * `start` - Programme start time (UTC epoch seconds)
/// * `duration_secs` - Programme duration in seconds
/// * `catchup_id` - Optional programme catchup ID for `{catchup-id}` substitution
/// * `timezone_shift_secs` - Timezone offset to apply (channel tvg-shift + correction)
/// * `granularity_secs` - Time granularity in seconds (1 = no clamping, 60 = minute boundaries)
pub fn format_catchup_url_with_granularity(
    template: &str,
    start: i64,
    duration_secs: i64,
    catchup_id: Option<&str>,
    timezone_shift_secs: i32,
    granularity_secs: i32,
) -> String {
    let clamped_duration = if granularity_secs > 1 {
        let g = granularity_secs as i64;
        (duration_secs / g) * g
    } else {
        duration_secs
    };

    let adjusted_start = start - timezone_shift_secs as i64;
    let now = Utc::now().timestamp() - timezone_shift_secs as i64;
    let end = adjusted_start + clamped_duration;

    let dt_start = timestamp_to_local(adjusted_start);
    let dt_end = timestamp_to_local(end);
    let dt_now = timestamp_to_local(now);

    let mut result = template.to_string();

    // Single-char time specifiers based on start time: {Y}, {m}, {d}, {H}, {M}, {S}
    format_time_char('Y', &dt_start, &mut result);
    format_time_char('m', &dt_start, &mut result);
    format_time_char('d', &dt_start, &mut result);
    format_time_char('H', &dt_start, &mut result);
    format_time_char('M', &dt_start, &mut result);
    format_time_char('S', &dt_start, &mut result);

    // Absolute UTC timestamps
    format_utc("{utc}", adjusted_start, &mut result);
    format_utc("${start}", adjusted_start, &mut result);
    format_utc("{utcend}", end, &mut result);
    format_utc("${end}", end, &mut result);
    format_utc("{lutc}", now, &mut result);
    format_utc("${now}", now, &mut result);
    format_utc("${timestamp}", now, &mut result);
    format_utc("${duration}", clamped_duration, &mut result);
    format_utc("{duration}", clamped_duration, &mut result);
    format_units("duration", clamped_duration, &mut result);
    format_utc("${offset}", now - adjusted_start, &mut result);
    format_units("offset", now - adjusted_start, &mut result);

    // Named time format strings: {utc:YmdHMS}, ${start:Y-m-d}, etc.
    format_time_named("utc", &dt_start, &mut result, false);
    format_time_named("start", &dt_start, &mut result, true);
    format_time_named("utcend", &dt_end, &mut result, false);
    format_time_named("end", &dt_end, &mut result, true);
    format_time_named("lutc", &dt_now, &mut result, false);
    format_time_named("now", &dt_now, &mut result, true);
    format_time_named("timestamp", &dt_now, &mut result, true);

    // {catchup-id} substitution
    if let Some(id) = catchup_id {
        result = result.replace("{catchup-id}", id);
    }

    result
}

/// Format only "now"-related timestamps in a URL template.
///
/// Used for live stream URL processing where only current-time placeholders
/// need substitution. Translated from `FormatDateTimeNowOnly()`.
///
/// If `programme_start > 0`, also processes start/end/duration specifiers.
pub fn format_now_only(
    template: &str,
    timezone_shift_secs: i32,
    programme_start: i64,
    programme_duration: i64,
) -> String {
    let now = Utc::now().timestamp() - timezone_shift_secs as i64;
    let dt_now = timestamp_to_local(now);

    let mut result = template.to_string();

    format_utc("{lutc}", now, &mut result);
    format_utc("${now}", now, &mut result);
    format_utc("${timestamp}", now, &mut result);
    format_time_named("lutc", &dt_now, &mut result, false);
    format_time_named("now", &dt_now, &mut result, true);
    format_time_named("timestamp", &dt_now, &mut result, true);

    if programme_start > 0 {
        let adjusted_start = programme_start - timezone_shift_secs as i64;
        let end = adjusted_start + programme_duration;
        let dt_start = timestamp_to_local(adjusted_start);
        let dt_end = timestamp_to_local(end);

        format_time_char('Y', &dt_start, &mut result);
        format_time_char('m', &dt_start, &mut result);
        format_time_char('d', &dt_start, &mut result);
        format_time_char('H', &dt_start, &mut result);
        format_time_char('M', &dt_start, &mut result);
        format_time_char('S', &dt_start, &mut result);

        format_utc("{utc}", adjusted_start, &mut result);
        format_utc("${start}", adjusted_start, &mut result);
        format_utc("{utcend}", end, &mut result);
        format_utc("${end}", end, &mut result);
        format_utc("{lutc}", now, &mut result);
        format_utc("${now}", now, &mut result);
        format_utc("${timestamp}", now, &mut result);
        format_utc("${duration}", programme_duration, &mut result);
        format_utc("{duration}", programme_duration, &mut result);
        format_units("duration", programme_duration, &mut result);
        format_utc("${offset}", now - adjusted_start, &mut result);
        format_units("offset", now - adjusted_start, &mut result);

        format_time_named("utc", &dt_start, &mut result, false);
        format_time_named("start", &dt_start, &mut result, true);
        format_time_named("utcend", &dt_end, &mut result, false);
        format_time_named("end", &dt_end, &mut result, true);
        format_time_named("lutc", &dt_now, &mut result, false);
        format_time_named("now", &dt_now, &mut result, true);
        format_time_named("timestamp", &dt_now, &mut result, true);
    }

    result
}

/// Validate whether a requested catchup time is within the allowed window.
///
/// # Arguments
/// * `requested_time` - The requested start time (UTC epoch seconds)
/// * `catchup_days` - Number of days in the catchup window (-1 to ignore)
///
/// Returns `true` if the time is within the window or the window is ignored.
pub fn is_within_catchup_window(requested_time: i64, catchup_days: i32) -> bool {
    if catchup_days < 0 {
        return true; // IGNORE_CATCHUP_DAYS
    }
    if catchup_days == 0 {
        return false;
    }
    let window_start = Utc::now().timestamp() - (catchup_days as i64 * 24 * 60 * 60);
    requested_time >= window_start
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Convert a UTC epoch timestamp to a local `DateTime`.
fn timestamp_to_local(epoch: i64) -> DateTime<Local> {
    Local
        .timestamp_opt(epoch, 0)
        .single()
        .unwrap_or_else(Local::now)
}

/// Replace a single-char time specifier `{ch}` with the formatted value.
///
/// Translated from `FormatTime(const char ch, ...)` in `CatchupController.cpp`.
/// Supports: Y (4-digit year), m (2-digit month), d (2-digit day),
/// H (2-digit hour), M (2-digit minute), S (2-digit second).
fn format_time_char(ch: char, dt: &DateTime<Local>, url: &mut String) {
    let placeholder = format!("{{{ch}}}");
    if !url.contains(&placeholder) {
        return;
    }

    let replacement = match ch {
        'Y' => format!("{:04}", dt.year()),
        'm' => format!("{:02}", dt.month()),
        'd' => format!("{:02}", dt.day()),
        'H' => format!("{:02}", dt.hour()),
        'M' => format!("{:02}", dt.minute()),
        'S' => format!("{:02}", dt.second()),
        _ => return,
    };

    while url.contains(&placeholder) {
        *url = url.replacen(&placeholder, &replacement, 1);
    }
}

/// Replace an absolute UTC timestamp placeholder with the epoch value.
///
/// Translated from `FormatUtc()` in `CatchupController.cpp`.
fn format_utc(placeholder: &str, epoch: i64, url: &mut String) {
    if let Some(pos) = url.find(placeholder) {
        let value = epoch.to_string();
        url.replace_range(pos..pos + placeholder.len(), &value);
    }
}

/// Replace `{name:N}` divisible-unit specifiers.
///
/// Translated from `FormatUnits()` in `CatchupController.cpp`.
/// E.g., `{duration:60}` divides the duration by 60 to get minutes.
fn format_units(name: &str, time: i64, url: &mut String) {
    let pattern = format!(r"\{{{}:(\d+)\}}", regex::escape(name));
    let re = Regex::new(&pattern).expect("dynamic units regex");

    if let Some(caps) = re.captures(url) {
        let full_match = caps.get(0).unwrap();
        let divider: i64 = caps.get(1).unwrap().as_str().parse().unwrap_or(1);

        if divider != 0 {
            let units = std::cmp::max(0, time / divider);
            let match_str = full_match.as_str().to_string();
            *url = url.replacen(&match_str, &units.to_string(), 1);
        }
    }
}

/// Replace named time format strings like `{utc:Y-m-d H:M:S}` or `${start:YmdHMS}`.
///
/// Translated from `FormatTime(const std::string name, ...)` in
/// `CatchupController.cpp`.
///
/// The format string inside the braces uses single-char specifiers:
/// Y, m, d, H, M, S — each replaced with the strftime equivalent `%Y`, etc.
fn format_time_named(name: &str, dt: &DateTime<Local>, url: &mut String, has_var_prefix: bool) {
    let qualifier = if has_var_prefix {
        format!("${{{name}:")
    } else {
        format!("{{{name}:")
    };

    let Some(found) = url.find(&qualifier) else {
        return;
    };

    let start = found + qualifier.len();
    let end = match url[start..].find('}') {
        Some(pos) => start + pos,
        None => return,
    };

    let format_str = &url[start..end];

    // Replace each single-char specifier with its value
    let mut formatted = format_str.to_string();
    formatted = formatted.replace('Y', &format!("{:04}", dt.year()));
    formatted = formatted.replace('m', &format!("{:02}", dt.month()));
    formatted = formatted.replace('d', &format!("{:02}", dt.day()));
    formatted = formatted.replace('H', &format!("{:02}", dt.hour()));
    formatted = formatted.replace('M', &format!("{:02}", dt.minute()));
    formatted = formatted.replace('S', &format!("{:02}", dt.second()));

    // Replace the entire qualifier...format} with the result
    let total_end = end + 1; // include closing '}'
    url.replace_range(found..total_end, &formatted);
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    /// Helper: create a fixed UTC timestamp for reproducible tests.
    /// 2024-03-15 14:30:45 UTC
    fn fixed_start() -> i64 {
        Utc.with_ymd_and_hms(2024, 3, 15, 14, 30, 45)
            .unwrap()
            .timestamp()
    }

    // Use a deterministic template test that doesn't depend on "now"
    // by using a template that only references start/end/duration.

    #[test]
    fn single_char_time_specifiers() {
        let start = fixed_start();
        let template = "http://example.com/{Y}/{m}/{d}/{H}/{M}/{S}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        // The exact values depend on local timezone, but we can verify
        // the placeholders were replaced (no braces remain for those chars)
        assert!(!result.contains("{Y}"));
        assert!(!result.contains("{m}"));
        assert!(!result.contains("{d}"));
        assert!(!result.contains("{H}"));
        assert!(!result.contains("{M}"));
        assert!(!result.contains("{S}"));
    }

    #[test]
    fn absolute_utc_timestamps() {
        let start = fixed_start();
        let duration = 3600i64;
        let template = "http://example.com?start={utc}&end={utcend}&dur={duration}";
        let result = format_catchup_url(template, start, duration, None, 0);

        assert!(result.contains(&format!("start={start}")));
        assert!(result.contains(&format!("end={}", start + duration)));
        assert!(result.contains(&format!("dur={duration}")));
    }

    #[test]
    fn dollar_prefixed_timestamps() {
        let start = fixed_start();
        let duration = 7200i64;
        let template = "http://example.com?s=${start}&e=${end}&d=${duration}";
        let result = format_catchup_url(template, start, duration, None, 0);

        assert!(result.contains(&format!("s={start}")));
        assert!(result.contains(&format!("e={}", start + duration)));
        assert!(result.contains(&format!("d={duration}")));
    }

    #[test]
    fn duration_divisor_units() {
        let start = fixed_start();
        let duration = 7200i64; // 2 hours = 120 minutes
        let template = "http://example.com?dur={duration:60}";
        let result = format_catchup_url(template, start, duration, None, 0);

        assert_eq!(result, "http://example.com?dur=120");
    }

    #[test]
    fn duration_divisor_seconds() {
        let start = fixed_start();
        let duration = 3600i64;
        let template = "http://example.com?dur={duration:1}";
        let result = format_catchup_url(template, start, duration, None, 0);

        assert_eq!(result, "http://example.com?dur=3600");
    }

    #[test]
    fn named_time_format_utc() {
        let start = fixed_start();
        let template = "http://example.com?t={utc:Y-m-d H:M:S}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        // Verify braces are gone and format is applied
        assert!(!result.contains("{utc:"));
        // Should contain date-like pattern (digits and separators)
        assert!(result.contains("?t="));
        // Verify it contains hyphens and colons from the format
        let time_part = result.split("?t=").nth(1).unwrap();
        assert!(time_part.contains('-'));
        assert!(time_part.contains(':'));
    }

    #[test]
    fn named_time_format_with_dollar_prefix() {
        let start = fixed_start();
        let template = "http://example.com?t=${start:Y-m-d}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        assert!(!result.contains("${start:"));
        let time_part = result.split("?t=").nth(1).unwrap();
        assert!(time_part.contains('-'));
    }

    #[test]
    fn catchup_id_substitution() {
        let start = fixed_start();
        let template = "http://example.com/{catchup-id}";
        let result = format_catchup_url(template, start, 3600, Some("prog_12345"), 0);

        assert_eq!(result, "http://example.com/prog_12345");
    }

    #[test]
    fn catchup_id_no_substitution_when_none() {
        let start = fixed_start();
        let template = "http://example.com/{catchup-id}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        assert_eq!(result, "http://example.com/{catchup-id}");
    }

    #[test]
    fn timezone_offset_applied() {
        let start = fixed_start();
        let duration = 3600i64;
        // Apply a 2-hour timezone shift
        let tz_shift = 7200;
        let template = "http://example.com?start={utc}";
        let result = format_catchup_url(template, start, duration, None, tz_shift);

        let expected_shifted = start - tz_shift as i64;
        assert!(result.contains(&format!("start={expected_shifted}")));
    }

    #[test]
    fn xtream_codes_full_template() {
        let start = fixed_start();
        let duration = 3600i64;
        let template =
            "http://list.tv:8080/timeshift/user/pass/{duration:60}/{Y}-{m}-{d}:{H}-{M}/1477.ts";
        let result = format_catchup_url(template, start, duration, None, 0);

        // Duration in minutes
        assert!(result.contains("/60/"));
        // No unresolved placeholders
        assert!(!result.contains("{duration"));
        assert!(!result.contains("{Y}"));
        assert!(!result.contains("{m}"));
        assert!(!result.contains("{d}"));
        assert!(!result.contains("{H}"));
        assert!(!result.contains("{M}"));
    }

    #[test]
    fn catchup_window_within() {
        let now = Utc::now().timestamp();
        // 1 hour ago is within a 7-day window
        assert!(is_within_catchup_window(now - 3600, 7));
    }

    #[test]
    fn catchup_window_outside() {
        let now = Utc::now().timestamp();
        // 8 days ago is outside a 7-day window
        assert!(!is_within_catchup_window(now - 8 * 86400, 7));
    }

    #[test]
    fn catchup_window_ignore() {
        // IGNORE_CATCHUP_DAYS (-1) always returns true
        assert!(is_within_catchup_window(0, -1));
    }

    #[test]
    fn catchup_window_zero_days() {
        let now = Utc::now().timestamp();
        assert!(!is_within_catchup_window(now, 0));
    }

    #[test]
    fn format_now_only_basic() {
        let template = "http://example.com?now=${now}";
        let result = format_now_only(template, 0, 0, 0);

        // ${now} should be replaced with a timestamp
        assert!(!result.contains("${now}"));
        let time_str = result.split("now=").nth(1).unwrap();
        let _ts: i64 = time_str.parse().expect("should be a number");
    }

    #[test]
    fn format_now_only_with_programme() {
        let start = fixed_start();
        let duration = 3600i64;
        let template = "http://example.com?s={utc}&d={duration}";
        let result = format_now_only(template, 0, start, duration);

        assert!(result.contains(&format!("s={start}")));
        assert!(result.contains(&format!("d={duration}")));
    }

    #[test]
    fn offset_units_specifier() {
        let start = fixed_start();
        let template = "http://example.com?o={offset:1}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        // {offset:1} should be replaced with seconds since start
        assert!(!result.contains("{offset:"));
        let offset_str = result.split("o=").nth(1).unwrap();
        let offset: i64 = offset_str.parse().expect("should be a number");
        assert!(offset >= 0);
    }

    #[test]
    fn multiple_same_char_specifiers() {
        let start = fixed_start();
        let template = "http://example.com/{Y}/{Y}";
        let result = format_catchup_url(template, start, 3600, None, 0);

        // Both {Y} should be replaced
        assert!(!result.contains("{Y}"));
        let parts: Vec<&str> = result
            .trim_start_matches("http://example.com/")
            .split('/')
            .collect();
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0], parts[1]); // both should be the same year
    }

    #[test]
    fn negative_duration_clamped_to_zero() {
        let start = fixed_start();
        let template = "http://example.com?dur={duration:60}";
        let result = format_catchup_url(template, start, -120, None, 0);

        // Negative duration divided by 60 = negative, clamped to 0
        assert_eq!(result, "http://example.com?dur=0");
    }

    // -----------------------------------------------------------------------
    // Granularity clamping tests
    // -----------------------------------------------------------------------

    #[test]
    fn granularity_60_clamps_90s_to_60s() {
        let start = fixed_start();
        // 90s duration with 60s granularity → clamped to 60s
        let template = "http://example.com?dur=${duration}";
        let result = format_catchup_url_with_granularity(template, start, 90, None, 0, 60);

        assert!(result.contains("dur=60"));
    }

    #[test]
    fn granularity_1_no_clamping() {
        let start = fixed_start();
        // 90s duration with 1s granularity → no clamping, stays 90s
        let template = "http://example.com?dur=${duration}";
        let result = format_catchup_url_with_granularity(template, start, 90, None, 0, 1);

        assert!(result.contains("dur=90"));
    }

    #[test]
    fn granularity_60_clamps_duration_units_too() {
        let start = fixed_start();
        // 150s with granularity=60 → clamped to 120s. {duration:60} = 120/60 = 2 minutes
        let template = "http://example.com?dur={duration:60}";
        let result = format_catchup_url_with_granularity(template, start, 150, None, 0, 60);

        assert_eq!(result, "http://example.com?dur=2");
    }
}