Skip to content

Instantly share code, notes, and snippets.

@shakked
Created January 24, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakked/db2fefdf37e6d06e5d7c to your computer and use it in GitHub Desktop.
Save shakked/db2fefdf37e6d06e5d7c to your computer and use it in GitHub Desktop.
//
// EngagementCrawler.swift
// Instagrowth
//
// Created by Zachary Shakked on 10/8/15.
// Copyright © 2015 Zachary Shakked. All rights reserved.
//
import UIKit
class EngagementCrawler: NSObject {
//MARK:- Instance Vars
let analyticCrawler : AnalyticCrawler
var likes : [Like]?
var comments : [Comment]?
var followers : Set<User> = []
var engagedFollowers : Set<User> = []
var ghostFollowers : Set<User> = []
var userInteractions : [UserInteraction] = []
var didCrunch : Bool = false
//MARK:- Init
init(analyticCrawler: AnalyticCrawler) {
self.analyticCrawler = analyticCrawler
super.init()
}
//MARK:- Sorts
func leastLikes() -> Set<User> {
let likers : Set<User> = Set(userInteractions.filter({$0.likes.count > 0}).map({ $0.user }))
return followers.subtract(likers)
}
func leastComments() -> Set<User> {
let commenters : Set<User> = Set(userInteractions.filter({$0.comments.count > 0}).map({ $0.user }))
return followers.subtract(commenters)
}
func noLikesOrComments() -> Set<User> {
return followers.subtract(engagedFollowers)
}
//MARK:- Config
func crunch(completion: (Bool) -> (Void)) {
let userInteractionFetcher = UserInteractionFetcher(postSummaries: analyticCrawler.postSummaries, accessToken: analyticCrawler.account.accessToken)
let group = dispatch_group_create()
dispatch_group_enter(group)
userInteractionFetcher.fetchLikes { (succeeded, likes) -> (Void) in
self.likes = succeeded ? likes : []
dispatch_group_leave(group)
}
dispatch_group_enter(group)
userInteractionFetcher.fetchComments { (succeeded, comments) -> (Void) in
self.comments = succeeded ? comments : []
dispatch_group_leave(group)
}
dispatch_group_enter(group)
InstagramQuerier.sharedQuerier.fetchFollowers(analyticCrawler.account.params) { (succeeded, followers: [User]) -> (Void) in
self.followers = succeeded ? Set<User>(followers) : []
dispatch_group_leave(group)
}
dispatch_group_notify(group, dispatch_get_main_queue()) {
guard let likes = self.likes, comments = self.comments else {
return
}
self.configureUserInteractions(likes, comments: comments)
self.configureEngagedUsers()
self.configureGhostFollowers()
self.didCrunch = true
completion(true)
}
}
private func configureUserInteractions(likes: [Like], comments: [Comment]) {
var userInteractions : [String : UserInteraction] = Dictionary<String, UserInteraction>()
guard let likes = self.likes, comments = self.comments else {
return
}
for like in likes {
if let userInteraction = userInteractions[like.user.username] {
userInteraction.addLike(like)
} else {
let userInteraction = UserInteraction(user: like.user)
userInteraction.addLike(like)
userInteractions[like.user.username] = userInteraction
}
}
for comment in comments {
if let from = comment.from {
if let userInteraction = userInteractions[from.username] {
userInteraction.addComment(comment)
} else {
let userInteraction = UserInteraction(user: from)
userInteraction.addComment(comment)
userInteractions[from.username] = userInteraction
}
}
}
self.userInteractions = Array(userInteractions.values)
}
private func configureEngagedUsers() {
guard let comments = comments, likes = likes else {
return
}
for comment in comments {
if let user = comment.from {
engagedFollowers.insert(user)
}
}
for like in likes {
engagedFollowers.insert(like.user)
}
}
private func configureGhostFollowers() {
let engagedFollowers = self.engagedFollowers
let allFollowers = self.followers
let ghostFollowers = allFollowers.subtract(engagedFollowers)
self.ghostFollowers = ghostFollowers
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment