-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEPFavoritesDownloadOperation.m
112 lines (81 loc) · 3.41 KB
/
EPFavoritesDownloadOperation.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
//
// EPFavoritesDownloadOperation.m
// Degrees of Tweetdom
//
// Created by Simone Manganelli on 3/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "EPFavoritesDownloadOperation.h"
@implementation EPFavoritesDownloadOperation
- (id)initWithStatusDelegate:(id)delegate;
{
statusDelegate = delegate;
return [self init];
}
- (id)init;
{
if (self = [super init]) {
twitterHandle = nil;
}
return self;
}
- (void)dealloc
{
[twitterHandle release];
[super dealloc];
}
- (void)setTwitterHandle:(NSString *)newTwitterHandle;
{
twitterHandle = [newTwitterHandle retain];
}
- (void)main;
{
NSString *fullNonPaginatedURL = [NSString stringWithFormat:@"http://twitter.com/%@/favourites",twitterHandle];
NSString *fileLocationPath = [[NSString stringWithFormat:@"~/Library/Caches/Degrees of Tweetdom/Favorites/%@.plist",twitterHandle] stringByExpandingTildeInPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileLocationPath]) {
// the favorites have already been downloaded, so they don't need to be downloaded again
// in the future, this block should check the modification date and recache the file if it's a week old or so
} else {
// main part of the download operation
NSMutableArray *arrayOfFavorites = [[NSMutableArray alloc] init];
NSError *favoritesListError = nil;
int pageNum = 1;
BOOL noMoreFavorites = NO;
while (! noMoreFavorites) {
NSString *URLString = [fullNonPaginatedURL stringByAppendingString:[NSString stringWithFormat:@"?page=%d",pageNum]];
NSXMLDocument *pageOfTwentyFriends = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:URLString]
options:NSXMLDocumentTidyHTML
error:&favoritesListError];
//NSLog(@"%@",pageOfTwentyFriends);
NSError *xPathError = nil;
NSArray *favoriteLiNodeArray = [pageOfTwentyFriends nodesForXPath:@".//span[@class='status-body']" error:&xPathError];
//NSLog(@"%@",favoriteLiNodeArray);
if (xPathError) NSLog(@"%@",xPathError);
xPathError = nil;
if ([favoriteLiNodeArray count] < 20) noMoreFavorites = YES;
// there could be fewer than 20 favorites more, but more than 0, so we still want to process
NSXMLNode *currentNode = nil;
for (currentNode in favoriteLiNodeArray) {
NSXMLNode *bodyNode = [[currentNode nodesForXPath:@".//span[@class='entry-content']" error:&xPathError] objectAtIndex:0];
if (xPathError) NSLog(@"%@",xPathError);
xPathError = nil;
NSString *favoriteBody = [bodyNode stringValue];
//NSLog(@"favoriteBody = %@",favoriteBody);
NSXMLNode *authorNode = [[currentNode nodesForXPath:@".//a[@class='screen-name']" error:&xPathError] objectAtIndex:0];
if (xPathError) NSLog(@"%@",xPathError);
xPathError = nil;
NSString *favoriteAuthorLink = [[(NSXMLElement *)authorNode attributeForName:@"href"] stringValue];
NSString *favoriteAuthor = [[favoriteAuthorLink componentsSeparatedByString:@"http://twitter.com/"] objectAtIndex:1];
//NSLog(@"favoriteAuthor = %@",favoriteAuthor);
NSDictionary *favoriteDict = [NSDictionary dictionaryWithObjectsAndKeys:favoriteBody,@"favoriteBody",favoriteAuthor,@"favoriteAuthor",nil];
[arrayOfFavorites addObject:favoriteDict];
}
pageNum++;
[pageOfTwentyFriends release];
}
[arrayOfFavorites writeToFile:fileLocationPath
atomically:YES];
[arrayOfFavorites release];
}
}
@end