Java tutorial
/** * Copyright 2016 Philipp Arndt * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.rnd7.libtvdb.util; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.io.IOUtils; public final class EpisodeUtil { private final static List<String> EXCLUDE; private final static List<Pattern> PATTERN; static { EXCLUDE = load("excludes"); PATTERN = load("parsepattern").stream().map(p -> Pattern.compile(p, Pattern.CASE_INSENSITIVE)) .collect(Collectors.toList()); } private EpisodeUtil() { } public static List<EpisodeInfo> parseName(final String name) { try { return parseNameInternal(name); } catch (final IOException e) { final List<EpisodeInfo> result = new ArrayList<EpisodeInfo>(); result.add(new EpisodeInfo(0, 0)); return result; } } private static String filter(final String name) { String result = name; for (final String ex : EXCLUDE) { result = result.replaceAll(ex, ""); } return result; } private static List<EpisodeInfo> parseNameInternal(final String name) throws IOException { final String filtered = filter(name.toLowerCase()); final List<EpisodeInfo> result = new ArrayList<EpisodeInfo>(); for (final Pattern pattern : PATTERN) { final Matcher matcher = pattern.matcher(filtered); boolean match = false; while (matcher.find()) { match = true; if (matcher.groupCount() == 3) { final int season = Integer.parseInt(matcher.group(1)); final int episodeA = Integer.parseInt(matcher.group(2)); final int episodeB = Integer.parseInt(matcher.group(3)); result.add(new EpisodeInfo(season, episodeA)); result.add(new EpisodeInfo(season, episodeB)); } else { final int season = Integer.parseInt(matcher.group(1)); final int episode = Integer.parseInt(matcher.group(2)); result.add(new EpisodeInfo(season, episode)); } } if (match) { break; } } if (result.isEmpty()) { // Fallback for single season series: final Pattern pattern = Pattern.compile(".*(\\d\\d).*", Pattern.CASE_INSENSITIVE); final Matcher matcher = pattern.matcher(filtered); if (matcher.matches()) { final int season = 1; final int episode = Integer.parseInt(matcher.group(1)); result.add(new EpisodeInfo(season, episode)); } } return result; } public static String toName(final List<EpisodeInfo> episodeInfos) { return episodeInfos.stream().map(EpisodeInfo::toString).collect(Collectors.joining(", ")); } private static List<String> load(final String name) { try (InputStream excludes = EpisodeUtil.class.getResourceAsStream(name);) { return IOUtils.readLines(excludes); } catch (final IOException e) { throw new RuntimeException(e); } finally { } } }