Refactoring to v.2.0
This commit is contained in:
@@ -1,101 +1,104 @@
|
||||
//
|
||||
// RSParsedArticle.m
|
||||
// RSXML
|
||||
// MIT License (MIT)
|
||||
//
|
||||
// Created by Brent Simmons on 12/6/14.
|
||||
// Copyright (c) 2014 Ranchero Software LLC. All rights reserved.
|
||||
// Copyright (c) 2016 Brent Simmons
|
||||
// Copyright (c) 2018 Oleg Geier
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// 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 "RSParsedArticle.h"
|
||||
#import "RSXMLInternal.h"
|
||||
#import "NSString+RSXML.h"
|
||||
|
||||
@interface RSParsedArticle()
|
||||
@property (nonatomic, copy) NSString *internalArticleID;
|
||||
@end
|
||||
|
||||
|
||||
@implementation RSParsedArticle
|
||||
|
||||
|
||||
#pragma mark - Init
|
||||
|
||||
- (instancetype)initWithFeedURL:(NSString *)feedURL {
|
||||
- (instancetype)initWithFeedURL:(NSString *)feedURL dateParsed:(NSDate*)parsed {
|
||||
|
||||
NSParameterAssert(feedURL != nil);
|
||||
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return nil;
|
||||
if (self) {
|
||||
_feedURL = feedURL;
|
||||
_dateParsed = parsed;
|
||||
}
|
||||
|
||||
_feedURL = feedURL;
|
||||
_dateParsed = [NSDate date];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - Unique Article ID
|
||||
|
||||
#pragma mark - Accessors
|
||||
|
||||
/**
|
||||
Article ID will be generated on the first access.
|
||||
*/
|
||||
- (NSString *)articleID {
|
||||
|
||||
if (!_articleID) {
|
||||
_articleID = self.calculatedUniqueID;
|
||||
if (!_internalArticleID) {
|
||||
_internalArticleID = self.calculatedUniqueID;
|
||||
}
|
||||
|
||||
return _articleID;
|
||||
return _internalArticleID;
|
||||
}
|
||||
|
||||
/**
|
||||
Initiate calculation of article id.
|
||||
*/
|
||||
- (void)calculateArticleID {
|
||||
(void)self.articleID;
|
||||
}
|
||||
|
||||
/**
|
||||
@return MD5 hash of @c feedURL @c + @c guid. Or a combination of properties when guid is not set.
|
||||
@note
|
||||
In general, feeds should have guids. When they don't, re-runs are very likely,
|
||||
because there's no other 100% reliable way to determine identity.
|
||||
*/
|
||||
- (NSString *)calculatedUniqueID {
|
||||
|
||||
/*guid+feedID, or a combination of properties when no guid. Then hash the result.
|
||||
In general, feeds should have guids. When they don't, re-runs are very likely,
|
||||
because there's no other 100% reliable way to determine identity.*/
|
||||
|
||||
NSMutableString *s = [NSMutableString stringWithString:@""];
|
||||
NSAssert(self.feedURL != nil, @"Feed URL should always be set!");
|
||||
NSMutableString *s = [NSMutableString stringWithString:self.feedURL];
|
||||
|
||||
NSString *datePublishedTimeStampString = nil;
|
||||
if (self.datePublished) {
|
||||
datePublishedTimeStampString = [NSString stringWithFormat:@"%.0f", self.datePublished.timeIntervalSince1970];
|
||||
}
|
||||
|
||||
if (!RSXMLStringIsEmpty(self.guid)) {
|
||||
if (self.guid.length > 0) {
|
||||
[s appendString:self.guid];
|
||||
}
|
||||
|
||||
else if (!RSXMLStringIsEmpty(self.link) && self.datePublished != nil) {
|
||||
[s appendString:self.link];
|
||||
[s appendString:datePublishedTimeStampString];
|
||||
}
|
||||
|
||||
else if (!RSXMLStringIsEmpty(self.title) && self.datePublished != nil) {
|
||||
[s appendString:self.title];
|
||||
[s appendString:datePublishedTimeStampString];
|
||||
}
|
||||
|
||||
else if (self.datePublished != nil) {
|
||||
[s appendString:datePublishedTimeStampString];
|
||||
|
||||
if (self.link.length > 0) {
|
||||
[s appendString:self.link];
|
||||
} else if (self.title.length > 0) {
|
||||
[s appendString:self.title];
|
||||
}
|
||||
[s appendString:[NSString stringWithFormat:@"%.0f", self.datePublished.timeIntervalSince1970]];
|
||||
}
|
||||
|
||||
else if (!RSXMLStringIsEmpty(self.link)) {
|
||||
else if (self.link.length > 0) {
|
||||
[s appendString:self.link];
|
||||
}
|
||||
|
||||
else if (!RSXMLStringIsEmpty(self.title)) {
|
||||
else if (self.title.length > 0) {
|
||||
[s appendString:self.title];
|
||||
}
|
||||
|
||||
else if (!RSXMLStringIsEmpty(self.body)) {
|
||||
else if (self.body.length > 0) {
|
||||
[s appendString:self.body];
|
||||
}
|
||||
|
||||
NSAssert(!RSXMLStringIsEmpty(self.feedURL), nil);
|
||||
[s appendString:self.feedURL];
|
||||
|
||||
return [s rsxml_md5HashString];
|
||||
}
|
||||
|
||||
- (void)calculateArticleID {
|
||||
|
||||
(void)self.articleID;
|
||||
}
|
||||
#pragma mark - Printing
|
||||
|
||||
- (NSString*)description {
|
||||
return [NSString stringWithFormat:@"{%@ '%@', guid: %@}", [self class], self.title, self.guid];
|
||||
|
||||
Reference in New Issue
Block a user