Skip to content

Commit

Permalink
2.0b12 - Add additional checks for MAL Sync to see if the user creden…
Browse files Browse the repository at this point in the history
…tials are still valid to prevent the Unofficial MAL API from being banned from too many failed login attemptws.
  • Loading branch information
moyitpro committed Jul 9, 2017
1 parent 2891801 commit e1fb67f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
9 changes: 6 additions & 3 deletions AdvancedPrefController.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ - (IBAction)clearlogin:(id)sender {
- (void)login:(NSString *)username password:(NSString *)password {
[savebut setEnabled:NO];
//Set Login URL
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPSessionManager *manager = [Utility manager];
manager.responseSerializer = [Utility httpresponseserializer];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Basic %@", [[NSString stringWithFormat:@"%@:%@", username, password] base64Encoding]] forHTTPHeaderField:@"Authorization"];
[manager GET:[NSString stringWithFormat:@"%@/1/account/verify_credentials", [defaults objectForKey:@"MALAPIURL"]] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
[manager GET:@"https://myanimelist.net/api/account/verify_credentials.xml" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
//Login successful
[Utility showsheetmessage:@"Login Successful" explaination: @"Login is successful." window:self.view.window];
// Store account in login keychain
[haengine storemalaccount:fieldusername.stringValue password:fieldpassword.stringValue];
// Set the date to Verify Credentials
[[NSUserDefaults standardUserDefaults] setObject:[NSDate dateWithTimeIntervalSinceNow:60*60*24] forKey:@"credentialscheckdate"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"credentialsvalid"];
[clearbut setEnabled: YES];
loggedinuser.stringValue = username;
[loggedinview setHidden:NO];
Expand Down
30 changes: 16 additions & 14 deletions Base.lproj/AdvancedPrefController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
<tabViewItems>
<tabViewItem label="Kodi/Plex Theater RPC" identifier="1" id="Raw-cz-FaZ">
<view key="view" id="uxH-VE-ISN">
<rect key="frame" x="10" y="33" width="431" height="285"/>
<rect key="frame" x="10" y="33" width="184" height="0.0"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gnC-PW-UbL">
<rect key="frame" x="23" y="232" width="103" height="17"/>
<rect key="frame" x="23" y="-53" width="103" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Host:" id="Sfk-hk-xua">
<font key="font" metaFont="system"/>
Expand All @@ -51,7 +51,7 @@
</textFieldCell>
</textField>
<textField identifier="kodiaddress" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MSC-qD-cvb">
<rect key="frame" x="138" y="227" width="263" height="22"/>
<rect key="frame" x="138" y="-58" width="263" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="8SV-i3-wT0">
<font key="font" metaFont="system"/>
Expand All @@ -69,7 +69,7 @@
</connections>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gJg-m2-aJf">
<rect key="frame" x="138" y="139" width="96" height="22"/>
<rect key="frame" x="138" y="-146" width="96" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="ch8-ks-Y5e">
<numberFormatter key="formatter" formatterBehavior="custom10_4" usesGroupingSeparator="NO" groupingSize="0" minimumIntegerDigits="0" maximumIntegerDigits="42" id="tme-m8-2zX" customClass="NSNumberFormatterNumberOnly">
Expand All @@ -89,7 +89,7 @@
</connections>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="i6f-U0-q8z">
<rect key="frame" x="35" y="142" width="91" height="17"/>
<rect key="frame" x="35" y="-143" width="91" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Port:" id="pU8-xG-g07">
<font key="font" metaFont="system"/>
Expand All @@ -98,7 +98,7 @@
</textFieldCell>
</textField>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Zuh-Gd-n0d">
<rect key="frame" x="63" y="258" width="347" height="18"/>
<rect key="frame" x="63" y="-27" width="347" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Enable Kodi/Plex Theater RPC API Detection" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="58w-YA-ei0">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -110,7 +110,7 @@
</connections>
</button>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Z3P-ov-X8s">
<rect key="frame" x="63" y="37" width="340" height="34"/>
<rect key="frame" x="63" y="-248" width="340" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="In order to make Kodi/Plex Theater JSON RPC API detection work, you need to have HTTP JSON-RPC enabled." id="Agl-d8-C1u">
<font key="font" metaFont="smallSystem"/>
Expand All @@ -119,7 +119,7 @@
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Mxp-Ed-WKM">
<rect key="frame" x="135" y="107" width="275" height="28"/>
<rect key="frame" x="135" y="-178" width="275" height="28"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="The default port number is 3005 in Plex Theater and 80 in Kodi." id="XKP-As-iQI">
<font key="font" metaFont="smallSystem"/>
Expand All @@ -128,7 +128,7 @@
</textFieldCell>
</textField>
<button horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Kvt-Mp-fU3">
<rect key="frame" x="4" y="10" width="25" height="25"/>
<rect key="frame" x="4" y="-275" width="25" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="help" bezelStyle="helpButton" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="tfu-ok-Pcb">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -139,7 +139,7 @@
</connections>
</button>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="JB0-ss-Mwe">
<rect key="frame" x="135" y="169" width="268" height="56"/>
<rect key="frame" x="135" y="-116" width="268" height="56"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" id="7G9-zR-zjL">
<font key="font" metaFont="smallSystem"/>
Expand All @@ -149,7 +149,7 @@
</textFieldCell>
</textField>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="tLR-wg-ocK">
<rect key="frame" x="89" y="77" width="306" height="18"/>
<rect key="frame" x="89" y="-208" width="306" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<string key="toolTip">Uses the filename of the media being played in Kodi/Plex Theater for recognition instead of the title. Enable this if you use a Plex Agent. Not compatable with transcoded media files.</string>
<buttonCell key="cell" type="check" title="Use Filename for Recognition" bezelStyle="regularSquare" imagePosition="left" inset="2" id="7GS-84-pZU">
Expand All @@ -165,7 +165,7 @@
</view>
</tabViewItem>
<tabViewItem label="MyAnimeList Sync" identifier="2" id="OFg-cA-88Q">
<view key="view" id="1Sa-gR-NS9">
<view key="view" ambiguous="YES" id="1Sa-gR-NS9">
<rect key="frame" x="10" y="33" width="431" height="285"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand All @@ -179,7 +179,7 @@
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Q8O-iP-Q0g">
<rect key="frame" x="47" y="207" width="94" height="17"/>
<rect key="frame" x="47" y="210" width="94" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="API URL:" id="Ygm-YV-IFl">
<font key="font" metaFont="system"/>
Expand Down Expand Up @@ -389,7 +389,7 @@
</view>
</tabViewItem>
<tabViewItem label="Plex Media Server" identifier="" id="4tO-Qi-kFQ">
<view key="view" ambiguous="YES" id="6hg-MD-aDV">
<view key="view" id="6hg-MD-aDV">
<rect key="frame" x="10" y="33" width="431" height="285"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -503,6 +503,7 @@
</buttonCell>
<connections>
<action selector="plexlogin:" target="-2" id="BWa-CT-Wob"/>
<binding destination="8ch-Z3-2Y0" name="enabled" keyPath="values.enableplexapi" id="767-Rv-GlQ"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="lpe-CH-H0U">
Expand All @@ -514,6 +515,7 @@
</buttonCell>
<connections>
<action selector="plexlogout:" target="-2" id="e9F-cm-cu9"/>
<binding destination="8ch-Z3-2Y0" name="enabled" keyPath="values.enableplexapi" id="9jR-Gf-nxj"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7l5-rQ-BLq">
Expand Down
1 change: 1 addition & 0 deletions Hachidori+Keychain.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
- (BOOL)removemalaccount;
- (NSString *)getmalusername;
- (NSString *)getBase64;
- (int)checkMALCredentials;
@end

38 changes: 38 additions & 0 deletions Hachidori+Keychain.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,42 @@ - (NSString *)getBase64{
return [[NSString stringWithFormat:@"%@:%@", [self getmalusername], [SAMKeychain passwordForService:@"Hachidori - MyAnimeList" account:self.malusername]] base64Encoding];
}

- (int)checkMALCredentials {
// Check if the credentialsvalid flag is not set to false/NO
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"credentialsvalid"]) {
return 0;
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"credentialscheckdate"] timeIntervalSinceNow] < 0) {
// Check credentials
//Set Login URL
NSURL *url = [NSURL URLWithString:@"https://myanimelist.net/api/account/verify_credentials.xml"];
EasyNSURLConnection *request = [[EasyNSURLConnection alloc] initWithURL:url];
//Ignore Cookies
[request setUseCookies:NO];
//Set Username and Password
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Basic %@", [self getBase64]]};
//Verify Username/Password
[request startRequest];
// Check for errors
NSError *error = [request getError];
if ([request getStatusCode] == 200 && !error) {
[[NSUserDefaults standardUserDefaults] setObject:[NSDate dateWithTimeIntervalSinceNow:60*60*24] forKey:@"credentialscheckdate"];
NSLog(@"User credentials valid.");
return 1;
}
else if ([request getStatusCode] == 204) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"credentialsvalid"];
NSLog(@"ERROR: User credentials are invalid. Aborting MAL Sync...");
return 0;
}
else {
NSLog(@"Unable to check user credentials. Trying again later.");
return 2;
}
}
return 1;


}

@end
16 changes: 13 additions & 3 deletions Hachidori+MALSync.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
@implementation Hachidori (MALSync)
- (BOOL)sync{
NSLog(@"Starting MyAnimeList Sync...");
// Set check Date if it doesn't exist
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"credentialscheckdate"]){
// Check credentials now if user has an account and these values are not set
[[NSUserDefaults standardUserDefaults] setObject:[NSDate new] forKey:@"credentialscheckdate"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"credentialsvalid"];
}
// Check Credentials status
if ([self checkMALCredentials] == 0) {
return NO;
}
self.MALApiUrl = [[NSUserDefaults standardUserDefaults] valueForKey:@"MALAPIURL"];
int syncstatus = [self checkStatus];
if (syncstatus == 1) {
Expand All @@ -38,7 +48,7 @@ - (int)checkStatus{
//Ignore Cookies
[request setUseCookies:NO];
//Set Token
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Bearer %@", [[self getFirstAccount] accessToken]]};
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Basic %@", [self getBase64]]};
//Perform Search
[request startRequest];
// Get Status Code
Expand Down Expand Up @@ -77,7 +87,7 @@ - (BOOL)updatetitle{
//Ignore Cookies
[request setUseCookies:NO];
//Set Token
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Bearer %@", [[self getFirstAccount] accessToken]]};
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Basic %@", [self getBase64]]};
[request setPostMethod:@"PUT"];
// Set info
[request addFormData:self.LastScrobbledEpisode forKey:@"episodes"];
Expand Down Expand Up @@ -124,7 +134,7 @@ - (BOOL)addtitle{
//Ignore Cookies
[request setUseCookies:NO];
//Set Token
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Bearer %@", [[self getFirstAccount] accessToken]]};
request.headers = @{@"Authorization": [NSString stringWithFormat:@"Basic %@", [self getBase64]]};
[request addFormData:self.MALID forKey:@"anime_id"];
[request addFormData:self.LastScrobbledEpisode forKey:@"episodes"];

Expand Down
4 changes: 2 additions & 2 deletions Hachidori-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0b11</string>
<string>2.0b12</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2.0b11</string>
<string>2.0b12</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down

0 comments on commit e1fb67f

Please sign in to comment.