-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPFavoriteFinder.m
131 lines (99 loc) · 4.3 KB
/
EPFavoriteFinder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// EPFavoriteFinder.m
// Degrees of Tweetdom
//
// Created by Simone Manganelli on 3/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "EPFavoriteFinder.h"
#import "EPFavoritesDownloadOperation.h"
#import "EPFriendAndFollowerGetter.h"
#import "EPOperationQueue.h"
@implementation EPFavoriteFinder
- (id)initWithStatusDelegate:(id)delegate;
{
statusDelegate = delegate;
[self createCacheFolder];
theMainOperationQueue = [[EPOperationQueue alloc] init];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"operationQueueCreated" object:theMainOperationQueue];
friendAndFollowerGetterInstance = [[EPFriendAndFollowerGetter alloc] initWithStatusDelegate:statusDelegate
andOperationQueue:theMainOperationQueue
];
return [self init];
}
- (void)dealloc;
{
NSLog(@"no, wut");
[friendAndFollowerGetterInstance release];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"operationQueueDeleted" object:theMainOperationQueue];
[theMainOperationQueue release];
[super dealloc];
}
- (void)createCacheFolder;
{
// make sure the folder where the Friends XML files are cached actually exists
NSError *folderCreationError = nil;
BOOL creationSucceeded = [[NSFileManager defaultManager] createDirectoryAtPath:[@"~/Library/Caches/Degrees of Tweetdom/Favorites/" stringByExpandingTildeInPath]
withIntermediateDirectories:YES
attributes:nil
error:&folderCreationError];
if (! creationSucceeded) [statusDelegate addStatusLine:[folderCreationError localizedDescription]];
}
- (NSDictionary *)findFavoritesAuthoredByTwitterer:(NSString *)twitterHandle;
{
NSMutableDictionary *authoredFavoritesDict = [[NSMutableDictionary alloc] init];
NSArray *friendsOfTwitterer = [friendAndFollowerGetterInstance getFriendsForTwitterer:twitterHandle];
NSString *currentFriend = nil;
for (currentFriend in friendsOfTwitterer) {
[self downloadFollowersForTwitterer:currentFriend];
}
currentFriend = nil;
for (currentFriend in friendsOfTwitterer) {
[statusDelegate addStatusLine:[NSString stringWithFormat:@"Searching through favorites of %@",currentFriend]];
NSArray *arrayOfFavorites = [self getFavoritesForTwitterer:currentFriend];
NSDictionary *currentFavorite = nil;
for (currentFavorite in arrayOfFavorites) {
NSString *currentFavoriteBody = [currentFavorite objectForKey:@"favoriteBody"];
NSString *currentFavoriteAuthor = [currentFavorite objectForKey:@"favoriteAuthor"];
if ([currentFavoriteAuthor isEqualToString:twitterHandle]) {
// we've found one of the twitterer's tweets that has been favorited!
NSMutableArray *favoritersArray = [authoredFavoritesDict objectForKey:currentFavoriteBody];
if (favoritersArray == nil) {
// first time we've encountered this tweet being faved
favoritersArray = [NSMutableArray arrayWithObject:currentFriend];
[authoredFavoritesDict setObject:favoritersArray forKey:currentFavoriteBody];
} else {
// this tweet has been faved by someone else
[favoritersArray addObject:currentFriend];
}
}
}
}
//NSLog(@"%@",authoredFavoritesDict);
[theMainOperationQueue cancelAllOperations];
return [authoredFavoritesDict autorelease];
}
- (NSArray *)getFavoritesForTwitterer:(NSString *)twitterHandle;
{
NSString *XMLFileLocationPath = [[NSString stringWithFormat:@"~/Library/Caches/Degrees of Tweetdom/Favorites/%@.plist",twitterHandle] stringByExpandingTildeInPath];
int timeoutCounter = 0;
int maxTimeout = 60; // seconds to wait for an XML file to download
while (! [[NSFileManager defaultManager] fileExistsAtPath:XMLFileLocationPath]) {
timeoutCounter++;
sleep(1);
if (timeoutCounter >= maxTimeout) {
[statusDelegate addStatusLine:[NSString stringWithFormat:@"Timed out waiting for %@'s Friend XML file to download.",twitterHandle]];
break;
}
}
NSArray *arrayOfFavorites = [NSArray arrayWithContentsOfFile:XMLFileLocationPath];
return arrayOfFavorites;
}
- (void)downloadFollowersForTwitterer:(NSString *)twitterHandle;
{
EPFavoritesDownloadOperation *downloadOperation = [[EPFavoritesDownloadOperation alloc] initWithStatusDelegate:self];
[downloadOperation setTwitterHandle:twitterHandle];
[theMainOperationQueue addOperation:downloadOperation];
[downloadOperation release];
}
@end