Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/MWFeedItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *author;
@property (nonatomic, copy) NSArray *enclosures;
@property (nonatomic, copy) NSDictionary *customProperties;


@end
3 changes: 3 additions & 0 deletions Classes/MWFeedParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ typedef enum { FeedTypeUnknown, FeedTypeRSS, FeedTypeRSS1, FeedTypeAtom } FeedTy
// Whether parsing is in progress
@property (nonatomic, readonly, getter=isParsing) BOOL parsing;

// Defines custom item keys
@property (nonatomic, copy) NSArray *customKeys;

#pragma mark Public Methods

// Init MWFeedParser with a URL string
Expand Down
21 changes: 20 additions & 1 deletion Classes/MWFeedParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
// New item
MWFeedItem *newItem = [[MWFeedItem alloc] init];
self.item = newItem;
self.currentCustomProperties = [[NSMutableDictionary alloc] init];
// Return
return;

Expand Down Expand Up @@ -583,6 +583,19 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
// Remove newlines and whitespace from currentText
NSString *processedText = [currentText stringByRemovingNewLinesAndWhitespace];

void (^fillCustomKeysWithBasePath)(NSString *) = ^ (NSString *path){
[self.customKeys enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop) {
NSString *path = [NSString stringWithFormat:@"/rss/channel/item/%@", key];
if ([currentPath isEqualToString: path]) {
if (processedText.length > 0) {
self.currentCustomProperties[key] = processedText;
} else if (currentElementAttributes.count) {
self.currentCustomProperties[key] = currentElementAttributes;
}
}
}];
};

// Process
switch (feedType) {
case FeedTypeRSS: {
Expand All @@ -603,6 +616,7 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
else if ([currentPath isEqualToString:@"/rss/channel/item/pubDate"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822]; processed = YES; }
else if ([currentPath isEqualToString:@"/rss/channel/item/enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
else if ([currentPath isEqualToString:@"/rss/channel/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
else if (self.customKeys.count) { fillCustomKeysWithBasePath(@"/rss/channel/item/%@"); }
}

// Info
Expand Down Expand Up @@ -630,6 +644,7 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
else if ([currentPath isEqualToString:@"/rdf:RDF/item/dc:creator"]) { if (processedText.length > 0) item.author = processedText; processed = YES; }
else if ([currentPath isEqualToString:@"/rdf:RDF/item/dc:date"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
else if ([currentPath isEqualToString:@"/rdf:RDF/item/enc:enclosure"]) { [self createEnclosureFromAttributes:currentElementAttributes andAddToItem:item]; processed = YES; }
else if (self.customKeys.count) { fillCustomKeysWithBasePath(@"/rdf:RDF/item/%@"); }
}

// Info
Expand Down Expand Up @@ -658,6 +673,7 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
else if ([currentPath isEqualToString:@"/feed/entry/dc:creator"]) { if (processedText.length > 0) item.author = processedText; processed = YES; }
else if ([currentPath isEqualToString:@"/feed/entry/published"]) { if (processedText.length > 0) item.date = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
else if ([currentPath isEqualToString:@"/feed/entry/updated"]) { if (processedText.length > 0) item.updated = [NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC3339]; processed = YES; }
else if (self.customKeys.count) { fillCustomKeysWithBasePath(@"/feed/entry/%@"); }
}

// Info
Expand All @@ -681,6 +697,9 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
if (((feedType == FeedTypeRSS || feedType == FeedTypeRSS1) && [qName isEqualToString:@"item"]) ||
(feedType == FeedTypeAtom && [qName isEqualToString:@"entry"])) {

if(self.currentCustomProperties.count) {
item.customProperties = self.currentCustomProperties;
}
// Dispatch item to delegate
[self dispatchFeedItemToDelegate];

Expand Down
2 changes: 1 addition & 1 deletion Classes/MWFeedParser_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@property (nonatomic, strong) MWFeedItem *item;
@property (nonatomic, strong) MWFeedInfo *info;
@property (nonatomic, copy) NSString *pathOfElementWithXHTMLType;

@property (nonatomic, strong) NSMutableDictionary *currentCustomProperties;
#pragma mark Private Methods

// Parsing Methods
Expand Down