Skip to content

Instantly share code, notes, and snippets.

View nderkach's full-sized avatar
✏️

Nikolay Derkach nderkach

✏️
View GitHub Profile
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)]
])
}
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 {
[
{
"action": {
"type": "block"
},
"trigger": {
"url-filter": "webkit.svg"
}
}
]
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
/// Initiates HK queries for new data based on the given type
///
/// - parameter type: `HKObjectType` which has new data avilable.
private func handleSample(_ sample: HKSample) {
switch sample.sampleType {
case HKObjectType.categoryType(forIdentifier: .sleepAnalysis):
guard let totalTimeAsleep = sample.metadata?["Asleep"] as? Double else {
return
}
private var myAnchor: HKQueryAnchor?
/// Sets up the observer queries for background health data delivery.
///
/// - parameter types: Set of `HKObjectType` to observe changes to.
private func setUpBackgroundDeliveryForDataTypes(types: Set<HKObjectType>) {
for type in types {
guard let sampleType = type as? HKSampleType else { print("ERROR: \(type) is not an HKSampleType"); continue }
if let anchorData = UserDefaults.standard.object(forKey: "anchor") as? Data {
private let healthStore = HKHealthStore()
/// Requests access to all the data types the app wishes to read/write from HealthKit.
/// On success, data is queried immediately and observer queries are set up for background
/// delivery. This is safe to call repeatedly and should be called at least once per launch.
func requestAccessWithCompletion(completion: @escaping AccessRequestCallback) {
guard HKHealthStore.isHealthDataAvailable() else {
debugPrint("Can't request access to HealthKit when it's not supported on the device.")
return
}
import HealthKit
/// Types of data that this app wishes to read from HealthKit.
///
/// - returns: A set of HKObjectType.
private func dataTypesToRead() -> Set<HKObjectType> {
return Set([
HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!, HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!
])
}
@nderkach
nderkach / healthkit.swift
Last active September 14, 2018 17:28
Authorize HealthKit
let healthKitManager = HealthKitManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
healthKitManager.requestAccessWithCompletion() { success, error in
if success{ print("HealthKit access granted") }
else { print("Error requesting access to HealthKit: \(error!)") }
}
}
@nderkach
nderkach / recognizer.py
Last active January 29, 2020 12:46
Facebook photo upload and photo tagging
#!/usr/bin/env python
import requests
import re
import urllib.parse
import sys, os
import json
from requests_toolbelt import MultipartEncoder
BASE_URL = 'https://mbasic.facebook.com'