-
Notifications
You must be signed in to change notification settings - Fork 5
/
InstaHelper.xm
277 lines (235 loc) · 10.3 KB
/
InstaHelper.xm
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#import <Foundation/Foundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "InstaHelper.h"
#import "IGHeaders.h"
#import "substrate.h"
@class PHAssetCollection;
@implementation InstaHelper
+ (IGRootViewController *)rootViewController {
AppDelegate *igDelegate = [UIApplication sharedApplication].delegate;
return (IGRootViewController *)((IGShakeWindow *)igDelegate.window).rootViewController;
}
+ (UIViewController *)currentController {
IGRootViewController *rootController = [InstaHelper rootViewController];
return rootController.topMostViewController;
}
+ (IGUser *)currentUser {
IGAuthHelper *authHelper = [%c(IGAuthHelper) sharedAuthHelper];
if ([authHelper respondsToSelector:@selector(currentUser)]) {
return authHelper.currentUser;
}
return [%c(IGAuthHelper) currentUser];
}
+ (IGUserSession *)currentSession {
IGAuthHelper *authHelper = [%c(IGAuthHelper) sharedAuthHelper];
return [authHelper currentUserSession];
}
+ (BOOL)isJailbroken {
NSString *aptitudeFolder = @"/etc/apt";
BOOL dir = NO;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:aptitudeFolder isDirectory:&dir];
if (exists) {
return YES;
} else {
return NO;
}
}
+ (NSURL*)documentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
+ (NSDate *)takenAt:(IGPost*)post {
IGFeedItem *feedItem = (IGFeedItem*)post;
BOOL responds = [feedItem respondsToSelector:@selector(takenAt)];
if (responds) {
return [feedItem takenAt].date;
} else if ([feedItem respondsToSelector:@selector(takenAtDate)]) {
// instagram 7.19
return [feedItem takenAtDate].date;
} else {
return [feedItem albumAwareTakenAtDate].date;
}
}
+ (void)saveVideoToAlbum:(NSURL*)localUrl album:(NSString*)album completion:(void (^)(NSError *error))completion {
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");
if (PHPhotoLibrary_class) {
PHAssetCollection *existingCollection;
if (album) {
PHFetchOptions *albumsFetchOption = [[PHFetchOptions alloc] init];
albumsFetchOption.predicate = [NSPredicate predicateWithFormat:@"title == %@",album];
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:albumsFetchOption];
existingCollection = userAlbums.firstObject;
}
if (!existingCollection && album) {
__block PHObjectPlaceholder *albumPlaceholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album];
albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
PHAssetCollection *assetCollection = fetchResult.firstObject;
[InstaHelper addVideo:localUrl toCollection:assetCollection completion:^(NSError *error) {
completion(error);
}];
} else {
// NSLog(@"Error creating album: %@", error);
completion(error);
}
}];
} else {
[InstaHelper addVideo:localUrl toCollection:existingCollection completion:^(NSError *error) {
completion(error);
}];
}
} else {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:localUrl
completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
completion(error);
} else {
completion(nil);
}
}];
}
}
+ (void)downloadRemoteFile:(NSURL*)url completion:(void (^)(NSData *data, NSError *complErr))completion {
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:7.5];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (error) {
completion(nil, error);
} else {
completion(data, nil);
}
}];
}
+ (void)saveRemoteVideo:(NSURL*)url album:(NSString*)album completion:(void (^)(NSError *error))completion {
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");
if (PHPhotoLibrary_class) {
[InstaHelper downloadRemoteFile:url completion:^(NSData *vidData, NSError *viderr) {
if (viderr) return completion(viderr);
NSFileManager *fsmanager = [NSFileManager defaultManager];
NSURL *videoDocs = [[fsmanager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *saveUrl = [videoDocs URLByAppendingPathComponent:[url lastPathComponent]];
dispatch_async(dispatch_get_main_queue(), ^{
[vidData writeToURL:saveUrl atomically:YES];
[InstaHelper saveVideoToAlbum:saveUrl album:album completion: ^(NSError *saveErr) {
if (saveErr) {
completion(saveErr);
} else {
// we don't want to remove this due to needing it for the share sheet
// [fsmanager removeItemAtPath:[saveUrl path] error:NULL];
completion(nil);
}
}];
});
}];
} else {
[InstaHelper downloadRemoteFile:url completion:^(NSData *vidData, NSError *viderr) {
if (viderr) return completion(viderr);
NSFileManager *fsmanager = [NSFileManager defaultManager];
NSURL *videoDocs = [[fsmanager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *saveUrl = [videoDocs URLByAppendingPathComponent:[url lastPathComponent]];
dispatch_async(dispatch_get_main_queue(), ^{
[vidData writeToURL:saveUrl atomically:YES];
[InstaHelper saveVideoToAlbum:saveUrl album:@"InstaBetter" completion: ^(NSError *saveErr) {
if (saveErr) {
completion(saveErr);
} else {
completion(nil);
}
}];
});
}];
}
}
+ (void)saveRemoteImage:(NSURL*)url album:(NSString*)album completion:(void (^)(NSError *error))completion {
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");
NSLog(@"SAVING REMOTE IMAGE!!");
if (PHPhotoLibrary_class) {
[InstaHelper downloadRemoteFile:url completion:^(NSData *imgData, NSError *imgerr) {
UIImage *image = [UIImage imageWithData:imgData];
PHAssetCollection *existingCollection;
if (album) {
PHFetchOptions *albumsFetchOption = [[PHFetchOptions alloc] init];
albumsFetchOption.predicate = [NSPredicate predicateWithFormat:@"title == %@",album];
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:albumsFetchOption];
existingCollection = userAlbums.firstObject;
}
// Photos framework does not handle existing collections, so we have to do that ourselves
if (!existingCollection && album) {
__block PHObjectPlaceholder *albumPlaceholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album];
albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
PHAssetCollection *assetCollection = fetchResult.firstObject;
[InstaHelper addImage:image toCollection:assetCollection completion:^(NSError *error) {
completion(error);
}];
} else {
completion(error);
}
}];
} else {
[InstaHelper addImage:image toCollection:existingCollection completion:^(NSError *error) {
completion(error);
}];
}
}];
} else {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:url];
NSLog(@"CALLED SAVING!!");
[library writeImageDataToSavedPhotosAlbum:imgData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"CHECKING %@", error);
if (error) {
NSLog(@"COMPLETED WITH ERROR!!");
completion(error);
} else {
NSLog(@"COMPLETED WITH NO ERROR!!");
completion(nil);
}
}];
}
}
+ (BOOL)isRemoteImage:(NSURL*)url {
NSArray *extensions = @[@"jpg", @"jpeg", @"png"];
NSString *ext = [url pathExtension];
return [extensions containsObject:[ext lowercaseString]];
}
+ (void)addImage:(UIImage*)image toCollection:(id)collection completion:(void (^)(NSError *error))completion {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (collection) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
}
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
// NSLog(@"Error creating asset: %@", error);
}
completion(error);
}];
}
+ (void)addVideo:(NSURL*)videoURL toCollection:(id)collection completion:(void (^)(NSError *error))completion {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
NSLog(@"Successfully saved!");
if (collection) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
}
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
// NSLog(@"Error creating asset: %@", error);
}
completion(error);
}];
}
@end