Skip to content

Instantly share code, notes, and snippets.

@ricardopereira
Last active August 20, 2018 22:35
Show Gist options
  • Save ricardopereira/a870bd591c034b01db80602904869d77 to your computer and use it in GitHub Desktop.
Save ricardopereira/a870bd591c034b01db80602904869d77 to your computer and use it in GitHub Desktop.
Download AppStore high res app screens
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "appstore-screens",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/tid-kijyun/Kanna.git", from: "4.0.0"),
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.0.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "appstore-screens",
dependencies: ["Kanna", "Alamofire"]),
]
)
import Foundation
import Kanna
import Alamofire
guard let url = URL(string: CommandLine.arguments[1]) else {
print("You should pass as argument an App Store URL."); exit(0)
}
Alamofire.request(url).responseString(queue: nil, encoding: .utf8) { response in
guard let html = response.result.value else {
print("No response."); exit(0)
}
guard let doc = try? HTML(html: html, encoding: .utf8) else {
print("Response isn't HTML."); exit(0)
}
var index = 0
var downloaded: [String] = []
let elements = doc.css("picture.we-artwork--screenshot-platform-iphone")
for item in elements {
guard let srcset = item.css("source").first?["srcset"] else {
continue
}
let srcsets = srcset.split(separator: ",")
guard let x3image = srcsets.last else { //x3
continue
}
let screenPath = x3image[x3image.startIndex..<x3image.index(x3image.endIndex, offsetBy: -3)]
guard let screenURL = URL(string: String(screenPath)) else {
fatalError("AppStore screenshot URL is invalid: \(screenPath)")
}
index += 1
let filename = screenURL.deletingPathExtension().lastPathComponent + String(index) + "." + screenURL.pathExtension
print("Start download:", filename)
let destination: DownloadRequest.DownloadFileDestination = { tempURL, response in
let documentsURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let fileURL = documentsURL.appendingPathComponent(filename)
print("Saved at:", fileURL)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(screenURL, to: destination).response { response in
if let error = response.error {
print("Download of \"\(filename)\" failed with:", error)
}
else {
print("Download of \"\(filename)\" has completed with no error")
}
// Resize
let task = Process()
task.launchPath = "/usr/local/bin/nconvert"
task.arguments = ["-out", "jpeg", "-ratio", "-resize", "321", "0", filename]
task.launch()
task.waitUntilExit()
if task.terminationStatus != 0 {
print("Resize using `nconvert` as failed.")
exit(0)
}
downloaded.append(screenURL.lastPathComponent)
if downloaded.count == index {
print("Finished.")
exit(0)
}
}
}
}
RunLoop.main.run()
@ricardopereira
Copy link
Author

Usage:

./appstore-screens "https://itunes.apple.com/pt/app/ampme/id986905979?mt=8"

Command dependency: NConvert for image resize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment