Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions Contacts/ABPerson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@ import AddressBook

extension ABPerson {
func firstName() -> String {
return self.valueForProperty(kABFirstNameProperty) as String? ?? ""
return self.valueForProperty(kABFirstNameProperty) as! String? ?? ""
}

func lastName() -> String {
return self.valueForProperty(kABLastNameProperty) as String? ?? ""
return self.valueForProperty(kABLastNameProperty) as! String? ?? ""
}

func emails() -> ABMultiValue? {
return self.valueForProperty(kABEmailProperty) as ABMultiValue?
return self.valueForProperty(kABEmailProperty) as! ABMultiValue?
}

func setImageDataFromURL(optionalURL: NSURL?) {
if let url = optionalURL {
let data = NSData(contentsOfURL: url)
if data != nil {
self.setImageData(data)
}
}
}
}
4 changes: 2 additions & 2 deletions Contacts/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ extension String {

CC_MD5(str!, strLen, result)

var hash = NSMutableString()
let hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}

result.destroy()

return String(format: hash)
return String(format: hash as String)
}
}
42 changes: 35 additions & 7 deletions Contacts/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,44 @@ import Foundation
import AddressBook

let addressbook = ABAddressBook()
let people = addressbook.people() as [ABPerson]
let people = addressbook.people() as! [ABPerson]
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

for person in people {
if let emails = person.emails() {
println("Finding Gravatar for \(person.firstName()) \(person.lastName())...")

for i in 0..<emails.count() {
let email = Email(address: emails.valueAtIndex(i) as String)
if person.imageData() == nil {
if let emails = person.emails() {
print("Finding Gravatar for \(person.firstName()) \(person.lastName())...")

person.setImageDataFromURL(email.gravatarURL())
for i in 0..<emails.count() {
let email = Email(address: emails.valueAtIndex(i) as! String)
var urlSession : NSURLSession!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
urlSession = NSURLSession(configuration: configuration)

var task = urlSession.dataTaskWithURL(email.gravatarURL()!) {
data, response, error in
let statusCode = (response as? NSHTTPURLResponse)?.statusCode ?? -1
if statusCode == 200 {
print("Found valid Gravatar for \(email.address)")
person.setImageData(data)
}
dispatch_group_leave(group)
}
dispatch_group_enter(group)
task.resume()
}
}
}
}

dispatch_group_notify(group, queue) {
sleep(5) // allow for setImageData to complete
print("All task completed.")
exit(0)
}

while true {
sleep(1)
}
5 changes: 4 additions & 1 deletion Gravatar2Contacts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@
5C2C4E6A19FC86310049C01A /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0610;
LastSwiftMigration = 0710;
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = "Chad Pytel";
TargetAttributes = {
5C2C4E7119FC86310049C01A = {
Expand Down Expand Up @@ -174,6 +176,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ the email address for the contact.

To run it, open the project in XCode and Run.

The program will only query Gravatars for contacts without image. As a result, already set images (manual or previous runs) will be kept.
If no Gravatar is found for a contact it does not change the image.

However, if any Gravatar is found for a contact, it will replace the existing
image. In other words, we always use the latest image from Gravatar.

This has the side effect that if you change an image for a contact, run this
program, and they have a different Gravatar, your custom image will be
overwritten.

### Known Issues

* If a contact has multiple email address with Gravatars associated with them,
Expand Down