-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVTwitterTweetService.m
136 lines (119 loc) · 4.48 KB
/
MVTwitterTweetService.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import "MVTwitterTweetService.h"
#import "MVJSONGetRequest.h"
#import "NSMutableAttributedString+LinksDetection.h"
#import "NSString+HTMLEntities.h"
@interface MVTwitterTweetService ()
@property (strong, readwrite) NSURL *url;
@property (readwrite) long long tweetId;
@property (strong, readwrite) NSString *text;
@property (strong, readwrite) NSAttributedString *attributedText;
@property (strong, readwrite) NSString *userName;
@property (strong, readwrite) NSURL *userImageUrl;
@property (strong, readwrite) NSString *userScreenName;
@property (readwrite) BOOL informationFetched;
@property (readwrite) BOOL error;
@end
@implementation MVTwitterTweetService
@synthesize url = url_,
tweetId = tweetId_,
text = text_,
attributedText = attributedText_,
userName = userName_,
userImageUrl = userImageUrl_,
userScreenName = userScreenName_,
informationFetched = informationFetched_,
error = error_;
- (id)initWithURL:(NSURL*)url
tweetId:(long long)tweetId
{
self = [super init];
if(self)
{
url_ = url;
tweetId_ = tweetId;
text_ = nil;
attributedText_ = nil;
userName_ = nil;
userImageUrl_ = nil;
userScreenName_ = nil;
informationFetched_ = NO;
error_ = NO;
}
return self;
}
- (NSURL*)userUrl
{
NSString *userUrlString = [NSString stringWithFormat:@"http://twitter.com/%@",
self.userScreenName];
return [NSURL URLWithString:userUrlString];
}
#pragma mark -
#pragma mark MVService Methods
- (void)fetchInformation
{
NSString *urlString = [NSString stringWithFormat:
@"http://api.twitter.com/1/statuses/show/%lld.json",
self.tweetId];
NSURL *url = [NSURL URLWithString:urlString];
MVJSONGetRequest *getRequest = [[MVJSONGetRequest alloc] initWithURL:url];
[getRequest get:^(NSObject *object) {
@try {
NSDictionary *dictionary = (NSDictionary*)object;
if(dictionary && ![dictionary valueForKey:@"error"] && ![dictionary valueForKey:@"errors"])
{
NSDictionary *status = dictionary;
if([dictionary valueForKey:@"retweeted_status"])
status = [dictionary valueForKey:@"retweeted_status"];
NSString *text = [status valueForKey:@"text"];
if(!text)
text = @"";
else
text = [text decodeHTMLEntities];
self.text = text;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:self.text];
[attributedText mv_detectLinks];
[attributedText mv_detectEmails];
[attributedText mv_detectTwitterUsernamesAndHashTags];
self.attributedText = attributedText;
NSDictionary *user = [status valueForKey:@"user"];
self.userName = [user valueForKey:@"name"];
NSString *userImageUrlString = [user valueForKey:@"profile_image_url"];
self.userImageUrl = [NSURL URLWithString:userImageUrlString];
self.userScreenName = [user valueForKey:@"screen_name"];
}
else
{
self.error = YES;
}
}
@catch (NSException *exception) {
self.error = YES;
}
self.informationFetched = YES;
}];
}
+ (MVTwitterTweetService*)serviceForURL:(NSURL*)url;
{
if([url.host isEqualToString:@"twitter.com"] || [url.host isEqualToString:@"www.twitter.com"])
{
NSString *pattern = @"\\/(status|statuses)\\/([0-9]*)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *matches = [regex matchesInString:url.absoluteString
options:0
range:NSMakeRange(0, url.absoluteString.length)];
if(matches.count == 1)
{
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSRange tweetIdRange = [match rangeAtIndex:2];
long long tweetId = [[url.absoluteString substringWithRange:tweetIdRange] longLongValue];
MVTwitterTweetService *service = [[MVTwitterTweetService alloc] initWithURL:url
tweetId:tweetId];
return service;
}
}
return nil;
}
@end