downloader.rs 9.67 KB
Newer Older
1 2 3
extern crate zip;
extern crate walkdir;

John Moon's avatar
John Moon committed
4 5 6 7
use std::process::Command;
use std::path::{Path, PathBuf};
use rocket::request::Form;
use rocket::response::NamedFile;
8
use rocket_contrib::templates::Template;
John Moon's avatar
John Moon committed
9 10
use url::Url;
use std::fs;
11 12
use walkdir::WalkDir;
use std::io::prelude::*;
John Moon's avatar
John Moon committed
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

use id3::{Tag, Version};

/// Template context passed to Tera template HTML file
#[derive(Serialize)]
pub struct DownloaderContext {
    path: String,
    fname: String,
    fsize: String,
    error: String
}

#[derive(Debug, FromFormValue, Clone, PartialEq)]
pub enum Format {
    MP3, WAV, M4A, AAV, MP4, WEBM
}

impl Format {
    pub fn ext(&self) -> &str {
        match self {
            Format::MP3 => "mp3",
            Format::WAV => "wav",
            Format::M4A => "m4a",
            Format::AAV => "aav",
            Format::MP4 => "mp4",
            Format::WEBM => "webm"
        }
    }

    pub fn is_video(&self) -> bool {
        match self {
            Format::MP3 => false,
            Format::WAV => false,
            Format::M4A => false,
            Format::AAV => false,
            Format::MP4 => true,
            Format::WEBM => true
        }
    }

    pub fn is_audio(&self) -> bool {
        !self.is_video()
    }
}

/// The QuoteForm sends the user's guess
#[derive(FromForm, Debug)]
pub struct DownloaderForm {
    url: String,
62
    playlist: bool,
John Moon's avatar
John Moon committed
63 64 65 66 67 68 69
    format: Format,
    title: String,
    artist: String,
    album: String,
    year: String,
}

70 71 72 73 74 75
pub fn zip_dir(dir: &str, basename: &str) -> Result<String, String> {
    let zip_path = format!("{}.zip", dir);
    let zip_file = match std::fs::File::create(&zip_path) {
        Ok(z) => z,
        Err(s) => return Err(s.to_string()),
    };
John Moon's avatar
John Moon committed
76

77 78 79 80
    let mut zip = zip::ZipWriter::new(zip_file);
    if zip.add_directory(basename, zip::write::FileOptions::default()).is_err() {
        return Err("Error adding directory to zip archive".to_string());
    }
John Moon's avatar
John Moon committed
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
    // Add each file in the playlist directory to the archive
    let mut buf = Vec::new();
    for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
        if entry.path().is_file() {
            if let Ok(filename) = entry.path().strip_prefix(Path::new("./youtube-dl-outputs")) {
                if let Some(filename) = filename.to_str() {
                    println!("{}", filename);
                    if zip.start_file(filename, zip::write::FileOptions::default()).is_ok() {
                        if let Ok(mut f) = std::fs::File::open(entry.path()) {
                            if f.read_to_end(&mut buf).is_ok() {
                                if zip.write(&buf).is_err() {
                                    return Err("Failed to flush output zip archive".to_string());
                                }
                            } else {
                                return Err("Failed to read individual file".to_string());
                            }
                            buf.clear();
                        } else {
                            return Err("Failed to open individual file".to_string());
                        }
                    } else {
                        return Err("Failed to start output file".to_string());
                    }
                } else {
                    return Err("Failed to convert file name to string".to_string());
                }
John Moon's avatar
John Moon committed
108
            } else {
109
                return Err("Failed to strip output prefix from file".to_string());
John Moon's avatar
John Moon committed
110 111
            }
        }
112 113
    }

John Moon's avatar
John Moon committed
114
    Ok(zip_path)
115 116
}

117 118 119 120 121 122 123 124 125 126
pub fn download_and_convert(form: &DownloaderForm) -> Result<String, String> {

    let output_fmt = format!("./youtube-dl-outputs/{}", {
        if form.playlist {
            "%(playlist_title)s/%(title)s.%(ext)s"
        } else {
            "%(title)s.%(ext)s"
        }
    });

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    if let Ok(url) = Url::parse(&form.url) {
        let url = &url.to_string();
        let fname = {
            let cmd = {
                if form.playlist {
                    Command::new("youtube-dl")
                        .arg(url)
                        .arg("--get-filename")
                        .arg("--restrict-filenames")
                        .arg("--yes-playlist")
                        .arg("--playlist-items")
                        .arg("1")
                        .arg("-o")
                        .arg("%(playlist_title)s")
                        .output()
                } else {
                    Command::new("youtube-dl")
                        .arg(url)
145
                        .arg("--restrict-filenames")
146
                        .arg("--get-filename")
147 148
                        .arg("-o")
                        .arg(&output_fmt)
149 150 151
                        .output()
                }
            };
John Moon's avatar
John Moon committed
152

153 154 155 156 157 158 159 160 161 162
            if let Ok(output) = cmd {
                if !output.status.success() {
                    return Err(format!("youtube-dl couldn't parse a video from the link \"{}\"", url));
                }

                if let Ok(fname) = String::from_utf8(output.stdout) {
                    fname
                } else {
                    return Err("Failed to parse youtube-dl filename with UTF8".to_string());
                }
John Moon's avatar
John Moon committed
163
            } else {
164
                return Err("Failed to run youtube-dl".to_string());
John Moon's avatar
John Moon committed
165
            }
166 167 168 169 170
        };

        let fname_no_fmt = {
            if form.playlist {
                &fname.trim()
John Moon's avatar
John Moon committed
171 172 173
            } else if let Some(fname) = Path::new(&fname).file_stem() {
                if let Some(fname_fmt) = fname.to_str() {
                    fname_fmt
174
                } else {
John Moon's avatar
John Moon committed
175
                    return Err("Failed to convert file name to string".to_string());
176
                }
John Moon's avatar
John Moon committed
177 178
            } else {
                return Err("Failed to parse file name stem".to_string());
179 180
            }
        };
John Moon's avatar
John Moon committed
181

182
        let fname = format!("./youtube-dl-outputs/{}.{}", fname_no_fmt, if form.playlist { ".zip" } else { form.format.ext() });
John Moon's avatar
John Moon committed
183

184 185 186 187 188 189 190 191
        let cmd_out = Command::new("youtube-dl")
            .arg(url)
            .arg(if form.playlist { "--yes-playlist" } else { "--no-playlist" })
            .arg("--restrict-filenames")
            .arg(if form.format.is_audio() { "--extract-audio" } else { "--prefer-free-formats" })
            .arg(if form.format.is_audio() { "--audio-format" } else { "--format" })
            .arg(form.format.ext())
            .arg("-o")
192
            .arg(&output_fmt)
193
            .output();
John Moon's avatar
John Moon committed
194

195 196 197 198
        if let Ok(cmd) = cmd_out {
            if !cmd.status.success() {
                return Err(format!("youtube-dl failed to download from link \"{}\"", url));
            }
John Moon's avatar
John Moon committed
199

200 201
            // Add MP3 tag if it's an MP3 file
            if form.format == Format::MP3 && !form.playlist {
202
                add_tag(&fname, &form);
203
            }
John Moon's avatar
John Moon committed
204

205 206 207 208
            // If it's a playlist, zip up the output folder
            if form.playlist {
                zip_dir(&format!("./youtube-dl-outputs/{}", fname_no_fmt), fname_no_fmt)
            } else {
209
                Ok(fname)
210 211 212
            }
        } else {
            Err("Failed to run youtube-dl".to_string())
John Moon's avatar
John Moon committed
213 214
        }
    } else {
215
        Err(format!("URL \"{}\" is invalid", form.url))
John Moon's avatar
John Moon committed
216 217 218 219
    }
}

pub fn get_context_from_path(path: &str) -> Result<DownloaderContext, String> {
John Moon's avatar
John Moon committed
220
    if let Some(fname) = Path::new(&path).file_name() {
John Moon's avatar
John Moon committed
221
        if let Some(fname) = fname.to_str() {
222
            if let Ok(meta) = fs::metadata(&path) {
John Moon's avatar
John Moon committed
223
                let fsize_kbytes = meta.len() / 1_000;
John Moon's avatar
John Moon committed
224 225 226
                Ok(DownloaderContext {
                    path: path.to_string(),
                    fname: fname.to_string(),
John Moon's avatar
John Moon committed
227
                    fsize: format!("{} KB", fsize_kbytes),
John Moon's avatar
John Moon committed
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
                    error: "".to_string()
                })
            } else {
                Err("Failed to get filesize from output".to_string())
            }
        } else {
            Err("Failed to get output filename".to_string())
        }
    } else {
        Err("Failed to get output filename".to_string())
    }

}

pub fn add_tag(file: &str, form: &DownloaderForm) {
    let mut tag = Tag::new();
    tag.set_artist(form.artist.clone());
    tag.set_album(form.album.clone());
    tag.set_title(form.title.clone());

    // In case it's an invalid year, just skip it
    if let Ok(year) = form.year.clone().parse::<i32>() {
        tag.set_year(year);
    }

    // If we fail to write to the file, no big deal
    if tag.write_to_path(file, Version::Id3v24).is_err() {
        println!("error writing MP3 tags to file");
    }
}

#[get("/downloader")]
pub fn downloader_get() -> Template {
    let ctx = DownloaderContext {
        path: "".to_string(),
        fname: "".to_string(),
        fsize: "".to_string(),
        error: "".to_string()
    };

    Template::render("downloader", &ctx)
}

/// Route to deal with the downloader post data
#[post("/downloader", data = "<form>")]
pub fn downloader_post(form: Form<DownloaderForm>) -> Template {
    let mut error = "".to_string();
    let mut ctx: Option<DownloaderContext> = {
276 277 278 279 280 281 282
        match download_and_convert(&form) {
            Ok(p) => {
                match get_context_from_path(&p) {
                    Ok(c) => Some(c),
                    Err(s) => {
                        error = s;
                        None
John Moon's avatar
John Moon committed
283 284
                    }
                }
285 286 287 288
            },
            Err(e) => {
                error = e;
                None
John Moon's avatar
John Moon committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
            }
        }
    };

    // In an error case, report the error to the user
    if ctx.is_none() {
        ctx = Some(DownloaderContext {
            path: "".to_string(),
            fname: "".to_string(),
            fsize: "".to_string(),
            error
        });
    }

    Template::render("downloader", &ctx)
}

/// Route to load static files
#[get("/youtube-dl-outputs/<file..>")]
pub fn download_file(file: PathBuf) -> Option<NamedFile> {
    NamedFile::open(Path::new("youtube-dl-outputs/").join(file)).ok()
}