summaryrefslogtreecommitdiff
path: root/QSDeliciousAPIProvider.m
blob: d97d1d23f9078d22a000f72419a53ea089e89ad6 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//  QSDeliciousAPIProvider.m
//  QSDeliciousPlugIn
//

#import "QSDeliciousAPIProvider.h"
#import "SocialSite.h"
#import <QSCore/QSCore.h>

@implementation QSDeliciousAPIProvider

- (instancetype)initWithSite:(SocialSite)site {
    self = [super init];
    if (self) {
        _site = site;
        _posts = [NSMutableArray array];
    }
    return self;
}

- (BOOL)canHandleSite:(SocialSite)site username:(NSString *)username password:(NSString *)password host:(NSString *)host {
    return (site == self.site) && 
           username.length > 0 && 
           password.length > 0 &&
           (site != SocialSiteLinkding); // This provider doesn't handle Linkding
}

- (SocialSite)supportedSite {
    return self.site;
}

- (NSString *)providerName {
    return [SocialSiteHelper displayNameForSite:self.site];
}

- (NSString *)apiURLForSite:(SocialSite)site {
    switch (site) {
        case SocialSiteDelicious:
            return @"api.del.icio.us/v1";
        case SocialSiteMagnolia:
            return @"ma.gnolia.com/api/mirrord/v1";
        case SocialSitePinboard:
            return @"api.pinboard.in/v1";
        default:
            return nil;
    }
}

- (NSURL *)requestURLForSite:(SocialSite)site username:(NSString *)username password:(NSString *)password host:(NSString *)host {
    NSString *apiURL = [self apiURLForSite:site];
    if (!apiURL) return nil;
    
    NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@%@/posts/all?", username, password, apiURL];
    return [NSURL URLWithString:urlString];
}

- (NSData *)cachedBookmarkDataForSite:(SocialSite)site username:(NSString *)username {
    NSString *siteURL = [SocialSiteHelper siteURLForSite:site];
    NSString *cachePath = [QSApplicationSupportSubPath([NSString stringWithFormat:@"Caches/%@/", siteURL], NO) stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xml", username]];
    return [NSData dataWithContentsOfFile:cachePath];
}

- (void)cacheBookmarkData:(NSData *)data forSite:(SocialSite)site username:(NSString *)username {
    NSString *siteURL = [SocialSiteHelper siteURLForSite:site];
    NSString *cachePath = [QSApplicationSupportSubPath([NSString stringWithFormat:@"Caches/%@/", siteURL], YES) stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xml", username]];
    [data writeToFile:cachePath atomically:NO];
}

- (NSString *)tagURLType {
    return [NSString stringWithFormat:@"tag.%@", [SocialSiteHelper reversedSiteURLForSite:self.site]];
}

- (NSArray *)fetchBookmarksForSite:(SocialSite)site username:(NSString *)username password:(NSString *)password host:(NSString *)host includeTags:(BOOL)includeTags {
    
    // Try cached data first
    NSData *data = [self cachedBookmarkDataForSite:site username:username];
    
    // If no cached data, fetch from API
    if (![data length]) {
        NSURL *requestURL = [self requestURLForSite:site username:username password:password host:host];
        if (!requestURL) return @[];
        
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:requestURL
                                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                              timeoutInterval:60.0];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
        [theRequest setValue:@"Quicksilver (Blacktree,MacOSX)" forHTTPHeaderField:@"User-Agent"];
        
        NSError *error;
        data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:&error];
        
        if (error) {
            NSLog(@"Error fetching bookmarks: %@", error.localizedDescription);
            return @[];
        }
        
        // Cache the data
        [self cacheBookmarkData:data forSite:site username:username];
    }
    
    // Parse XML data
    NSXMLParser *postParser = [[NSXMLParser alloc] initWithData:data];
    [postParser setDelegate:self];
    
    self.posts = [NSMutableArray arrayWithCapacity:1];
    [postParser parse];
    
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:1];
    NSMutableSet *tagSet = [NSMutableSet set];
    
    // Create bookmark objects
    for (NSDictionary *post in self.posts) {
        QSObject *newObject = [self objectForPost:post];
        if (newObject) {
            [objects addObject:newObject];
            
            // Collect tags if requested
            if (includeTags) {
                NSString *tagString = [post objectForKey:@"tag"];
                if (tagString.length > 0) {
                    [tagSet addObjectsFromArray:[tagString componentsSeparatedByString:@" "]];
                }
            }
        }
    }
    
    // Create tag objects if requested
    if (includeTags) {
        for (NSString *tag in tagSet) {
            if (tag.length > 0) {
                QSObject *tagObject = [QSObject makeObjectWithIdentifier:[NSString stringWithFormat:@"[%@ tag]:%@", [self providerName], tag]];
                [tagObject setObject:tag forType:[self tagURLType]];
                [tagObject setObject:username forMeta:[NSString stringWithFormat:@"%@.username", [SocialSiteHelper reversedSiteURLForSite:site]]];
                [tagObject setName:tag];
                [tagObject setPrimaryType:[self tagURLType]];
                [objects addObject:tagObject];
            }
        }
    }
    
    return objects;
}

- (NSArray *)fetchBookmarksForTag:(NSString *)tag site:(SocialSite)site username:(NSString *)username password:(NSString *)password host:(NSString *)host {
    NSData *data = [self cachedBookmarkDataForSite:site username:username];
    if (!data) return @[];
    
    NSXMLParser *postParser = [[NSXMLParser alloc] initWithData:data];
    [postParser setDelegate:self];
    self.posts = [NSMutableArray arrayWithCapacity:1];
    [postParser parse];
    
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:1];
    
    for (NSDictionary *post in self.posts) {
        NSString *postTags = [post objectForKey:@"tag"];
        if ([postTags rangeOfString:tag].location != NSNotFound) {
            QSObject *newObject = [self objectForPost:post];
            if (newObject) {
                [objects addObject:newObject];
            }
        }
    }
    
    return objects;
}

- (QSObject *)objectForPost:(NSDictionary *)post {
    QSObject *newObject = [QSObject makeObjectWithIdentifier:[post objectForKey:@"hash"]];
    [newObject setObject:[post objectForKey:@"href"] forType:QSURLType];
    [newObject setName:[post objectForKey:@"description"]];
    [newObject setDetails:[post objectForKey:@"extended"]];
    [newObject setPrimaryType:QSURLType];
    return newObject;
}

#pragma mark - NSXMLParserDelegate

- (void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"post"] && attributeDict) {
        [self.posts addObject:attributeDict];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    // Implementation if needed
}

@end