-
Notifications
You must be signed in to change notification settings - Fork 11
/
TKTumblr.m
222 lines (183 loc) · 8.51 KB
/
TKTumblr.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Copyright (c) 2010, 2011 TumblrKit
//
// 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.
//
#include "TKTumblr.h"
#include "NSString+TumblrKit.h"
#include "NSDictionary+TumblrKit.h"
#include "TKTumblelog.h"
@implementation TKTumblr
@synthesize delegate, email, password, currentPost, currentElementName, requestedPost, currentTumblelog;
- (id)initWithEmail:(NSString *)theEmail andPassword:(NSString *)thePassword
{
if ((self = [super init]) != nil) {
self.email = theEmail;
self.password = thePassword;
self.currentTumblelog = nil;
self.currentPost = nil;
self.currentElementName = nil;
self.requestedPost = nil;
}
return self;
}
- (TKPost *)postWithID:(NSNumber *)thePostID andDomain:(NSString *)theDomain
{
TKTumblrReadRequest *theReadRequest = [[TKTumblrReadRequest alloc] initWithPostID:thePostID andDomain:theDomain];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[theReadRequest endpoint]];
id previousDelegate = [self delegate];
[self setDelegate:self];
[parser setDelegate:self];
[parser parse];
[self setDelegate:previousDelegate];
TKPost *thePost = nil;
if (requestedPost) {
thePost = [self requestedPost];
[self setRequestedPost:nil];
}
return thePost;
}
- (void)postsWithReadRequest:(TKTumblrReadRequest *)theReadRequest
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[theReadRequest endpoint]];
[parser setDelegate:self];
[parser parse];
}
- (BOOL)uploadPost:(TKPost *)thePost
{
return [self uploadPost:thePost withDomain:nil];
}
- (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain
{
NSMutableDictionary *postDict = (NSMutableDictionary *)[self attributesAsDictionary];
if (theDomain)
[postDict setObject:theDomain forKey:@"group"];
[postDict addEntriesFromDictionary:[thePost attributesAsDictionary]];
NSData *postBody = [postDict multipartMIMEData];
NSMutableURLRequest *theURLRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.tumblr.com/api/write"]];
[theURLRequest setHTTPMethod:@"POST"];
[theURLRequest setValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[theURLRequest setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];
[theURLRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", [NSString MIMEBoundary]] forHTTPHeaderField:@"Content-Type"];
[theURLRequest setHTTPBody:postBody];
if (delegate && [delegate respondsToSelector:@selector(tumblrWillUploadPost:withDomain:)]) {
[delegate tumblrWillUploadPost:thePost withDomain:theDomain];
}
NSError *error = nil;
NSHTTPURLResponse *theURLResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theURLRequest
returningResponse:&theURLResponse
error:&error];
// Release the request before we can enter some potentially dangerous
// code path.
// Bail out quickly if NSURLConnection populated error.
if (([theURLResponse statusCode] != TKTumblrCreated)) {
if (delegate && [delegate respondsToSelector:@selector(tumblrDidFailToUploadPost:withDomain:returnCode:)]) {
[delegate tumblrDidFailToUploadPost:thePost
withDomain:theDomain
returnCode:(TKTumblrResponseReturnCode)[theURLResponse statusCode]];
}
return NO;
}
// The following code will only execute when we get a TKTumblrCreated response, so it's
// safe to assume that responseData will have only the postID of the post we created.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *postID = [formatter numberFromString:[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]];
if (delegate && [delegate respondsToSelector:@selector(tumblrDidUploadPost:withDomain:postID:)]) {
[delegate tumblrDidUploadPost:thePost withDomain:theDomain postID:postID];
}
return YES;
}
- (NSDictionary *)attributesAsDictionary
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password",
nil];
return dict;
}
- (NSArray *)tumblelogs
{
NSString *theURLString = [NSString stringWithFormat:@"https://www.tumblr.com/api/authenticate?email=%@&password=%@",
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *theURL = [NSURL URLWithString:theURLString];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
[parser setDelegate:self];
[parser parse];
return nil;
}
#pragma mark NSXMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
self.currentElementName = elementName;
if ([elementName isEqualToString:@"post"]) {
self.currentPost = [TKPost postWithAttributes:attributeDict];
}
else if ([elementName isEqualToString:@"tumblelog"]) {
self.currentTumblelog = [TKTumblelog tumblelogWithAttributes:attributeDict];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"post"]) {
if (currentPost != nil) {
if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceivePost:withDomain:)]) {
[delegate tumblrDidReceivePost:currentPost withDomain:@""];
}
}
self.currentPost = nil;
}
else if ([elementName isEqualToString:@"tumblelog"]) {
if (currentTumblelog != nil) {
if (delegate && [delegate respondsToSelector:@selector(tumblrDidReceiveTumblelog:)]) {
[delegate tumblrDidReceiveTumblelog:currentTumblelog];
}
}
self.currentTumblelog = nil;
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
static NSDictionary *elementToSelectorDict = nil;
if (!elementToSelectorDict) {
elementToSelectorDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"appendToText:", @"text",
@"appendToCaption:",@"caption",
@"appendToPlayer:", @"player",
@"appendToBody:", @"body",
@"setURLWithString:", @"url",
nil];
}
NSString *key = [[currentElementName componentsSeparatedByString:@"-"] lastObject];
SEL selector = NSSelectorFromString([elementToSelectorDict objectForKey:key]);
if (selector) {
[currentPost performSelector:selector withObject:string];
}
}
#pragma clang diagnostic pop
#pragma mark TKTumblrDelegate
- (void)tumblrDidReceivePost:(TKPost *)thePost withDomain:(NSString *)theDomain
{
[self setRequestedPost:thePost];
}
@end