import Command import Console import Crypto import Fluent import FluentSQLite struct DownloadVideosCommand: Command, Service { var arguments: [CommandArgument] { return [] } var options: [CommandOption] { return [] } var help: [String] { return [] } func run(using ctx: CommandContext) throws -> Future { return ctx.container.withPooledConnection(to: .sqlite) { db in return Video .query(on: db) .group(.or) { or in or.filter(\Video.status == 0) or.filter(\Video.status == 1) } .all() .flatMap { videos in let downloadTasks: [LazyFuture] = videos.map { video in return { return Process.asyncExecute( "youtube-dl", ["-x", "--audio-format", "mp3", video.url], on: ctx.container) { output in switch output { case .stdout(let data), .stderr(let data): if let string = String(data: data, encoding: .utf8) { print(string) } } }.flatMap { returnCode -> Future