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
use reqwest::Url;
use failure::{Error, bail};
use serde::{Serialize, Deserialize};

use crate::{
    plugin_conf, config::Rtd,
    plugins::{TitlePlugin, PluginConfig},
};

/// YouTube title plugin configuration structure
#[derive(Serialize, Deserialize, Default, Clone)]
#[serde(default)]
pub struct Config {
    api_key: String,
}

/// YouTube title plugin
pub struct YouTubePlugin {}

#[cfg(not(test))]
static REQUEST_URL: &str = "https://www.googleapis.com/youtube/v3/videos?part=snippet";

impl TitlePlugin for YouTubePlugin {
    fn name(&self) -> &'static str { "youtube" }

    fn check(&self, config: &PluginConfig, url: &Url) -> bool {
        if config.youtube.api_key.is_empty() {
            false
        } else {
            url.domain() == Some("youtube.com")
            || url.domain() == Some("www.youtube.com")
            || url.domain() == Some("youtu.be")
            || url.domain() == Some("music.youtube.com")
        }
    }

    fn evaluate(&self, rtd: &Rtd , url: &Url) -> Result<String, Error> {
        let video_id = match url.domain() {
            Some("youtu.be") => url.path()[1..].to_string(),
            Some("www.youtube.com") | Some("youtube.com") | Some("music.youtube.com") => {
                url
                    .query_pairs()
                    .filter(|(k, _)| k == "v")
                    .map(|(_, v)| v)
                    .collect()
            },
            _ => bail!("Unknown domain"),
        };

        let mut req_url = Url::parse(REQUEST_URL)?;
        req_url
            .query_pairs_mut()
            .append_pair("id", &video_id)
            .append_pair("key", &plugin_conf!(rtd, youtube).api_key);

        let client = match rtd.get_client() {
            Ok(c) => c,
            _ => bail!("Can't get http client"),
        };

        let mut res = client
            .request(&req_url.into_string())?
            .json::<Resp>()?;

        let first_item = match res.items.pop() {
            Some(v) => v,
            None => bail!("No list items in response"),
        };

        Ok(first_item.snippet.title)
    }
}

// Structures used for typed JSON parsing

#[derive(Debug, Deserialize)]
struct Resp {
    items: Vec<Item>,
}

#[derive(Debug, Deserialize)]
struct Item {
    snippet: Snippet,
}

#[derive(Debug, Deserialize)]
struct Snippet {
    title: String,
}

// Tests

#[cfg(test)]
static REQUEST_URL: &str = "http://127.0.0.1:28285/v3/";

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        thread,
        time::Duration,
    };
    use tiny_http::Response;

    #[test]
    fn name() {
        let plugin = YouTubePlugin {};
        assert_eq!(plugin.name(), "youtube");
    }

    #[test]
    fn check() {
        let plugin = YouTubePlugin {};
        let mut config = PluginConfig::default();
        let url = Url::parse("https://www.youtube.com/watch?v=abc123def78").unwrap();
        let url2 = Url::parse("https://youtu.be/abc123def78").unwrap();
        let url3 = Url::parse("https://music.youtube.com/watch?v=abc123def78&feature=share").unwrap();
        let bad_url = Url::parse("https://google.com/").unwrap();

        // No API key set
        assert_eq!(plugin.check(&config, &url), false);
        assert_eq!(plugin.check(&config, &url2), false);
        assert_eq!(plugin.check(&config, &url3), false);
        assert_eq!(plugin.check(&config, &bad_url), false);

        // API key is set
        config.youtube.api_key = String::from("bar");
        assert_eq!(plugin.check(&config, &url), true);
        assert_eq!(plugin.check(&config, &url2), true);
        assert_eq!(plugin.check(&config, &url3), true);
        assert_eq!(plugin.check(&config, &bad_url), false);
    }

    #[test]
    fn evaluate_no_client() {
        let plugin = YouTubePlugin {};
        let rtd = Rtd::new();
        let url = "https://www.youtube.com/watch?v=abc123def78";
        let res = plugin.evaluate(&rtd, &url.parse().unwrap());
        assert!(res.is_err());
        if let Err(e) = res { assert_eq!(&format!("{}", e), "Can't get http client"); }
    }

    #[test]
    fn evaluate_bad_domain() {
        let plugin = YouTubePlugin {};
        let rtd = Rtd::new().init_http_client().unwrap();
        let url = "https://www.notyoutube.com/watch?v=abc123def78";
        let res = plugin.evaluate(&rtd, &url.parse().unwrap());
        assert!(res.is_err());
        if let Err(e) = res { assert_eq!(&format!("{}", e), "Unknown domain"); }
    }

    #[test]
    fn evaluate() {
        let plugin = YouTubePlugin {};
        let rtd = Rtd::new().init_http_client().unwrap();
        let bind = "127.0.0.1:28285";
        let url = "https://www.youtube.com/watch?v=abc123def78";
        let response_no_list_items = r#"{"kind":"youtube#videoListResponse","etag":"123456","items":[],"pageInfo":{"totalResults":1,"resultsPerPage":1}}"#;
        let response = r#"{"kind":"youtube#videoListResponse","etag":"123456","items":[{"kind":"youtube#video","etag":"123456","id":"abc123def78","snippet":{"publishedAt":"2020-08-10T11:45:00Z","channelId":"123456789abcdefg","title":"Glorious YouTube video","description":"","thumbnails":{"default":{"url":"","width":120,"height":90},"medium":{"url":"","width":320,"height":180},"high":{"url":"","width":480,"height":360},"standard":{"url":"","width":640,"height":480},"maxres":{"url":"","width":1280,"height":720}},"channelTitle":"A channel name","tags":[],"categoryId":"10","liveBroadcastContent":"none","localized":{"title":"Glorious YouTube video","description":""}}}],"pageInfo":{"totalResults":1,"resultsPerPage":1}}"#;

        let server_thread = thread::spawn(move || {
            let server = tiny_http::Server::http(bind).unwrap();
            for i in 0..2 {
            let rq = server.recv().unwrap();
                if rq.url().to_string().starts_with("/v3/") {
                    let resp = if i == 0 {
                        Response::from_string(response)
                    } else {
                        Response::from_string(response_no_list_items)
                    };
                    thread::sleep(Duration::from_millis(10));
                    rq.respond(resp).unwrap();
                }
            }
        });

        thread::sleep(Duration::from_millis(1000));

        let res = plugin.evaluate(&rtd, &url.parse().unwrap()).unwrap();
        assert_eq!(res, String::from("Glorious YouTube video"));

        thread::sleep(Duration::from_millis(1000));

        let res = plugin.evaluate(&rtd, &url.parse().unwrap());
        assert!(res.is_err());
        if let Err(e) = res { assert_eq!(&format!("{}", e), "No list items in response"); }

        server_thread.join().unwrap();
    }
}