MLTitleDecrapifier.m 5.11 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:
35
                        @"xvid", @"h264", @"dvd", @"rip", @"divx", @"[fr]", @"720p", @"1080i", @"1080p", @"x264", @"hdtv", @"aac", 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])];
42
    [mutableString replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
Pierre's avatar
Pierre committed
43 44 45 46 47 48 49 50
    return mutableString;
}

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

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


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

    // Search for s01e10.
63 64 65 66 67 68 69
    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
70 71 72 73 74 75 76 77 78 79 80 81 82 83
        {
            return YES;
        }
    }
    return NO;
}

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

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

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

    // Search for s01e10.
94 95 96 97 98 99 100
    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
101
        {
102 103 104
            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
105
            tvShowName = tvShowName ? [[MLTitleDecrapifier decrapify:tvShowName] capitalizedString] : nil;
106 107 108
            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
        }
    }
    return nil;

}
123 124 125 126 127 128

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

129 130 131 132 133 134 135 136 137
    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];

    return [NSDictionary dictionaryWithObject:title forKey:VLCMetaInformationTitle];
138 139
}

Pierre's avatar
Pierre committed
140
@end