2017-06-21 06:18:46 +02:00
|
|
|
//
|
|
|
|
// RSParsedArticle.m
|
2017-06-26 01:32:07 +02:00
|
|
|
// RSParser
|
2017-06-21 06:18:46 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 12/6/14.
|
|
|
|
// Copyright (c) 2014 Ranchero Software LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2017-10-04 22:28:48 +02:00
|
|
|
#import <RSParser/RSParsedArticle.h>
|
|
|
|
#import <RSParser/RSParserInternal.h>
|
|
|
|
#import <RSParser/NSString+RSParser.h>
|
2017-12-19 22:03:05 +01:00
|
|
|
#import <RSParser/RSParsedAuthor.h>
|
|
|
|
#import <RSParser/RSParsedEnclosure.h>
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
@implementation RSParsedArticle
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Init
|
|
|
|
|
|
|
|
- (instancetype)initWithFeedURL:(NSString *)feedURL {
|
|
|
|
|
|
|
|
NSParameterAssert(feedURL != nil);
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
_feedURL = feedURL;
|
|
|
|
_dateParsed = [NSDate date];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-19 19:17:09 +01:00
|
|
|
#pragma mark - Enclosures
|
|
|
|
|
|
|
|
- (void)addEnclosure:(RSParsedEnclosure *)enclosure {
|
|
|
|
|
|
|
|
if (self.enclosures) {
|
|
|
|
self.enclosures = [self.enclosures setByAddingObject:enclosure];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.enclosures = [NSSet setWithObject:enclosure];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:03:05 +01:00
|
|
|
#pragma mark - Authors
|
|
|
|
|
|
|
|
- (void)addAuthor:(RSParsedAuthor *)author {
|
|
|
|
|
|
|
|
if (self.authors) {
|
|
|
|
self.authors = [self.authors setByAddingObject:author];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.authors = [NSSet setWithObject:author];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - articleID
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
- (NSString *)articleID {
|
2017-12-19 19:59:24 +01:00
|
|
|
|
|
|
|
if (self.guid) {
|
|
|
|
return self.guid;
|
|
|
|
}
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
if (!_articleID) {
|
2017-12-19 19:59:24 +01:00
|
|
|
_articleID = [self calculatedArticleID];
|
2017-06-21 06:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _articleID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-19 19:59:24 +01:00
|
|
|
- (NSString *)calculatedArticleID {
|
2017-06-21 06:18:46 +02:00
|
|
|
|
2017-12-19 19:59:24 +01:00
|
|
|
/*Concatenate 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.
|
|
|
|
This is intended to create an ID unique inside a feed, but not globally unique.
|
|
|
|
Not suitable for a database ID, in other words.*/
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
NSMutableString *s = [NSMutableString stringWithString:@""];
|
|
|
|
|
|
|
|
NSString *datePublishedTimeStampString = nil;
|
|
|
|
if (self.datePublished) {
|
|
|
|
datePublishedTimeStampString = [NSString stringWithFormat:@"%.0f", self.datePublished.timeIntervalSince1970];
|
|
|
|
}
|
2017-12-19 20:19:46 +01:00
|
|
|
|
|
|
|
// Ideally we have a permalink and a pubDate. Either one would probably be a good guid, but together they should be rock-solid. (In theory. Feeds are buggy, though.)
|
|
|
|
if (!RSParserStringIsEmpty(self.permalink) && datePublishedTimeStampString) {
|
|
|
|
[s appendString:self.permalink];
|
|
|
|
[s appendString:datePublishedTimeStampString];
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!RSParserStringIsEmpty(self.link) && datePublishedTimeStampString) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:self.link];
|
|
|
|
[s appendString:datePublishedTimeStampString];
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:19:46 +01:00
|
|
|
else if (!RSParserStringIsEmpty(self.title) && datePublishedTimeStampString) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:self.title];
|
|
|
|
[s appendString:datePublishedTimeStampString];
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:19:46 +01:00
|
|
|
else if (datePublishedTimeStampString) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:datePublishedTimeStampString];
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:19:46 +01:00
|
|
|
else if (!RSParserStringIsEmpty(self.permalink)) {
|
|
|
|
[s appendString:self.permalink];
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
else if (!RSParserStringIsEmpty(self.link)) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:self.link];
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
else if (!RSParserStringIsEmpty(self.title)) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:self.title];
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
else if (!RSParserStringIsEmpty(self.body)) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[s appendString:self.body];
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:59:24 +01:00
|
|
|
return [s rsparser_md5Hash];
|
2017-06-21 06:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|