This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func sendMessageToTelegram(withText text: String) { | |
| // Send messages on telegram | |
| let apiToken = "YOUR_TELEGRAM_TOKEN" | |
| let chatId = "@YOUR_TELEGRAM_CHAT_NAME" | |
| let strUrl = String(format: "https://api.telegram.org/bot%@/sendMessage?chat_id=%@&text=%@", apiToken, chatId, text.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!) | |
| Alamofire.request(strUrl).responseJSON { response in | |
| if let json = response.result.value { | |
| print("JSON: \(json)") // serialized json response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "action": { | |
| "type": "block" | |
| }, | |
| "trigger": { | |
| "url-filter": "webkit.svg" | |
| } | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static func fetchAndParseTrackerList(completion: @escaping (BlockerList) -> Void ) { | |
| var parsedLines: [String] = [] | |
| // Fetch JSON file containing the list of trackers to block | |
| Alamofire.request(Constants.trackerListURL).responseData { response in | |
| if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) { | |
| let lines = utf8Text.components(separatedBy: .newlines) | |
| for line in lines { | |
| if line.starts(with: "#") || line.isEmpty { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typealias BlockerList = [[String: [String: String]]] | |
| private static func generateBlacklistJSON(from trackerList: [String]) -> BlockerList { | |
| var blacklist: BlockerList = [] | |
| for tracker in trackerList { | |
| blacklist.append([ | |
| "action": ["type": "block"], | |
| "trigger": ["url-filter": String(format: "https?://(www.)?%@.*", tracker)] | |
| ]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ContentBlockerRequestHandler: NSObject, NSExtensionRequestHandling { | |
| func beginRequest(with context: NSExtensionContext) { | |
| let attachment = NSItemProvider(contentsOf: Bundle.main.url(forResource: "blockerList", withExtension: "json"))! | |
| let item = NSExtensionItem() | |
| item.attachments = [attachment] | |
| context.completeRequest(returningItems: [item], completionHandler: nil) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| guard let jsonData = try? JSONSerialization.data(withJSONObject: trackers, options: .prettyPrinted) else { | |
| return | |
| } | |
| let documentFolder = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.trackerblocker") | |
| guard let jsonURL = documentFolder?.appendingPathComponent(Constants.blockerListFilename) else { | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SafariServices | |
| SFContentBlockerManager.reloadContentBlocker(withIdentifier: "ch.derka.TrackerBlocker.TrackerBlockerExtension", completionHandler: { error in | |
| // Handle error | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let documentFolder = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.trackerblocker") | |
| guard let jsonURL = documentFolder?.appendingPathComponent(Constants.blockerListFilename) else { | |
| return | |
| } | |
| let attachment = NSItemProvider(contentsOf: jsonURL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sales Path | |
| The car manufacturer Honda holds their distribution system in the form of a tree (not necessarily binary). The root is the company itself, and every node in the tree represents a car distributor that receives cars from the parent node and ships them to its children nodes. The leaf nodes are car dealerships that sell cars direct to consumers. In addition, every node holds an integer that is the cost of shipping a car to it. | |
| Take for example the tree below: | |
| https://www.pramp.com/img/content/img_01.png | |
| A path from Honda’s factory to a car dealership, which is a path from the root to a leaf in the tree, is called a Sales Path. The cost of a Sales Path is the sum of the costs for every node in the path. For example, in the tree above one Sales Path is 0→3→0→10, and its cost is 13 (0+3+0+10). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # def consecutive(arr): | |
| # arr = sorted(arr) | |
| # max_so_far_start = 0 | |
| # max_so_far_end = 1 | |
| # max_range = (0, 1) | |
| # cur_i = 1 | |
| # while cur_i <= len(arr): | |
| # if cur_i != len(arr) and arr[cur_i] == arr[cur_i-1]+1: |