about summary refs log tree commit diff stats
path: root/crates/yt_dlp/src/post_processors/dearrow.rs
blob: 110beebaeea63e98064fbe412c58ae5bcd3d5236 (plain) (blame)
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
use log::{info, warn};
use serde::{Deserialize, Serialize};

use crate::{InfoJson, json_get};

use super::PostProcessor;

#[derive(Debug, Clone, Copy)]
pub struct DeArrowPP;

impl PostProcessor for DeArrowPP {
    fn extractors(&self) -> &'static [&'static str] {
        &["Youtube"]
    }

    fn process(&self, mut info: InfoJson) -> Result<InfoJson, super::Error> {
        let mut output: DeArrowApi = reqwest::blocking::get(format!(
            "https://sponsor.ajay.app/api/branding?videoID={}",
            json_get!(info, "id", as_str)
        ))?
        .json()?;

        output.titles.reverse();

        let title_len = output.titles.len();
        loop {
            let Some(title) = output.titles.pop() else {
                break;
            };

            if (title.locked || title.votes < 1) && title_len > 1 {
                info!(
                    "Skipping title {:#?}, as it is not good enough",
                    title.value
                );
                // Skip titles that are not “good” enough.
                continue;
            }

            if let Some(old_title) = info.insert(
                "title".to_owned(),
                serde_json::Value::String(title.value.clone()),
            ) {
                warn!("Updating title from {:#?} to {:#?}", old_title, title.value);
                info.insert("original_title".to_owned(), old_title);
            } else {
                warn!("Setting title to {:#?}", title.value);
            }

            break;
        }

        Ok(info)
    }
}

#[derive(Serialize, Deserialize)]
/// See: <https://wiki.sponsor.ajay.app/w/API_Docs/DeArrow>
struct DeArrowApi {
    titles: Vec<Title>,
    thumbnails: Vec<Thumbnail>,

    #[serde(alias = "randomTime")]
    random_time: Option<f64>,

    #[serde(alias = "videoDuration")]
    video_duration: Option<f64>,

    #[serde(alias = "casualVotes")]
    casual_votes: Vec<String>,
}

#[derive(Serialize, Deserialize)]
struct Title {
    /// Note: Titles will sometimes contain > before a word.
    /// This tells the auto-formatter to not format a word.
    /// If you have no auto-formatter, you can ignore this and replace it with an empty string
    #[serde(alias = "title")]
    value: String,

    original: bool,
    votes: u64,
    locked: bool,

    #[serde(alias = "UUID")]
    uuid: String,

    /// only present if requested
    #[serde(alias = "userID")]
    user_id: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct Thumbnail {
    // null if original is true
    timestamp: Option<f64>,

    original: bool,
    votes: u64,
    locked: bool,

    #[serde(alias = "UUID")]
    uuid: String,

    /// only present if requested
    #[serde(alias = "userID")]
    user_id: Option<String>,
}