Permalink
Browse files

Added image property

scan the summary/content of the feed item for first image
  • Loading branch information...
1 parent 9c85382 commit d95e1759b02a6bdd004178c6e26f866578ab747f @lilfaf committed Apr 6, 2012
View
4 .gitignore
@@ -1,2 +1,6 @@
.DS_Store
+
+RSSKit/RSSImageParser.h
+
+RSSKit/RSSImageParser.m
View
1 RSSKit.xcodeproj/project.pbxproj
@@ -283,6 +283,7 @@
BF3A6F83152C100A0020B7C5 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
View
6 RSSKit.xcodeproj/xcuserdata/louis.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -8,13 +8,13 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "RSSKit/RSSParser.m"
- timestampString = "355318573.088516"
+ timestampString = "355363919.531243"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "202"
endingLineNumber = "202"
- landmarkName = "@implementation RSSParser"
- landmarkType = "3">
+ landmarkName = "-connectionDidFinishLoading:"
+ landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>
View
2 RSSKit/RSSEntry.h
@@ -22,6 +22,7 @@
NSString *copyright;
RSSAttachedMedia *attachedMedia;
NSString *author;
+ NSString *image;
}
@property (nonatomic, retain) NSString *title;
@@ -35,6 +36,7 @@
@property (nonatomic, retain) NSString *copyright;
@property (nonatomic, retain) RSSAttachedMedia *attachedMedia;
@property (nonatomic, retain) NSString *author;
+@property (nonatomic, retain) NSString *image;
@end
View
2 RSSKit/RSSEntry.m
@@ -22,6 +22,7 @@ @implementation RSSEntry
@synthesize copyright;
@synthesize attachedMedia;
@synthesize author;
+@synthesize image;
- (id) init {
self = [super init];
@@ -43,6 +44,7 @@ - (void) dealloc {
self.copyright = NULL;
self.attachedMedia = NULL;
self.author = NULL;
+ self.image = NULL;
[super dealloc];
}
View
2 RSSKit/RSSParser.h
@@ -70,7 +70,7 @@
@property (nonatomic, readonly) BOOL failed;
@property (nonatomic, readonly) BOOL successful;
-// init RSSKit
+// init parser
- (id) initWithUrl:(NSString *)theUrl asynchronous:(BOOL)sync;
- (id) initWithUrl:(NSString *)theUrl;
View
38 RSSKit/RSSParser.m
@@ -90,7 +90,7 @@ - (void) parse {
// create request
contentUrl = [[NSURL alloc] initWithString:self.url];
request = [[NSMutableURLRequest alloc] initWithURL:contentUrl
- cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
+ cachePolicy:NSURLRequestReloadRevalidatingCacheData
timeoutInterval:60];
// download the feed
@@ -334,16 +334,19 @@ - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)element namespac
if (term) {
// Atom 1.0
[entry.categories addObject:term];
+
} else {
// RSS 2.0
[entry.categories addObject:text];
}
} else if ([tagPath isEqualToString:@"/rss/channel/item/comments"] || [tagPath isEqualToString:@""]) {
entry.comments = text;
- } else if ([tagPath isEqualToString:@"/rss/channel/item/author"] || [tagPath isEqualToString:@"/feed/entry/author/name"]) {
+ } else if ([tagPath isEqualToString:@"/rss/channel/item/author"] || [tagPath isEqualToString:@"/feed/entry/author/name"] || [tagPath isEqualToString:@"/rss/channel/item/dc:creator"]) {
entry.author = text;
} else if ([tagPath isEqualToString:@"/feed/entry/content"] || [tagPath isEqualToString:@"/rss/channel/item/content:encoded"]) {
- entry.content = text;
+ entry.content = text;
+ // scan for first image in content html
+ entry.image = [self processImage:entry.content ? entry.content : entry.summary];
} else if ([tagPath isEqualToString:@"/rss/channel/item/enclosure"]) {
RSSAttachedMedia *media = [[RSSAttachedMedia alloc] init];
media.url = [attributes objectForKey:@"url"];
@@ -379,5 +382,34 @@ - (void) parser:(NSXMLParser *)parser foundCDATA:(NSData *)data {
[string release];
}
+- (NSString *) processImage:(NSString *)htmlString {
+
+ NSScanner *theScanner;
+ NSString *text = nil;
+
+ theScanner = [NSScanner scannerWithString: htmlString];
+
+ // find start of tag
+ [theScanner scanUpToString: @"<img src=\"" intoString: NULL];
+ if ([theScanner isAtEnd] == NO) {
+ NSInteger newLoc = [theScanner scanLocation] + 10;
+ [theScanner setScanLocation: newLoc];
+
+ // find end of tag
+ [theScanner scanUpToString: @"\"" intoString: &text];
+ }
+
+ // uri contain .jpg ?
+ NSString *format = @".jpg";
+ NSRange range = [text rangeOfString : format];
+
+ if (range.location != NSNotFound) {
+ // found it!
+ return text;
+ }
+
+ return nil;
+}
+
@end

0 comments on commit d95e175

Please sign in to comment.