Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 au.id.hazelwood.xmltvguidebuilder.runner; import au.id.hazelwood.xmltvguidebuilder.binding.BindingService; import au.id.hazelwood.xmltvguidebuilder.config.Config; import au.id.hazelwood.xmltvguidebuilder.config.ConfigFactory; import au.id.hazelwood.xmltvguidebuilder.config.InvalidConfigException; import au.id.hazelwood.xmltvguidebuilder.grabber.Grabber; import au.id.hazelwood.xmltvguidebuilder.mapper.XmltvMapper; import au.id.hazelwood.xmltvguidebuilder.model.ChannelListings; import au.id.hazelwood.xmltvguidebuilder.postprocessor.ListingVerifier; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.FileWriterWithEncoding; import org.apache.commons.lang.time.StopWatch; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.xml.bind.JAXBException; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.Writer; import static au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.formatDurationWords; import static au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.toISODateTime; /** * @author Ricky Hazelwood * @version 1.0 */ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final int BACKUP_COUNT = 3; private final ConfigFactory configFactory; private final Grabber grabber; private final XmltvMapper xmltvMapper; private final BindingService bindingService; private final ListingVerifier listingVerifier; public App() throws Exception { configFactory = new ConfigFactory(); grabber = new Grabber(); xmltvMapper = new XmltvMapper("xmltvguidebuilder", "1.0.0"); bindingService = new BindingService("org.xmltv.schema"); listingVerifier = new ListingVerifier(); } public void run(File configFile, File outFile) throws InvalidConfigException, JAXBException, IOException { LOGGER.info("Running XMLTV Guide Builder"); StopWatch watch = new StopWatch(); watch.start(); LOGGER.info("Reading config file '{}'", configFile.getCanonicalPath()); Config config = configFactory.create(configFile); DateTime today = new DateTime().withTimeAtStartOfDay(); DateTime from = today.plusDays(config.getOffset()); DateTime to = from.plusDays(config.getDays()); LOGGER.info("Getting listing from foxtel grabber between {} and {}", toISODateTime(from), toISODateTime(to)); ChannelListings channelListings = grabber.getListing(config, from, to, config.getChannelConfigs()); LOGGER.info("Verifying listings for all channels between {} and {}", new Object[] { toISODateTime(from), toISODateTime(to) }); DateTime subsetTo = from.plusDays(7).isBefore(to) ? from.plusDays(7) : to; listingVerifier.verifyListing(channelListings, from, to, subsetTo); LOGGER.info("Backup old output files"); backupOldOutputFiles(outFile); LOGGER.info("Writing result to {}", outFile.getCanonicalPath()); Writer writer = new BufferedWriter(new FileWriterWithEncoding(outFile, "UTF-8")); bindingService.marshal(xmltvMapper.toXmltv(channelListings), writer); IOUtils.closeQuietly(writer); watch.stop(); LOGGER.info("XMLTV Guide Builder successful in {}", formatDurationWords(watch.getTime())); } private void backupOldOutputFiles(File outFile) throws IOException { for (int i = BACKUP_COUNT; i > 0; i--) { File backup = new File(outFile.getPath() + "." + i); File previousBackup = i > 1 ? new File(outFile.getPath() + "." + (i - 1)) : outFile; if (previousBackup.exists()) { FileUtils.copyFile(previousBackup, backup); } } } }