From 7ccb531f0c1c67159bf1be432516a64ec9907b12 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 19 Dec 2017 10:17:09 -0800 Subject: [PATCH] =?UTF-8?q?Parse=20Atom=20enclosures.=20However,=20I=20cou?= =?UTF-8?q?ldn=E2=80=99t=20find=20any=20in=20the=20wild,=20so=20there=20ar?= =?UTF-8?q?e=20no=20tests=20yet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frameworks/RSParser/Feeds/XML/RSAtomParser.m | 52 ++++++++++++++++--- .../RSParser/Feeds/XML/RSParsedArticle.h | 2 + .../RSParser/Feeds/XML/RSParsedArticle.m | 12 +++++ .../RSParser/Feeds/XML/RSParsedEnclosure.h | 1 + Frameworks/RSParser/Feeds/XML/RSRSSParser.m | 12 +---- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Frameworks/RSParser/Feeds/XML/RSAtomParser.m b/Frameworks/RSParser/Feeds/XML/RSAtomParser.m index dd1ec2a1a..7faed75d7 100755 --- a/Frameworks/RSParser/Feeds/XML/RSAtomParser.m +++ b/Frameworks/RSParser/Feeds/XML/RSAtomParser.m @@ -14,6 +14,7 @@ #import #import #import +#import @interface RSAtomParser () @@ -94,11 +95,14 @@ static NSString *kXMLBaseKey = @"xml:base"; static NSString *kXMLLangKey = @"xml:lang"; static NSString *kTextHTMLValue = @"text/html"; static NSString *kRelatedValue = @"related"; +static NSString *kEnclosureValue = @"enclosure"; static NSString *kShortURLValue = @"shorturl"; static NSString *kHTMLValue = @"html"; static NSString *kEnValue = @"en"; static NSString *kTextValue = @"text"; static NSString *kSelfValue = @"self"; +static NSString *kLengthKey = @"length"; +static NSString *kTitleKey = @"title"; static const char *kID = "id"; static const NSInteger kIDLength = 3; @@ -175,6 +179,11 @@ static const NSInteger kTextLength = 5; static const char *kSelf = "self"; static const NSInteger kSelfLength = 5; +static const char *kEnclosure = "enclosure"; +static const NSInteger kEnclosureLength = 10; + +static const char *kLength = "length"; +static const NSInteger kLengthLength = 7; #pragma mark - Parsing @@ -241,28 +250,46 @@ static const NSInteger kSelfLength = 5; - (void)addLink { - NSString *urlString = self.currentAttributes[kHrefKey]; + NSDictionary *attributes = self.currentAttributes; + + NSString *urlString = attributes[kHrefKey]; if (urlString.length < 1) { return; } - NSString *rel = self.currentAttributes[kRelKey]; + RSParsedArticle *article = self.currentArticle; + + NSString *rel = attributes[kRelKey]; if (rel.length < 1) { rel = kAlternateValue; } if (rel == kAlternateValue) { - if (!self.currentArticle.link) { - self.currentArticle.link = urlString; + if (!article.link) { + article.link = urlString; } } else if (rel == kRelatedValue) { - if (!self.currentArticle.permalink) { - self.currentArticle.permalink = urlString; + if (!article.permalink) { + article.permalink = urlString; } } + else if (rel == kEnclosureValue) { + RSParsedEnclosure *enclosure = [self enclosureWithURLString:urlString attributes:attributes]; + [article addEnclosure:enclosure]; + } } +- (RSParsedEnclosure *)enclosureWithURLString:(NSString *)urlString attributes:(NSDictionary *)attributes { + + RSParsedEnclosure *enclosure = [[RSParsedEnclosure alloc] init]; + enclosure.url = urlString; + enclosure.title = attributes[kTitleKey]; + enclosure.mimeType = attributes[kTypeKey]; + enclosure.length = [attributes[kLengthKey] integerValue]; + + return enclosure; +} - (void)addContent { @@ -502,6 +529,14 @@ static const NSInteger kSelfLength = 5; return kAlternateValue; } + if (RSSAXEqualTags(name, kLength, kLengthLength)) { + return kLengthKey; + } + + if (RSSAXEqualTags(name, kTitle, kTitleLength)) { + return kTitleKey; + } + return nil; } @@ -522,11 +557,16 @@ static BOOL equalBytes(const void *bytes1, const void *bytes2, NSUInteger length static const NSUInteger enLength = kEnLength - 1; static const NSUInteger textLength = kTextLength - 1; static const NSUInteger selfLength = kSelfLength - 1; + static const NSUInteger enclosureLength = kEnclosureLength - 1; if (length == alternateLength && equalBytes(bytes, kAlternate, alternateLength)) { return kAlternateValue; } + if (length == enclosureLength && equalBytes(bytes, kEnclosure, enclosureLength)) { + return kEnclosureValue; + } + if (length == textHTMLLength && equalBytes(bytes, kTextHTML, textHTMLLength)) { return kTextHTMLValue; } diff --git a/Frameworks/RSParser/Feeds/XML/RSParsedArticle.h b/Frameworks/RSParser/Feeds/XML/RSParsedArticle.h index 442a47510..a297da6f6 100755 --- a/Frameworks/RSParser/Feeds/XML/RSParsedArticle.h +++ b/Frameworks/RSParser/Feeds/XML/RSParsedArticle.h @@ -28,6 +28,8 @@ @property (nonatomic, nullable) NSDate *dateModified; @property (nonatomic, nonnull) NSDate *dateParsed; +- (void)addEnclosure:(RSParsedEnclosure *_Nonnull)enclosure; + - (void)calculateArticleID; // Optimization. Call after all properties have been set. Call on a background thread. @end diff --git a/Frameworks/RSParser/Feeds/XML/RSParsedArticle.m b/Frameworks/RSParser/Feeds/XML/RSParsedArticle.m index ad3e5fa27..ba39c79fd 100755 --- a/Frameworks/RSParser/Feeds/XML/RSParsedArticle.m +++ b/Frameworks/RSParser/Feeds/XML/RSParsedArticle.m @@ -32,6 +32,18 @@ } +#pragma mark - Enclosures + +- (void)addEnclosure:(RSParsedEnclosure *)enclosure { + + if (self.enclosures) { + self.enclosures = [self.enclosures setByAddingObject:enclosure]; + } + else { + self.enclosures = [NSSet setWithObject:enclosure]; + } +} + #pragma mark - Accessors - (NSString *)articleID { diff --git a/Frameworks/RSParser/Feeds/XML/RSParsedEnclosure.h b/Frameworks/RSParser/Feeds/XML/RSParsedEnclosure.h index b8f0e4cde..8fc9e404d 100644 --- a/Frameworks/RSParser/Feeds/XML/RSParsedEnclosure.h +++ b/Frameworks/RSParser/Feeds/XML/RSParsedEnclosure.h @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) NSString *url; @property (nonatomic) NSInteger length; @property (nonatomic, nullable) NSString *mimeType; +@property (nonatomic, nullable) NSString *title; @end diff --git a/Frameworks/RSParser/Feeds/XML/RSRSSParser.m b/Frameworks/RSParser/Feeds/XML/RSRSSParser.m index 0c6c11d0d..67edc286a 100755 --- a/Frameworks/RSParser/Feeds/XML/RSRSSParser.m +++ b/Frameworks/RSParser/Feeds/XML/RSRSSParser.m @@ -244,18 +244,10 @@ static const NSInteger kEnclosureLength = 10; RSParsedEnclosure *enclosure = [[RSParsedEnclosure alloc] init]; enclosure.url = url; - - NSString *lengthString = attributes[kLengthKey]; - if (lengthString) { - enclosure.length = lengthString.integerValue; - } - + enclosure.length = [attributes[kLengthKey] integerValue]; enclosure.mimeType = attributes[kTypeKey]; - // The RSS spec specifies zero or one enclosures. - // However, the Media RSS namespace allows for more than one. - // We could add support for multiple enclosures at some time in the future. - self.currentArticle.enclosures = [NSSet setWithObject:enclosure]; + [self.currentArticle addEnclosure:enclosure]; } - (NSString *)urlString:(NSString *)s {