File: RSSFeed%2BFetching.m

package info (click to toggle)
rsskit 0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 572
  • sloc: objc: 2,041; makefile: 48; sh: 4
file content (358 lines) | stat: -rw-r--r-- 9,486 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*  -*-objc-*-
 *
 *  GNUstep RSS Kit
 *  Copyright (C) 2006 Guenther Noack
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation, in version 2.1
 *  of the License
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#import "RSSFeed+Fetching.h"
#import "RSSFeedProtocol.h"
#import "DublinCore.h"
#import "GNUstep.h"

// #define DEBUG 1


#import "DOMParser.h"

#import "FeedParser.h"
#import "Atom03Parser.h"
#import "Atom10Parser.h"
#import "RSS10Parser.h"
#import "RSS20Parser.h"

NSString *const RSSFeedFetchedNotification = @"RSSFeedFetchedNotification";
NSString *const RSSFeedWillFetchNotification = @"RSSFeedWillFetchNotification";
NSString *const RSSFeedFetchFailedNotification = @"RSSFeedFetchFailedNotification";

#define URI_ATOM10              @"http://www.w3.org/2005/Atom"
#define URI_PURL_CONTENT        @"http://purl.org/rss/1.0/modules/content/"
#define URI_PODCAST             @"http://www.itunes.com/dtds/podcast-1.0.dtd"
#define URI_PURL_CONTENT        @"http://purl.org/rss/1.0/modules/content/"
#define URI_PODCAST             @"http://www.itunes.com/dtds/podcast-1.0.dtd"
#define URI_PURL_DUBLINCORE     @"http://purl.org/dc/elements/1.1/"



@interface RSSFeed (PrivateFetching)
-(NSData*) fetchDataFromURL: (NSURL*) myURL;
-(enum RSSFeedError) fetchWithData: (NSData*)data;

-(enum RSSFeedError) parseATOM03WithRootNode: (XMLNode*) root;
-(enum RSSFeedError) parseATOM10WithRootNode: (XMLNode*) root;
-(enum RSSFeedError) parseRSS10WithRootNode: (XMLNode*) root;
-(enum RSSFeedError) parseRSS20WithRootNode: (XMLNode*) root;

- (enum RSSFeedError) setError: (enum RSSFeedError) err;
@end




@implementation RSSFeed (PrivateFetching)
/**
 * URL client 
 */
- (void) URL: (NSURL *) sender 
         resourceDidFailedLoadingWithReason: (NSString *) reason
{
  /* Make sure it is ours */
  if (sender != feedURL) 
    return;

  NSLog(@"URL %@ failed loading because of %@", sender, reason);
  [self setError: RSSFeedErrorMalformedURL];
  [cacheData setLength: 0]; /* Clean up cache */
  status = RSSFeedIsIdle;
  [[NSNotificationCenter defaultCenter]
       postNotificationName: RSSFeedFetchFailedNotification
       object: self
       userInfo: [NSDictionary dictionaryWithObject: reason forKey: @"Reason"]];
}

- (void) URL: (NSURL *) sender
         resourceDataDidBecomeAvailable: (NSData *) newBytes
{
  /* Make sure it is ours */
  if (sender != feedURL) 
    return;

  if (cacheData == nil) {
    ASSIGN(cacheData, [NSMutableData data]);
  }
  [cacheData appendData: newBytes];
}

- (void) URLResourceDidFinishLoading: (NSURL *) sender
{
  /* Make sure it is ours */
  if (sender != feedURL) 
    return;

  NSLog(@"%@ finished loading %@", self, sender);
  
  if ((cacheData == nil) && [cacheData length] == 0) {
    NSLog(@"No Data");
  }
  [self fetchWithData: cacheData];
  status = RSSFeedIsIdle;
  
  /* Clean up cache */
  [cacheData setLength: 0];
  NSLog(@"Process Done");
}

/**
 * Fetches the feed from the URL which is stored in the myURL
 * argument
 */
-(NSData*) fetchDataFromURL: (NSURL*) myURL
{
   NSData* data;
   
   if (myURL == nil) {
       [self setError: RSSFeedErrorMalformedURL];
   }
   
   data = [myURL resourceDataUsingCache: NO];
   
   if (data == nil) {
       [self setError: RSSFeedErrorServerNotReachable];
   }
   
   return [[data retain] autorelease];
}




// parse ATOM 1.0
-(enum RSSFeedError) parseATOM10WithRootNode: (XMLNode*) root
{
  FeedParser* parser = [Atom10Parser parserWithDelegate: self];
  [parser parseWithRootNode: root];
  return RSSFeedErrorNoError;
}

// parse ATOM 0.3
-(enum RSSFeedError) parseATOM03WithRootNode: (XMLNode*) root
{
  FeedParser* parser = [Atom03Parser parserWithDelegate: self];
  [parser parseWithRootNode: root];
  return RSSFeedErrorNoError;
}

// parse RSS 2.0
-(enum RSSFeedError) parseRSS20WithRootNode: (XMLNode*) root
{
  FeedParser* parser = [RSS20Parser parserWithDelegate: self];
  [parser parseWithRootNode: root];
  return RSSFeedErrorNoError;
}

// parse RSS 1.0
-(enum RSSFeedError) parseRSS10WithRootNode: (XMLNode*) root
{
  FeedParser* parser = [RSS10Parser parserWithDelegate: self];
  [parser parseWithRootNode: root];
  return RSSFeedErrorNoError;
}



/**
 * @private
 * Uses the feed contained in data instead of the URL.
 */
-(enum RSSFeedError) fetchWithData: (NSData*)data
{ 
  NSString* rssVersion;
  
  NSXMLParser* parser;
  XMLNode* root;
  XMLNode* document;
  int i;
  
  parser = AUTORELEASE([[NSXMLParser alloc] initWithData: data]);
  
  document = AUTORELEASE([[XMLNode alloc]
			   initWithName: nil
			   namespace: nil
			   attributes: nil
			   parent: nil]);
  
  [parser setDelegate: document];
  [parser setShouldProcessNamespaces: YES]; 
  
  if ([parser parse] == NO)
    {
      return [self setError: RSSFeedErrorMalformedRSS];
    }
  
  root = [document firstChildElement]; // finds the root node
  
  if (clearFeedBeforeFetching == YES)
    {
      status = RSSFeedIsIdle;
      [self clearArticles];
    }
  
  
  // FIXME: Catch errors here which are returned from parsing methods!
  if ([[root name] isEqualToString: @"RDF"]) // RSS 1.0 detected
    {
      rssVersion = @"RSS 1.0";
      [self parseRSS10WithRootNode: root];
    }
  else if ([[root name] isEqualToString: @"rss"] &&
	   [[[root attributes] objectForKey: @"version"]
	     isEqualToString: @"2.0"]) // RSS 2.0 detected
    {
      rssVersion = @"RSS 2.0";
      [self parseRSS20WithRootNode: root];
    }
  else if ([[root name] isEqualToString: @"rss"] &&
	   [[[root attributes] objectForKey: @"version"]
	     isEqualToString: @"0.91"]) // RSS 0.91 detected
    {
      rssVersion = @"RSS 0.91";
      NSLog(@"WARNING: RSS 0.91 support is a *hack* at the moment");
      [self parseRSS20WithRootNode: root];
    }
  else if ([[root name] isEqualToString: @"feed"] &&
	   [[root namespace] isEqualToString: URI_ATOM10]) // ATOM 1.0
    {
      rssVersion = @"ATOM 1.0";
      [self parseATOM10WithRootNode: root];
    }
  else if ([[root name] isEqualToString: @"feed"] &&
	   [[[root attributes] objectForKey: @"version"]
	     isEqualToString: @"0.3"])   // ATOM 0.3 detected
    {
      rssVersion = @"ATOM 0.3";
      [self parseATOM03WithRootNode: root];      
    }
  else
    {
      NSLog(@"Failed to decide RSS version");
      rssVersion = @"Malformed RSS?";
      status = RSSFeedIsIdle;
      [[NSNotificationCenter defaultCenter]
          postNotificationName: RSSFeedFetchFailedNotification
                        object: self
       userInfo: [NSDictionary dictionaryWithObject: @"Malformed RSS"
                                             forKey: @"Reason"]];
      return [self setError: RSSFeedErrorMalformedRSS];
    }
  
  // make sure all articles know their parent feed
  for (i=0; i<[articles count]; i++) {
      // We're in a RSSFeed, so we can assume this is a RSSArticle object.
      [(RSSArticle*)[articles objectAtIndex: i] setFeed: self];
  }
  
  [[NSNotificationCenter defaultCenter]
          postNotificationName: RSSFeedFetchedNotification
                        object: self];
  
  status = RSSFeedIsIdle;
  return [self setError: RSSFeedErrorNoError];
}



// sets the error for the feed (see RSSFeed.h)
-(enum RSSFeedError) setError: (enum RSSFeedError) err
{
  lastError = err;
  return err;
}

@end




@implementation RSSFeed (Fetching)



/**
 * Returns the last error.
 * Guaranteed to return the last fetching result.
 */
-(enum RSSFeedError) lastError
{
  return lastError;
}

/**
 * Fetches the feed from its feed URL, parses it and adds the found
 * articles to the list of articles contained in this feed (if they
 * are new).
 */
-(enum RSSFeedError) fetch
{
   NSData* data;
   
   status = RSSFeedIsFetching;
   
   // no errors at first :-)
   [self setError: RSSFeedErrorNoError];
   [[NSNotificationCenter defaultCenter] 
                      postNotificationName: RSSFeedWillFetchNotification
                      object: self];
   
   data = [self fetchDataFromURL: feedURL];
   
   status = RSSFeedIsIdle;
   
   return [self fetchWithData: data];
}


- (void) fetchInBackground
{
  if (feedURL == nil) 
  {
    [self setError: RSSFeedErrorMalformedURL];
    return;
  }
  
  if (status == RSSFeedIsFetching)
  {
    // FIXME: need an error for repeated loading.
    // GN: Be careful. When you assign an error to that feed while it
    //     is actually already being fetched in the background, the
    //     thread that fetches the data in the background will probably
    //     set the feed's error code, too. You will never know which
    //     fetching process set the error message.
    return;
  }
  
  status = RSSFeedIsFetching;
  [self setError: RSSFeedErrorNoError];
  [[NSNotificationCenter defaultCenter] 
                       postNotificationName: RSSFeedWillFetchNotification
                       object: self];
  
  [feedURL loadResourceDataNotifyingClient: self usingCache: NO];
}

@end