Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create class to represent enclosures #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions Classes/MWFeedItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,8 @@
NSDate *updated; // Date the item was updated if available
NSString *summary; // Description of item
NSString *content; // More detailed content (if available)

// Enclosures: Holds 1 or more item enclosures (i.e. podcasts, mp3. pdf, etc)
// - NSArray of NSDictionaries with the following keys:
// url: where the enclosure is located (NSString)
// length: how big it is in bytes (NSNumber)
// type: what its type is, a standard MIME type (NSString)
NSArray *enclosures;

NSArray *enclosures; // Array of feed enclosures (MWFeedItemEnclosure)

}

@property (nonatomic, copy) NSString *identifier;
Expand Down
44 changes: 44 additions & 0 deletions Classes/MWFeedItemEnclosure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// MWFeedItemEnclosure.m
// MWFeedParser
//
// Copyright (c) 2010 Michael Waterfall
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// 1. The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// 2. This Software cannot be used to archive or collect data such as (but not
// limited to) that of events, news, experiences and activities, for the
// purpose of any concept relating to diary/journal keeping.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import <Foundation/Foundation.h>

@interface MWFeedItemEnclosure : NSObject <NSCoding> {

NSString *url; // Enclosure url
NSString *type; // Mime-type enclosure
NSInteger length; // File size in bytes

}

@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *type;
@property (nonatomic) NSInteger length;

@end
69 changes: 69 additions & 0 deletions Classes/MWFeedItemEnclosure.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// MWFeedItemEnclosure.m
// MWFeedParser
//
// Copyright (c) 2010 Michael Waterfall
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// 1. The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// 2. This Software cannot be used to archive or collect data such as (but not
// limited to) that of events, news, experiences and activities, for the
// purpose of any concept relating to diary/journal keeping.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import "MWFeedItemEnclosure.h"

@implementation MWFeedItemEnclosure

@synthesize url, type, length;

#pragma mark NSObject

- (NSString *)description {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"%@: ", NSStringFromClass([self class])];
if (url) [string appendFormat:@"“%@”", url];
if (type) [string appendFormat:@" (%@)", type];
if (length) [string appendFormat:@" %i bytes", length];
return [string autorelease];
}

- (void)dealloc {
[url release];
[type release];
[super dealloc];
}

#pragma mark NSCoding

- (id)initWithCoder:(NSCoder *)decoder {
if ((self = [super init])) {
url = [[decoder decodeObjectForKey:@"url"] retain];
type = [[decoder decodeObjectForKey:@"type"] retain];
length = [decoder decodeIntegerForKey:@"length"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
if (url) [encoder encodeObject:url forKey:@"url"];
if (type) [encoder encodeObject:type forKey:@"type"];
if (length) [encoder encodeInteger:length forKey:@"length"];
}

@end
1 change: 1 addition & 0 deletions Classes/MWFeedParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#import <Foundation/Foundation.h>
#import "MWFeedInfo.h"
#import "MWFeedItem.h"
#import "MWFeedItemEnclosure.h"

// Debug Logging
#if 0 // Set to 1 to enable debug logging
Expand Down
36 changes: 15 additions & 21 deletions Classes/MWFeedParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -877,49 +877,42 @@ - (void)setUrl:(NSURL *)value {
#pragma mark -
#pragma mark Misc

// Create an enclosure NSDictionary from enclosure (or link) attributes
// Create an enclosure (MWFeedItemEnclosure) from enclosure (or link) attributes
- (BOOL)createEnclosureFromAttributes:(NSDictionary *)attributes andAddToItem:(MWFeedItem *)currentItem {

// Create enclosure
NSDictionary *enclosure = nil;
NSString *encURL = nil, *encType = nil;
NSNumber *encLength = nil;
MWFeedItemEnclosure *enclosure = nil;
if (attributes) {
switch (feedType) {
case FeedTypeRSS: { // http://cyber.law.harvard.edu/rss/rss.html#ltenclosuregtSubelementOfLtitemgt
// <enclosure>
encURL = [attributes objectForKey:@"url"];
encType = [attributes objectForKey:@"type"];
encLength = [NSNumber numberWithLongLong:[((NSString *)[attributes objectForKey:@"length"]) longLongValue]];
enclosure = [[MWFeedItemEnclosure alloc] init];
enclosure.url = (NSString *)[attributes objectForKey:@"url"];
enclosure.type = [attributes objectForKey:@"type"];
enclosure.length = [((NSString *)[attributes objectForKey:@"length"]) integerValue];
break;
}
case FeedTypeRSS1: { // http://www.xs4all.nl/~foz/mod_enclosure.html
// <enc:enclosure>
encURL = [attributes objectForKey:@"rdf:resource"];
encType = [attributes objectForKey:@"enc:type"];
encLength = [NSNumber numberWithLongLong:[((NSString *)[attributes objectForKey:@"enc:length"]) longLongValue]];
enclosure = [[MWFeedItemEnclosure alloc] init];
enclosure.url = (NSString *)[attributes objectForKey:@"rdf:resource"];
enclosure.type = [attributes objectForKey:@"enc:type"];
enclosure.length = [((NSString *)[attributes objectForKey:@"enc:length"]) integerValue];
break;
}
case FeedTypeAtom: { // http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rel_attribute
// <link rel="enclosure" href=...
if ([[attributes objectForKey:@"rel"] isEqualToString:@"enclosure"]) {
encURL = [attributes objectForKey:@"href"];
encType = [attributes objectForKey:@"type"];
encLength = [NSNumber numberWithLongLong:[((NSString *)[attributes objectForKey:@"length"]) longLongValue]];
enclosure = [[MWFeedItemEnclosure alloc] init];
enclosure.url = (NSString *)[attributes objectForKey:@"href"];
enclosure.type = [attributes objectForKey:@"type"];
enclosure.length = [((NSString *)[attributes objectForKey:@"length"]) integerValue];
}
break;
}
default: break;
}
}
if (encURL) {
NSMutableDictionary *e = [[NSMutableDictionary alloc] initWithCapacity:3];
[e setObject:encURL forKey:@"url"];
if (encType) [e setObject:encType forKey:@"type"];
if (encLength) [e setObject:encLength forKey:@"length"];
enclosure = [NSDictionary dictionaryWithDictionary:e];
[e release];
}

// Add to item
if (enclosure) {
Expand All @@ -928,6 +921,7 @@ - (BOOL)createEnclosureFromAttributes:(NSDictionary *)attributes andAddToItem:(M
} else {
currentItem.enclosures = [NSArray arrayWithObject:enclosure];
}
[enclosure release];
return YES;
} else {
return NO;
Expand Down
6 changes: 6 additions & 0 deletions MWFeedParser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
4CC8960B119F0CED00ED61B6 /* MWFeedItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CC89608119F0CED00ED61B6 /* MWFeedItem.m */; };
4CC8960F119F0CFB00ED61B6 /* NSString+XMLEntities.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CC8960E119F0CFB00ED61B6 /* NSString+XMLEntities.m */; };
4CC89612119F0D0C00ED61B6 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CC89611119F0D0C00ED61B6 /* RootViewController.m */; };
D97132801436D6600037DA50 /* MWFeedItemEnclosure.m in Sources */ = {isa = PBXBuildFile; fileRef = D971327F1436D6600037DA50 /* MWFeedItemEnclosure.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -58,6 +59,8 @@
4CC89610119F0D0C00ED61B6 /* RootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
4CC89611119F0D0C00ED61B6 /* RootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* MWFeedParser-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "MWFeedParser-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
D971327E1436D6600037DA50 /* MWFeedItemEnclosure.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MWFeedItemEnclosure.h; sourceTree = "<group>"; };
D971327F1436D6600037DA50 /* MWFeedItemEnclosure.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MWFeedItemEnclosure.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -162,6 +165,8 @@
4CC89606119F0CED00ED61B6 /* MWFeedInfo.m */,
4CC89607119F0CED00ED61B6 /* MWFeedItem.h */,
4CC89608119F0CED00ED61B6 /* MWFeedItem.m */,
D971327E1436D6600037DA50 /* MWFeedItemEnclosure.h */,
D971327F1436D6600037DA50 /* MWFeedItemEnclosure.m */,
);
name = MWFeedParser;
sourceTree = "<group>";
Expand Down Expand Up @@ -270,6 +275,7 @@
4C65F2EB12085ACE00606CFC /* GTMNSString+HTML.m in Sources */,
4C65F318120863F800606CFC /* NSString+HTML.m in Sources */,
4C661F44125E3F2D007F1792 /* NSDate+InternetDateTime.m in Sources */,
D97132801436D6600037DA50 /* MWFeedItemEnclosure.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 12 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ MWFeedParser is an Objective-C framework for downloading and parsing RSS (1.* an
- Updated date (the date the item was updated, if available)
- Summary (brief description of item)
- Content (detailed item content, if available)
- Enclosures (i.e. podcasts, mp3, pdf, etc)
- Identifier (an item's guid/id)

#### Enclosures
- URL
- Filetype
- Size

If you use MWFeedParser on your iPhone/iPad app then please do let me know, I'd love to check it out :)

***Important:*** This free software is provided under the MIT licence (X11 license) with the addition of the following condition:
Expand Down Expand Up @@ -79,7 +83,7 @@ Once parsing has been initiated, the delegate will receive the feed data as it i
- (void)feedParserDidFinish:(MWFeedParser *)parser; // Parsing complete or stopped at any time by `stopParsing`
- (void)feedParser:(MWFeedParser *)parser didFailWithError:(NSError *)error; // Parsing failed

`MWFeedInfo` and `MWFeedItem` contains properties (title, link, summary, etc.) that will hold the parsed data. View `MWFeedInfo.h` and `MWFeedItem.h` for more information.
`MWFeedInfo`, `MWFeedItem` and `MWFeedItemEnclosure` contains properties (title, link, summary, etc.) that will hold the parsed data. View `MWFeedInfo.h`, `MWFeedItem.h` and `MWFeedItemEnclosure.h` for more information.

***Important:*** There are some occasions where feeds do not contain some information, such as titles, links or summaries. Before using any data, you should check to see if that data exists:

Expand Down Expand Up @@ -109,9 +113,14 @@ Here is a list of the available properties for feed info and item objects:
- `item.updated` (`NSDate`)
- `item.summary` (`NSString`)
- `item.content` (`NSString`)
- `item.enclosures` (`NSArray` of `NSDictionary` with keys `url`, `type` and `length`)
- `item.enclosures` (`NSArray` of `MWFeedItemEnclosure`)
- `item.identifier` (`NSString`)

#### MWFeedItemEnclosure
- `enclosure.url` (`NSURL`)
- `enclosure.type` (`NSString`)
- `enclosure.length` (`NSInteger`)


## Using the data

Expand Down