Can not convert attached valid code which is successfully handled by https://objectivec2swift.com #187

Open
denizoguz opened this Issue Mar 27, 2016 · 2 comments

Projects

None yet

2 participants

@denizoguz

Can not convert following valid code.

`

import "ArasKargo.h"

import "TrackingResult.h"

import "AFURLRequestSerialization.h"

import "AFHTTPRequestOperationManager.h"

import "TrackingNumberHistory.h"

import "HTMLReader/HTMLReader.h"

import "PreTrackingConfiguration.h"

static NSString *const HOST = @"http://kargotakip.araskargo.com.tr";

@implementation ArasKargo {

}

  • (id)init {
    self = [super init];
    if (self) {
    self.name = NSLocalizedString(@"courier.aras_kargo", @"");
    self.id = 2;
    self.URL = [NSString stringWithFormat:@"%@/%@", HOST, @"?code="];
    self.iconName = @"ArasKargo";
    self.captchaURL = [NSString stringWithFormat:@"%@/%@", HOST, @"securityimage.aspx"];
    self.css = @"aras.css";
    self.labelColor = [UIColor colorWithRed:1.00 green:0.91 blue:0.00 alpha:1.0];
    self.infoColor = [UIColor colorWithRed:0.69 green:0.16 blue:0.15 alpha:1.0];
    }
    return self;
    }

  • (void)trackWithTrackingNumber:(NSString *)trackingNumber usingCaptcha:(NSString *)captcha inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext andGiveResult:(void (^)(TrackingResult *trackingResult))callback {
    manager = [self getHttpRequestOperationManagerWithHTTP];
    TrackingResult *trackingResult = [self getEmptyTrackingResult:trackingNumber withManagedObjectContext:managedObjectContext];
    NSString *queryURL = [NSString stringWithFormat:@"%@%@", self.URL, trackingNumber];
    [manager GET:queryURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (!operation.responseString) {
    [self setCannotTrack:trackingResult withCallback:callback];
    return;
    }
    HTMLDocument *htmlDocument = [HTMLDocument documentWithString:operation.responseString];
    NSArray *labels = [htmlDocument nodesMatchingSelector:@"#Label1"];
    if (!labels.firstObject) {
    [self setCannotTrack:trackingResult withCallback:callback];
    return;
    }
    trackingResult.trackingNumberHistory.status = [labels.firstObject textContent];

    //fetch page showing details
    NSString *detailsURL = [NSString stringWithFormat:@"%@/%@", HOST, @"control.aspx"];
    [manager GET:detailsURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (!operation.responseString) {
            [self setCannotTrack:trackingResult withCallback:callback];
            return;
        }
    
        AFHTTPRequestSerializer *requestSerializer = manager.requestSerializer;
        [requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
        [requestSerializer setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
    
        NSMutableDictionary *parameters = [self getAllHiddenFields:operation.responseString];
        parameters[@"txt"] = captcha;
        parameters[@"btn1"] = @"Onayla";
    
        [manager POST:detailsURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self fetchFinalPage:trackingNumber manager:manager trackingResult:trackingResult andGiveResult:callback];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self fetchFinalPage:trackingNumber manager:manager trackingResult:trackingResult andGiveResult:callback];
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self setCannotTrack:trackingResult withCallback:callback];
        return;
    }];
    

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self setCannotTrack:trackingResult withCallback:callback];
    return;
    }];
    }

  • (void)fetchFinalPage:(NSString *)trackingNumber manager:(AFHTTPRequestOperationManager *)manager trackingResult:(TrackingResult *)trackingResult andGiveResult:(void (^)(TrackingResult *trackingResult))callback {
    [manager GET:@"http://kargotakip.araskargo.com.tr/kargotakip.aspx" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (!operation.responseString) {
    [self setCannotTrack:trackingResult withCallback:callback];
    return;
    }
    [self performSecondRequestUsingFirstRequest:operation.responseString usingLinkPrefix:@"CargoInfoWaybillAndDelivered.aspx?shipmentid=" andUsinglinkTerminationString:@"&" andUsingCallback:^(NSString *secondRequestBody) {
    [self setBasicInfoFromBody:secondRequestBody withResponse:trackingResult];
    }];
    [self performSecondRequestUsingFirstRequest:operation.responseString usingLinkPrefix:@"CargoInfoTransactionAndRedirection.aspx?barcode=" andUsinglinkTerminationString:@"" >" andUsingCallback:^(NSString *secondRequestBody) {
    HTMLDocument *htmlDocument = [HTMLDocument documentWithString:secondRequestBody];
    NSArray *operationsTableRows = [htmlDocument nodesMatchingSelector:@"#transactionsDataGrid tr"];
    if ([operationsTableRows count] > 0) {
    for (int i = 1; i < [operationsTableRows count]; i++) {
    HTMLElement *rowElement = [operationsTableRows objectAtIndex:i];
    NSArray *rowColumns = [rowElement nodesMatchingSelector:@"td"];
    NSString *dateAndTime = [[rowColumns objectAtIndex:0] innerHTML];
    NSString *il = [[rowColumns objectAtIndex:1] innerHTML];
    NSString *birim = [[rowColumns objectAtIndex:2] innerHTML];
    NSString *operation = [[rowColumns objectAtIndex:3] innerHTML];
    NSString *location = [NSString stringWithFormat:@"%@/%@", birim, il];
    NSMutableDictionary *operationDetailsForResult = [[NSMutableDictionary alloc] init];
    [operationDetailsForResult setValue:dateAndTime forKey:DATE];
    [operationDetailsForResult setValue:location forKey:LOCATION];
    [operationDetailsForResult setValue:operation forKey:OPERATION];
    operationDetailsForResult[LOCATION_LINK] = [self getMapsLink:location];
    NSMutableArray *operationResultsArray = [trackingResult.queryResult objectForKey:OPERATIONS];
    [operationResultsArray addObject:operationDetailsForResult];
    }
    }
    callback(trackingResult);
    }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self setCannotTrack:trackingResult withCallback:callback];
    return;
    }];
    }

  • (void)setBasicInfoFromBody:(NSString *)secondRequestBody withResponse:(TrackingResult *)trackingResult {
    if (secondRequestBody == nil) {
    trackingResult.success = NO;
    trackingResult.trackingNumberHistory.status = NSLocalizedString(@"Cat not track", "");
    return;
    }
    HTMLDocument *document = [HTMLDocument documentWithString:secondRequestBody];
    HTMLElement *element = [document firstNodeMatchingSelector:@"#LabelIlkCikis > a"];
    NSString *departingBranchName = element.innerHTML;
    NSString *departingBranchLink = [element.attributes valueForKey:@"href"];
    element = [document firstNodeMatchingSelector:@"#varis_subesi > a"];
    NSString *arrivalBranchName = element.innerHTML;
    NSString *arrivalBranchLink = [element.attributes valueForKey:@"href"];
    element = [document firstNodeMatchingSelector:@"#fatura_turu"];
    NSString *paymentType = [element textContent];
    NSString *shipmentDate = [[document firstNodeMatchingSelector:@"#cikis_tarihi"] innerHTML];
    NSString *receiverName = [[document firstNodeMatchingSelector:@"#alici_adi_soyadi"] innerHTML];
    NSString *senderName = [[document firstNodeMatchingSelector:@"#gonderici_adi_soyadi"] innerHTML];
    NSString *signatureName = [[document firstNodeMatchingSelector:@"#Teslim_Alan"] innerHTML];
    NSString *deliveryDate = [[document firstNodeMatchingSelector:@"#Teslim_Tarihi"] innerHTML];
    NSString *status = [[document firstNodeMatchingSelector:@"#Son_Durum"] innerHTML];

    trackingResult.success = YES;
    trackingResult.trackingNumberHistory.status = status;
    [trackingResult.queryResult setValue:senderName forKey:SENDER_NAME];
    [trackingResult.queryResult setValue:shipmentDate forKey:SHIPMENT_DATE];
    [trackingResult.queryResult setValue:receiverName forKey:RECEIVER_NAME];
    [trackingResult.queryResult setValue:departingBranchName forKey:DEPARTING_BRANCH];
    [trackingResult.queryResult setValue:departingBranchLink forKey:DEPARTING_BRANCH_LINK];
    [trackingResult.queryResult setValue:arrivalBranchName forKey:ARRIVAL_BRANCH];
    [trackingResult.queryResult setValue:arrivalBranchLink forKey:ARRIVAL_BRANCH_LINK];
    [trackingResult.queryResult setValue:paymentType forKey:PAYMENT_TYPE];
    [trackingResult.queryResult setValue:status forKey:STATUS];
    [trackingResult.queryResult setValue:signatureName forKey:SIGNATURE_NAME];
    [trackingResult.queryResult setValue:deliveryDate forKey:DELIVERY_DATE];
    }

  • (void)setParameter:(NSString *)parameterName on:(NSMutableDictionary *)parameters fromDocument:(HTMLDocument *)document {
    NSString *cssSelector = [NSString stringWithFormat:@"#%@", parameterName];
    parameters[parameterName] = [[[document firstNodeMatchingSelector:cssSelector] attributes] objectForKey:@"value"];
    }

  • (void)performSecondRequestUsingFirstRequest:(NSString *)requestBodyAsString
    usingLinkPrefix:(NSString *)linkPrefix
    andUsinglinkTerminationString:(NSString *)linkTerminationString
    andUsingCallback:(void (^)(NSString *requestBody))callback {
    NSRange secondRequestStartIndex = [requestBodyAsString rangeOfString:linkPrefix];
    if (secondRequestStartIndex.location == NSNotFound) {
    NSLog(@"Can not find index of %@", linkPrefix);
    callback(nil);
    return;
    }
    NSRange terminationSearchRange = NSMakeRange(secondRequestStartIndex.location, ([requestBodyAsString length] - secondRequestStartIndex.location));
    NSRange endIndex = [requestBodyAsString rangeOfString:linkTerminationString
    options:nil
    range:terminationSearchRange];
    if (endIndex.location == NSNotFound) {
    NSLog(@"Can not find index of %@", linkTerminationString);
    callback(nil);
    }
    NSString *requestURLSuffix = [requestBodyAsString substringWithRange:NSMakeRange(secondRequestStartIndex.location, endIndex.location - secondRequestStartIndex.location)];
    requestURLSuffix = [requestURLSuffix stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
    AFHTTPRequestOperationManager *manager = [self getHttpRequestOperationManagerWithHTTP];
    NSString *queryURL = [NSString stringWithFormat:@"%@/%@", HOST, requestURLSuffix];
    [manager GET:queryURL parameters:nil
    success:^void(AFHTTPRequestOperation *operation, id responseObject) {
    callback(operation.responseString);
    }
    failure:^void(AFHTTPRequestOperation *operation, NSError *error) {
    callback(operation.responseString);
    }
    ];

}

  • (NSString *)getTrackingNumberFormat {
    return @"(\d{13}|[a-zA-Z]{2}\d{11})|([A-Z]{2}\d{6}-\d{6})";
    }

@end

`

@drkameleon drkameleon added the bug label Mar 27, 2016
@drkameleon
Owner

Hi, I'll look into it. Are you getting an 'unknown type' error or sth else?

@denizoguz

@drkameleon
it gives "Unexpected: id" error for the line self.id = 2; id is defined on the base class. If I remove that line it gives another error "Unexpected: ^" for the line :(void (^)(TrackingResult *trackingResult))callback {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment