MLTitleDecrapifier.m 9.25 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

#import "MLTitleDecrapifier.h"

Felix Paul Kühne's avatar
Felix Paul Kühne committed
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
#ifdef MLKIT_READONLY_TARGET

@implementation MLTitleDecrapifier

+ (NSString *)decrapify:(NSString *)string;
{
    return @"";
}

+ (BOOL)isTVShowEpisodeTitle:(NSString *)string
{
    return NO;
}

+ (NSDictionary *)tvShowEpisodeInfoFromString:(NSString *)string
{
    return @{};
}

+ (NSDictionary *)audioContentInfoFromFile:(MLFile *)file
{
    return @{};
}

@end

#else

Pierre's avatar
Pierre committed
57 58 59
@implementation MLTitleDecrapifier
+ (NSString *)decrapify:(NSString *)string
{
60 61 62
    if (string == nil)
        return nil;

Pierre's avatar
Pierre committed
63
    static NSArray *ignoredWords = nil;
64
    if (!ignoredWords)
Pierre's avatar
Pierre committed
65
        ignoredWords = [[NSArray alloc] initWithObjects:
66
                        @"xvid", @"h264", @"dvd", @"rip", @"divx", @"[fr]", @"720p", @"1080i", @"1080p", @"x264", @"hdtv", @"aac", @"bluray", nil];
67

Pierre's avatar
Pierre committed
68 69 70 71
    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])];
72
    [mutableString replaceOccurrencesOfString:@"_" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
73
    [mutableString replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
74
    [mutableString replaceOccurrencesOfString:@"-" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
75 76
    [mutableString replaceOccurrencesOfString:@"[]" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
    [mutableString replaceOccurrencesOfString:@"()" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
77 78 79 80 81 82 83 84

    NSString *staticString = [NSString stringWithString:mutableString];
    mutableString = nil;

    while ([staticString rangeOfString:@"  "].location != NSNotFound)
        staticString = [staticString stringByReplacingOccurrencesOfString:@"  " withString:@" "];

    if (staticString.length > 2) {
85 86 87 88 89 90
        @try {
            if ([staticString characterAtIndex:0] == 0x20)
                staticString = [staticString substringFromIndex:1];
        }
        @catch (NSException *exception) {
        }
91 92 93
    }

    return staticString;
Pierre's avatar
Pierre committed
94 95 96 97 98 99 100
}

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

101 102 103
// Shortcut to ease reading
static inline unichar c(NSString *string, unsigned index)
{
104 105 106 107 108 109
    @try {
        return [string characterAtIndex:index];
    }
    @catch (NSException *exception) {
        return 0x00;
    }
110 111 112
}


Pierre's avatar
Pierre committed
113 114
+ (BOOL)isTVShowEpisodeTitle:(NSString *)string
{
115
    NSString *str = [string lowercaseString];
Pierre's avatar
Pierre committed
116 117

    // Search for s01e10.
118 119 120 121 122 123 124
    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
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        {
            return YES;
        }
    }
    return NO;
}

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

static inline NSNumber *numberFromTwoChars(char high, char low)
{
139
    return @(intFromChar(high) * 10 + intFromChar(low));
Pierre's avatar
Pierre committed
140 141
}

142 143 144 145 146
static inline NSNumber *numberFromThreeChars(char high, char mid, char low)
{
    return @(intFromChar(high) * 100 + intFromChar(mid) * 10 + intFromChar(low));
}

Pierre's avatar
Pierre committed
147 148
+ (NSDictionary *)tvShowEpisodeInfoFromString:(NSString *)string
{
149 150
    if (!string)
        return nil;
151
    NSString *str = [string lowercaseString];
152
    NSUInteger stringLength = [str length];
Pierre's avatar
Pierre committed
153

154 155 156
    if (stringLength < 6)
        return nil;

157 158 159 160 161 162 163 164
    BOOL successfulSearch = NO;
    NSMutableDictionary *mutableDict;
    NSNumber *season;
    NSNumber *episode;
    NSString *tvShowName;
    NSString *episodeName;

    // Search for S00E00*
165
    for (unsigned int i = 0; i < stringLength - 5; i++) {
166 167 168 169 170 171
        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
172
        {
173 174
            season = numberFromTwoChars(c(str,i+1), c(str,i+2));

175 176 177 178
            if (isDigit(c(str, i+6)))
                episode = numberFromThreeChars(c(str,i+4), c(str,i+5), c(str,i+6));
            else
                episode = numberFromTwoChars(c(str,i+4), c(str,i+5));
179
            tvShowName = i > 0 ? [str substringToIndex:i] : NSLocalizedString(@"UNTITLED_SHOW", @"");
Pierre's avatar
Pierre committed
180
            tvShowName = tvShowName ? [[MLTitleDecrapifier decrapify:tvShowName] capitalizedString] : nil;
181

182
            episodeName = stringLength > i + 4 ? [str substringFromIndex:i+6] : nil;
183 184
            episodeName = episodeName ? [MLTitleDecrapifier decrapify:episodeName] : nil;

185
            successfulSearch = YES;
186
            goto returnThings;
Pierre's avatar
Pierre committed
187 188 189
        }
    }

190 191
    // search for 0x00
    if (!successfulSearch) {
192
        for (unsigned int i = 0; i < stringLength - 3; i++) {
193 194 195 196 197
            if (isDigit(c(str, i)) &&
                c(str, i+1) == 'x' &&
                isDigit(c(str, i+2)) &&
                isDigit(c(str, i+3)))
            {
198 199
                season = @(intFromChar(c(str,i)));
                episode = numberFromTwoChars(c(str,i+2), c(str,i+3));
200

201
                tvShowName = i > 0 ? [str substringToIndex:i] : NSLocalizedString(@"UNTITLED_SHOW", @"");
202 203
                tvShowName = tvShowName ? [[MLTitleDecrapifier decrapify:tvShowName] capitalizedString] : nil;

204
                episodeName = stringLength > i + 4 ? [str substringFromIndex:i+4] : nil;
205 206
                episodeName = episodeName ? [MLTitleDecrapifier decrapify:episodeName] : nil;

207
                successfulSearch = YES;
208
                goto returnThings;
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
    // search for S0E00*
    if (!successfulSearch) {
        for (unsigned int i = 0; i < stringLength - 4; i++) {
            if (c(str, i) == 's' &&
                isDigit(c(str, i+1)) &&
                c(str, i+2) == 'e' &&
                isDigit(c(str, i+3)) &&
                isDigit(c(str, i+4)))
            {
                season = [NSNumber numberWithInt:intFromChar(c(str,i+1))];

                if (isDigit(c(str, i+5)))
                    episode = numberFromThreeChars(c(str,i+3), c(str,i+4), c(str,i+5));
                else
                    episode = numberFromTwoChars(c(str,i+3), c(str,i+4));
                tvShowName = i > 0 ? [str substringToIndex:i] : NSLocalizedString(@"UNTITLED_SHOW", @"");
                tvShowName = tvShowName ? [[MLTitleDecrapifier decrapify:tvShowName] capitalizedString] : nil;

                episodeName = stringLength > i + 4 ? [str substringFromIndex:i+5] : nil;
                episodeName = episodeName ? [MLTitleDecrapifier decrapify:episodeName] : nil;

                successfulSearch = YES;
                goto returnThings;
            }
        }
    }

returnThings:
241
    if (successfulSearch) {
242 243 244 245 246 247 248 249 250 251
        mutableDict = [[NSMutableDictionary alloc] initWithCapacity:4];
        if (season)
            mutableDict[@"season"] = season;
        if (episode)
            mutableDict[@"episode"] = episode;
        if (tvShowName && ![tvShowName isEqualToString:@" "])
            mutableDict[@"tvShowName"] = tvShowName;
        if (episodeName.length > 0 && ![episodeName isEqualToString:@" "])
            mutableDict[@"tvEpisodeName"] = [episodeName capitalizedString];

252
        return [NSDictionary dictionaryWithDictionary:mutableDict];
253
    }
254 255

    return nil;
Pierre's avatar
Pierre committed
256
}
257 258 259 260 261 262

+ (NSDictionary *)audioContentInfoFromFile:(MLFile *)file
{
    if (!file)
        return nil;

263 264 265 266 267 268 269 270
    NSString *title = file.title;
    NSArray *components = [title componentsSeparatedByString:@" "];
    if (components.count > 0) {
        if ([components[0] intValue] > 0)
            title = [self decrapify:[title stringByReplacingOccurrencesOfString:components[0] withString:@""]];
    } else
        title = [self decrapify:title];

271 272 273 274
    if (title != nil)
        return @{VLCMetaInformationTitle: title};

    return @{};
275 276
}

Pierre's avatar
Pierre committed
277
@end
Felix Paul Kühne's avatar
Felix Paul Kühne committed
278 279

#endif