diff --git a/zap2it-GuideScrape.py b/zap2it-GuideScrape.py index 48235af..8ec4ff4 100644 --- a/zap2it-GuideScrape.py +++ b/zap2it-GuideScrape.py @@ -15,7 +15,7 @@ #Additional Libraries for Parameter Parsing import sys, getopt #Libraries for historical copies -import datetime +import datetime, os def sanitizeData(data): #https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents @@ -106,17 +106,15 @@ def buildXMLDate(inputDateString): optConfigFile = arg elif opt in ("-o","--ofile"): optGuideFile = arg -print("Loading config: ", optConfigFile, " and outputting: ", optGuideFile) - - - +print("Loading config: ", optConfigFile, " and outputting: ", optGuideFile) #Configuration loading Config = configparser.ConfigParser() Config Config.read(optConfigFile) + #Build authentication request url = 'https://tvlistings.zap2it.com/api/user/login' parameters = { @@ -207,4 +205,11 @@ def buildXMLDate(inputDateString): file.write(guideXML.encode('utf8')) file.close() -#TODO Clean old files? +#Clean old files +outputFilePath = os.path.abspath(optGuideFile) +outputDir = os.path.dirname(outputFilePath) +for item in os.listdir(outputDir): + fileName = os.path.join(outputDir,item) + if os.path.isfile(fileName) & item.endswith('.xmltv') & (os.stat(fileName).st_mtime < time.time() - (int(Config.get("prefs","historicalGuideDays")) * 86400)): + os.remove(fileName) +sys.exit() diff --git a/zap2itconfig.ini.dist b/zap2itconfig.ini.dist index 669e5ca..59e6a62 100644 --- a/zap2itconfig.ini.dist +++ b/zap2itconfig.ini.dist @@ -4,3 +4,4 @@ Password: examplePass [prefs] country: USA zipCode: 55555 +historicalGuideDays: 14