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
use format::Format;
use errors::*;
use reqwest::{Client, StatusCode, self as request};
use std::collections::HashMap;
use std::io::Read;
use env_variables;

use url::{Url, form_urlencoded};
use url::percent_encoding::percent_decode;

const YOUTUBE_VIDEO_INFO_URL: &str = "https://www.youtube.com/get_video_info";

#[derive(Serialize, Deserialize, Default)]
pub struct VideoInfo {
    pub title: String,
    pub id: String,
    pub describe: String,
    pub formats: Vec<Format>,
    pub keywords: Vec<String>,
    pub author: String,
    pub duration: i32,
}

pub fn get_download_url(f: &Format) -> Result<Url> {
    let url_str = if let Some(u) = f.meta.get("url") {
        u.as_str()
    } else {
        bail!("couldn't extract url from format")
    };

    let url_str = percent_decode(url_str.as_bytes())
        .decode_utf8()?
        .into_owned();
    Ok(Url::parse(&url_str)?)
}

pub fn get_filename(i: &VideoInfo, f: &Format) -> String {
    let title = if !i.title.is_empty() {
        i.title.to_owned()
    } else {
        "no title".to_string()
    };

    format!("{} {}.{}", title, f.resolution, f.extension)
}

pub fn get_video_info(value: &str) -> Result<VideoInfo> {
    let parse_url = match Url::parse(value) {
        Ok(u) => u,
        Err(_) => {
            return get_video_info_from_html(value);
        }
    };

    if parse_url.host_str() == Some("youtu.be") {
        return get_video_info_from_short_url(&parse_url);
    }

    get_video_info_from_url(&parse_url)
}

fn get_video_info_from_url(u: &Url) -> Result<VideoInfo> {
    if let Some(video_id) = u.query_pairs()
        .into_owned()
        .collect::<HashMap<String, String>>()
        .get("v")
    {
        return get_video_info_from_html(video_id);
    }
    bail!("invalid youtube url, no video id");
}

fn get_video_info_from_short_url(u: &Url) -> Result<VideoInfo> {
    let path = u.path().trim_left_matches("/");
    if path.len() > 0 {
        return get_video_info_from_html(path);
    }

    bail!("could not parse short URL");
}

fn get_video_info_from_html(id: &str) -> Result<VideoInfo> {
    let info_url = format!("{}?video_id={}", YOUTUBE_VIDEO_INFO_URL, id);
    debug!("{}", info_url);
    let mut resp = get_client(info_url.as_str())?.get(info_url.as_str())?.send()?;
    if resp.status() != StatusCode::Ok {
        bail!("video info response invalid status code");
    }

    let mut info = String::new();
    resp.read_to_string(&mut info)?;
    let info = parse_query(info);
    let mut video_info: VideoInfo = Default::default();
    match info.get("status") {
        Some(s) => {
            if s == "fail" {
                bail!(format!(
                    "Error {}:{}",
                    info.get("errorcode")
                        .map(|s| s.as_str())
                        .unwrap_or_default(),
                    info.get("reason").map(|s| s.as_str()).unwrap_or_default()
                ));
            }
        }
        None => {
            return Err(From::from("get video info, status not found"));
        }
    };

    if let Some(title) = info.get("title") {
        video_info.title = title.to_string();
    } else {
        debug!("unable to extract title");
    }

    if let Some(author) = info.get("author") {
        video_info.author = author.to_string();
    } else {
        debug!("unable to extract author");
    }

    if let Some(length) = info.get("length_seconds") {
        video_info.duration = length.parse::<i32>().unwrap_or_default();
    } else {
        debug!("unable to parse duration string");
    }

    if let Some(keywords) = info.get("keywords") {
        video_info.keywords = keywords.split(",").map(|s| s.to_string()).collect();
    } else {
        debug!("unable to extract keywords")
    }

    let mut format_strings = vec![];
    if let Some(fmt_stream) = info.get("url_encoded_fmt_stream_map") {
        format_strings.append(&mut fmt_stream.split(",").collect())
    }

    if let Some(adaptive_fmts) = info.get("adaptive_fmts") {
        format_strings.append(&mut adaptive_fmts.split(",").collect());
    }

    let mut formats: Vec<Format> = vec![];
    for v in &format_strings {
        let query = parse_query(v.to_string());
        let itag = match query.get("itag") {
            Some(i) => i,
            None => {
                continue;
            }
        };

        if let Ok(i) = itag.parse::<i32>() {
            if let Some(mut f) = Format::new(i) {
                if query
                    .get("conn")
                    .map(|s| s.as_str())
                    .unwrap_or_default()
                    .starts_with("rtmp")
                {
                    f.meta.insert("rtmp".to_string(), "true".to_string());
                }

                for (k, v) in &query {
                    f.meta.insert(k.to_string(), v.to_string());
                }

                formats.push(f);
            } else {
                debug!("no metadata found for itag: {}, skipping...", itag)
            }
        }
    }

    video_info.formats = formats;
    Ok(video_info)
}

fn parse_query(query_str: String) -> HashMap<String, String> {
    let parse_query = form_urlencoded::parse(query_str.as_bytes());
    return parse_query
        .into_owned()
        .collect::<HashMap<String, String>>();
}

pub fn get_client(s: &str) -> Result<Client> {
    let client = if let Some(u) = env_variables::for_url(s) {
        request::Client::builder()?
            .proxy(request::Proxy::all(u.as_str())?)
            .build()?
    } else {
        request::Client::new()?
    };
    Ok(client)
}