MLTitleDecrapifier.m 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*****************************************************************************
 * MLTitleDecrapifier.m
 * Lunettes
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
 * Copyright (C) 2010-2013 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10
 *          Felix Paul Kühne <fkuehne # videolan.org>
11
 *
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Pierre's avatar
Pierre committed
26 27 28 29 30 31 32

#import "MLTitleDecrapifier.h"

@implementation MLTitleDecrapifier
+ (NSString *)decrapify:(NSString *)string
{
    static NSArray *ignoredWords = nil;
33
    if (!ignoredWords)
Pierre's avatar
Pierre committed
34
        ignoredWords = [[NSArray alloc] initWithObjects:
Pierre's avatar
Pierre committed
35
                        @"xvid", @"h264", @"dvd", @"rip", @"divx", @"[fr]", nil];
36

Pierre's avatar
Pierre committed
37 38 39 40
    NSMutableString *mutableString = [NSMutableString stringWithString:string];
    for (NSString *word in ignoredWords)
        [mutableString replaceOccurrencesOfString:word withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
    [mutableString replaceOccurrencesOfString:@"." withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
41
    [mutableString replaceOccurrencesOfString:@" - " withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
Pierre's avatar
Pierre committed
42 43 44 45 46 47 48 49
    return mutableString;
}

static inline BOOL isDigit(char c)
{
    return c >= '0' && c <= '9';
}

50 51 52 53 54 55 56
// Shortcut to ease reading
static inline unichar c(NSString *string, unsigned index)
{
    return [string characterAtIndex:index];
}


Pierre's avatar
Pierre committed
57 58
+ (BOOL)isTVShowEpisodeTitle:(NSString *)string
{
59
    NSString *str = [string lowercaseString];
Pierre's avatar
Pierre committed
60 61

    // Search for s01e10.
62 63 64 65 66 67 68
    for (int i = 0; i < (int)[str length] - 5; i++) {
        if (c(str, i) == 's' &&
            isDigit(c(str, i+1)) &&
            isDigit(c(str, i+2)) &&
            c(str, i+3) == 'e' &&
            isDigit(c(str, i+4)) &&
            isDigit(c(str, i+5)))
Pierre's avatar
Pierre committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
        {
            return YES;
        }
    }
    return NO;
}

static inline int intFromChar(char n)
{
    return n - '0';
}

static inline NSNumber *numberFromTwoChars(char high, char low)
{
83
    return @(intFromChar(high) * 10 + intFromChar(low));
Pierre's avatar
Pierre committed
84 85 86 87
}

+ (NSDictionary *)tvShowEpisodeInfoFromString:(NSString *)string
{
88 89
    if (!string)
        return nil;
90
    NSString *str = [string lowercaseString];
Pierre's avatar
Pierre committed
91 92

    // Search for s01e10.
93 94 95 96 97 98 99
    for (int i = 0; i < (int)[str length] - 5; i++) {
        if (c(str, i) == 's' &&
            isDigit(c(str, i+1)) &&
            isDigit(c(str, i+2)) &&
            c(str, i+3) == 'e' &&
            isDigit(c(str, i+4)) &&
            isDigit(c(str, i+5)))
Pierre's avatar
Pierre committed
100
        {
101 102 103
            NSNumber *season = numberFromTwoChars(c(str,i+1), c(str,i+2));
            NSNumber *episode = numberFromTwoChars(c(str,i+4), c(str,i+5));
            NSString *tvShowName = i > 0 ? [str substringToIndex:i-1] : nil;
Pierre's avatar
Pierre committed
104
            tvShowName = tvShowName ? [[MLTitleDecrapifier decrapify:tvShowName] capitalizedString] : nil;
105 106 107 108
            NSLog(@"str: %@", str);
            NSString *episodeName = [str substringFromIndex:i+6];
            episodeName = episodeName ? [[MLTitleDecrapifier decrapify:episodeName] capitalizedString] : nil;
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:4];
109 110 111 112 113 114
            if (season)
                [dict setObject:season forKey:@"season"];
            if (episode)
                [dict setObject:episode forKey:@"episode"];
            if (tvShowName)
                [dict setObject:tvShowName forKey:@"tvShowName"];
115 116
            if (episodeName)
                [dict setObject:episodeName forKey:@"tvEpisodeName"];
117
            return [NSDictionary dictionaryWithDictionary:dict];
Pierre's avatar
Pierre committed
118 119 120 121 122 123
        }
    }
    return nil;

}
@end