Skip to content

Instantly share code, notes, and snippets.

@ivanbruel
Last active November 5, 2017 07:04
Show Gist options
  • Save ivanbruel/b3d7ac8f71df88a3cd8581835773a7b6 to your computer and use it in GitHub Desktop.
Save ivanbruel/b3d7ac8f71df88a3cd8581835773a7b6 to your computer and use it in GitHub Desktop.
Buy iPhone
//
// main.swift
// BuyPhone
//
// Created by Ivan Bruel on 04/11/2017.
// Copyright © 2017 Ivan Bruel. All rights reserved.
//
import Foundation
//////////////////////////////////////////////////////////////////
// Now, the actual usage of notifications becomes very simple :)
func presentNotification(message: String) {
Process.launchedProcess(launchPath: "/usr/bin/osascript", arguments: ["-e", "display notification \"\(message)\""])
}
let silver = "MQAR2LL/A"
let black = "MQAQ2LL/A"
let iPhone = URL(string: "https://www.apple.com/shop/retail/pickup-message?pl=true&cppart=TMOBILE/US&parts.0=MQAR2LL/A&parts.1=MQAQ2LL/A&location=94040")!
func findIphone(url: URL) -> String? {
guard let data = try? Data(contentsOf: url),
let json = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)) as? [String: Any],
let body = json["body"] as? [String: Any],
let stores = body["stores"] as? [[String: Any]] else {
return nil
}
for store in stores {
guard let storeName = store["storeName"] as? String,
let availability = store["partsAvailability"] as? [String: Any],
let blackIphone = availability[black] as? [String: Any],
let silverIphone = availability[silver] as? [String: Any] else {
return nil
}
if let silverPickup = silverIphone["pickupDisplay"] as? String,
silverPickup == "available" {
return "❤️❤️❤️❤️❤️ Available \(storeName): Silver"
}
if let blackPickup = blackIphone["pickupDisplay"] as? String,
blackPickup == "available" {
return "❤️❤️❤️❤️❤️ Available \(storeName): Black"
}
}
return nil
}
while true {
if let message = findIphone(url: iPhone) {
presentNotification(message: message)
break
}
print("\(Date().timeIntervalSince1970) None available yet")
sleep(30)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment