forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountsViewController+VendorID.m
153 lines (129 loc) · 6.87 KB
/
AccountsViewController+VendorID.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
//
// AccountsViewController+VendorID.m
// AppSales
//
// Created by Ole Zorn on 24.08.11.
// Copyright 2011 omz:software. All rights reserved.
//
#import "AccountsViewController+VendorID.h"
#import "NSDictionary+HTTP.h"
#import "RegexKitLite.h"
@implementation AccountsViewController (AccountsViewController_VendorID)
- (void)findVendorIDsWithLogin:(NSDictionary *)loginInfo
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *username = [loginInfo objectForKey:kAccountUsername];
NSString *password = [loginInfo objectForKey:kAccountPassword];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:@"https://itunesconnect.apple.com"]];
for (NSHTTPCookie *cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:@"https://reportingitc.apple.com"]];
for (NSHTTPCookie *cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
NSString *ittsBaseURL = @"https://itunesconnect.apple.com";
NSString *ittsLoginPageAction = @"/WebObjects/iTunesConnect.woa";
NSString *signoutSentinel = @"menu-item sign-out";
NSURL *loginURL = [NSURL URLWithString:[ittsBaseURL stringByAppendingString:ittsLoginPageAction]];
NSHTTPURLResponse *loginPageResponse = nil;
NSError *loginPageError = nil;
NSData *loginPageData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:loginURL] returningResponse:&loginPageResponse error:&loginPageError];
NSString *loginPage = [[[NSString alloc] initWithData:loginPageData encoding:NSUTF8StringEncoding] autorelease];
if ([loginPage rangeOfString:signoutSentinel].location == NSNotFound) {
// find the login action
NSScanner *loginPageScanner = [NSScanner scannerWithString:loginPage];
[loginPageScanner scanUpToString:@"action=\"" intoString:nil];
if (![loginPageScanner scanString:@"action=\"" intoString:nil]) {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
[pool release];
return;
}
NSString *loginAction = nil;
[loginPageScanner scanUpToString:@"\"" intoString:&loginAction];
NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"theAccountName",
password, @"theAccountPW",
@"39", @"1.Continue.x", // coordinates of submit button on screen. any values seem to work
@"7", @"1.Continue.y",
nil];
loginPage = [self stringFromSynchronousPostRequestWithURL:[NSURL URLWithString:[ittsBaseURL stringByAppendingString:loginAction]] bodyDictionary:postDict];
if (loginPage == nil || [loginPage rangeOfString:signoutSentinel].location == NSNotFound) {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
[pool release];
return;
}
}
NSError *error = nil;
NSString *salesAction = @"https://reportingitc.apple.com";
NSString *salesRedirectPage = [NSString stringWithContentsOfURL:[NSURL URLWithString:salesAction] usedEncoding:NULL error:&error];
if (error) {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
[pool release];
return;
}
NSScanner *salesRedirectScanner = [NSScanner scannerWithString:salesRedirectPage];
NSString *viewState = [salesRedirectPage stringByMatching:@"\"javax.faces.ViewState\" value=\"(.*?)\"" capture:1];
[salesRedirectScanner scanUpToString:@"script id=\"defaultVendorPage:" intoString:nil];
if (![salesRedirectScanner scanString:@"script id=\"defaultVendorPage:" intoString:nil]) {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
[pool release];
return;
}
NSString *defaultVendorPage = nil;
[salesRedirectScanner scanUpToString:@"\"" intoString:&defaultVendorPage];
// click though from the dashboard to the sales page
NSDictionary *reportPostData = [NSDictionary dictionaryWithObjectsAndKeys:
[defaultVendorPage stringByReplacingOccurrencesOfString:@"_2" withString:@"_0"], @"AJAXREQUEST",
viewState, @"javax.faces.ViewState",
defaultVendorPage, @"defaultVendorPage",
[@"defaultVendorPage:" stringByAppendingString:defaultVendorPage],[@"defaultVendorPage:" stringByAppendingString:defaultVendorPage],
nil];
[self dataFromSynchronousPostRequestWithURL:[NSURL URLWithString:@"https://reportingitc.apple.com/vendor_default.faces"] bodyDictionary:reportPostData response:NULL];
NSString *salesPage = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://reportingitc.apple.com/sales.faces"] usedEncoding:NULL error:NULL];
NSString *defaultVendorID = [salesPage stringByMatching:@">.*?\\s?(8[0-9]{7})" capture:1];
if (!defaultVendorID) {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:@selector(finishedLoadingVendorID:) withObject:defaultVendorID waitUntilDone:YES];
}
[pool release];
}
- (void)finishedLoadingVendorID:(NSString *)vendorID
{
if (self.presentedViewController) {
//Adding new account:
FieldEditorViewController *vc = [[(UINavigationController *)self.presentedViewController viewControllers] objectAtIndex:0];
[vc.values setObject:vendorID forKey:kAccountVendorID];
[vc.tableView reloadData];
} else {
//Editing existing account:
FieldEditorViewController *vc = self.navigationController.viewControllers.lastObject;
[vc.values setObject:vendorID forKey:kAccountVendorID];
[vc.tableView reloadData];
}
}
- (void)failedToLoadVendorIDs
{
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"The vendor ID could not be filled automatically. Please check your username and password or enter your vendor ID manually. You'll find it at the top of the Sales and Trends module on itunesconnect.apple.com.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil] autorelease] show];
}
- (NSString *)stringFromSynchronousPostRequestWithURL:(NSURL *)URL bodyDictionary:(NSDictionary *)bodyDictionary
{
NSData *data = [self dataFromSynchronousPostRequestWithURL:URL bodyDictionary:bodyDictionary response:NULL];
if (data) {
return [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
}
return nil;
}
- (NSData *)dataFromSynchronousPostRequestWithURL:(NSURL *)URL bodyDictionary:(NSDictionary *)bodyDictionary response:(NSHTTPURLResponse **)response
{
NSString *postDictString = [bodyDictionary formatForHTTP];
NSData *httpBody = [postDictString dataUsingEncoding:NSASCIIStringEncoding];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:URL];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:httpBody];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:response error:NULL];
return data;
}
@end