Skip to content

Commit

Permalink
Use SwiftArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
fcanas committed Jan 29, 2022
1 parent d5ce2e5 commit 528048a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 75 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"revision": "2835209970afdaaaf137fd7073d9c0ab2beb2905",
"version": "0.2.1"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "83b23d940471b313427da226196661856f6ba3e0",
"version": "0.4.4"
}
}
]
},
Expand Down
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/fcanas/HLSCore.git", from: "0.2.1"),
.package(url: "https://github.com/fcanas/FFCLog.git", from:"0.1.0")
.package(url: "https://github.com/fcanas/FFCLog.git", from:"0.1.0"),
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.4.3")),
],
targets: [
.executableTarget(name: "scrape", dependencies: ["scrapeLib", "FFCLog"]),
.executableTarget(name: "scrape", dependencies: [
"scrapeLib",
"FFCLog",
.product(name: "ArgumentParser", package: "swift-argument-parser")
]),
.target(name: "scrapeLib", dependencies: ["HLSCore", "FFCLog"]),
.testTarget(name: "scrapeTests", dependencies: ["scrapeLib"])
]
Expand Down
120 changes: 47 additions & 73 deletions Sources/scrape/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,63 @@
// Copyright © 2016 Fabian Canas. All rights reserved.
//

import ArgumentParser
import Foundation
import scrapeLib
import FFCLog

/// Argument Parsing
extension String: Error {}

let processInfo = ProcessInfo()

var args: [String] = Array(processInfo.arguments[1..<processInfo.arguments.count])

enum Option: String, CaseIterable {
case playlistOnly = "-p"
case verbose = "-v"
case help = "-h"
}

extension Option {
var usageDescription: String {
switch self {
case .playlistOnly:
return "download only playlist files"
case .verbose:
return "verbose output"
case .help:
return "show this help"
struct Scrape: ParsableCommand {

@Argument
var sourceURL: String

@Argument(help: "A destination directory that will be the root of the downloaded HLS directory structure and files.")
var destinationDirectory: String

@Flag(name:.shortAndLong, help: "Downloads only playlist files, no media files.")
var playlistOnly: Bool = false

@Flag(name:.shortAndLong, help: "Prints verbose diagnostic output to the console.")
var verbose: Bool = false

func run() throws {

guard
let sourceURL = URL(localOrRemoteString: sourceURL)
else {
throw "Source URL"
}
}
}

func printUsage() {
print("usage: scrape \(Option.allCases.reduce("", { $0 + "[\($1.rawValue)] " }))input_url output_url")
print(" input_url a remote URL to an m3u8 HLS playlist")
print(" output_url a local path where the HLS stream should be saved")
Option.allCases.forEach { (opt) in
print(" \(opt.rawValue) \(opt.usageDescription)")
}
}

guard args.count >= 2 else {
print("At least 2 arguments needed.")
printUsage()
exit(EXIT_FAILURE)
}

guard
let destinationURL = URL(localOrRemoteString: destinationDirectory)
else {
throw "Source URL appears invalid"
}

let urlFilter: ((URL) -> Bool)?

if playlistOnly {
urlFilter = { url in
url.fileExtension.hasPrefix("m3u")
}
} else {
urlFilter = nil
}

let logLevel: Level = verbose ? .all : .error

guard let destinationURL = URL(localOrRemoteString: args.popLast()!) else {
print("Destination path appears invalid")
printUsage()
exit(EXIT_FAILURE)
}

guard let sourceURL = URL(localOrRemoteString: args.popLast()!) else {
print("Source URL appears invalid")
printUsage()
exit(EXIT_FAILURE)
}
let downloader = Downloader(destination: destinationURL,
ingestFunction: HLS.ingestResource,
logger: FFCLog(thresholdLevel: logLevel))
urlFilter.map { downloader.urlFilter = $0 }

var urlFilter: ((URL) -> Bool)?
var logLevel: Level = .error
downloader.downloadResource(sourceURL)

while let arg = args.popLast() {
guard let option = Option(rawValue: arg) else {
print("Unrecognized option: \(arg)")
printUsage()
exit(EXIT_FAILURE)
}
switch option {
case .playlistOnly:
urlFilter = { url in
url.fileExtension.hasPrefix("m3u")
}
case .verbose:
logLevel = .all
case .help:
printUsage()
exit(EXIT_SUCCESS)
downloader.group.wait()
}
}

let downloader = Downloader(destination: destinationURL,
ingestFunction: HLS.ingestResource,
logger: FFCLog(thresholdLevel: logLevel))
urlFilter.map { downloader.urlFilter = $0 }

downloader.downloadResource(sourceURL)

downloader.group.wait()
Scrape.main()

0 comments on commit 528048a

Please sign in to comment.