From 528048aa8c1d46152c3d2dd6172d7c26ffdad969 Mon Sep 17 00:00:00 2001 From: Fabian Canas Date: Fri, 28 Jan 2022 22:16:33 -0500 Subject: [PATCH] Use SwiftArgumentParser --- Package.resolved | 9 +++ Package.swift | 9 ++- Sources/scrape/main.swift | 120 +++++++++++++++----------------------- 3 files changed, 63 insertions(+), 75 deletions(-) diff --git a/Package.resolved b/Package.resolved index 4c0c779..4c6e568 100644 --- a/Package.resolved +++ b/Package.resolved @@ -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" + } } ] }, diff --git a/Package.swift b/Package.swift index 158c566..1c3f96e 100644 --- a/Package.swift +++ b/Package.swift @@ -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"]) ] diff --git a/Sources/scrape/main.swift b/Sources/scrape/main.swift index b67eb37..5ac8111 100644 --- a/Sources/scrape/main.swift +++ b/Sources/scrape/main.swift @@ -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..= 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()