Example #1
0
def parseSeriesXML(seriesXMLFilename):
    # open xml file download it if it doesn't exist
    try:
        xmlFile = utilities.openFile(seriesXMLFilename)
    except IOError as e:
        logger.warning("There is no local information for series: {0}".format(aid))
        downloadSeriesXML(seriesXMLFilename, aid, version)
        xmlFile = utilities.openFile(seriesXMLFilename)
    tree = ET.parse(xmlFile)
    root = tree.getroot()
    episodes = root.find('episodes')

    #if the episodes tag is none check for error
    if episodes is None:
        if root.tag == 'error':
            if utilities.checkFileAge(seriesXMLFilename, 0.20):
                logger.debug("File '{0}' is several hours old re-downloading".format(seriesXMLFilename))
                downloadSeriesXML(seriesXMLFilename, aid, version)
                try:
                    xmlFile = utilities.openFile(seriesXMLFilename)
                except IOError as e:
                    logger.error("IOError[{0}] in file {1}: {2}".format(e.errno, seriesXMLFilename, e.strerror))
                    return
                tree = ET.parse(xmlFile)
                root = tree.getroot()
                episodes = root.find('episodes')
                if episodes is None:
                    if root.tag == 'error':
                        logger.error("Error parsing through {0}: {1}".format(seriesXMLFilename, root.text))
                        return
                    else:
                        logger.error('unknown error occured parsing {0}'.format(seriesXMLFilename))
                        return
            else:
                logger.error("Error parsing through {0}: {1}".format(seriesXMLFilename, root.text))
                return
        else:
            logger.error('unknown error occured parsing {0}'.format(seriesXMLFilename))
            return

    allEpInfo = []
    for episode in episodes.iterfind('episode'):
        epInfo=[]
        episodeNo = episode.find('epno')
        if (episodeNo.get('type') == '1'):
            title = episodeNo.text
            title = int(title)
            epInfo.append('{0:0=2d}'.format(title))
            for title in episode.findall('title'):
                langAttrib = '{http://www.w3.org/XML/1998/namespace}lang'
                if title.get(langAttrib) == 'en':
                    t = title.text
                    # replace any |/\ with a comma
                    t = re.sub('\s?[\\/|]', ',', t)
                    epInfo.append(t)
                    allEpInfo.append(epInfo)
    return allEpInfo
Example #2
0
def generatePrefNameCSV(xmlFilename, ShowList):
    try:
        xmlFileObject = utilities.openFile(xmlFilename)
    except IOError as e:
        url = 'http://anidb.net/api/animetitles.xml.gz'
        utilities.downloadFile(url, xmlFilename)
        xmlFileObject = utilities.openFile(xmlFilename)
        logger.info("'{0}' not found: downloading".format(xmlFilename))

    #open xmlfile once here for faster parsing
    tree = ET.parse(xmlFileObject)
    root = tree.getroot()
    preferedNames=[]
    for show in ShowList:
        showMatches = findShowMatches(show, root)
        if (len(showMatches) == 0):
            # check date of latest animetitles.xml.gz
            # get new one if it was not already downloaded today
            if utilities.checkFileAge(xmlFilename):
                url = 'http://anidb.net/api/animetitles.xml.gz'
                utilities.downloadFile(url, xmlFilename)
                # search through xml file again
                xmlFileObject = utilities.openFile(xmlFilename)
                tree = ET.parse(xmlFileObject)
                root = tree.getroot()
                showMatches = findShowMatches(show, root)
        #get user to pick show from list or write a warning and do nothing if silent is on
        if (len(showMatches) > 1):
            if not silent:
                print('{0} matches for show {1}'.format(
                    len(showMatches), show))
                showChoice = listChoice(showMatches)
                showChoice.append(show)
                preferedNames.append(showChoice)
                showMatches = [showChoice]
                logger.debug("{0} Matched: '{1}' with {2}".format(xmlFilename, show, showChoice))
            else:
                logger.warning("Multiple matches found for title '{0}' silent mode is on".format(title))
                showMatches = []
        elif (len(showMatches) == 1):
            showMatches[0].append(show)
            preferedNames.append(showMatches[0])
            logger.debug("{0} Matched: '{1}' with {2}".format(xmlFilename, show, showMatches[0]))
        else:
            logger.error("Anime '{0}' not found".format(show))

    return preferedNames
Example #3
0
def generateFilenamesSeries(xmlFilename, outDir,
                            seriesName, filenames, titleList):
        # sort descending for faster matching later
        filenames = sorted(filenames, reverse=True)
        titleList = sorted(titleList, reverse=True)

        # if the highest file episode is > the highest episode from the xmlFile
        if (filenames[0][0] > titleList[0][0]):
            if utilities.checkFileAge(xmlFilename, 0.5):
                downloadSeriesXML(xmlFilename, aid, version)
                logger.info("Downloading newer file '{0}' for show {1}".format(xmlFilename, seriesName))
                try:
                    xmlFile = utilities.openFile(xmlFilename)
                except IOError as e:
                    logger.error("IOError[{0}] in file {1}: {2}".format(e.errno, filename, e.strerror))
                titleList = parseSeriesXML(xmlFilename)
                titleList = sorted(titleList, reverse=True)
            else:
                logger.error('{0} xml file is up to date'.format(seriesName))

        #start generating new names
        newNames = []
        for fileEp, file in filenames:
            filename, ext = os.path.splitext(file)
            path, filename = os.path.split(file)

            # checks if file is and ending or opening
            if fileEp[0] == 'E' or fileEp[0] == 'e':
                    m = re.search('\d{1,2}',fileEp)
                    try:
                        ep = m.group(0)
                        ep = '{0:0=2d}'.format(int(ep))
                    except AttributeError:
                        ep = '01'
                    newFilename = '{0} - ED{1}{2}'.format(
                        seriesName, ep, ext)
                    newFilename = os.path.join(outDir, seriesName, newFilename)
                    newNames.append([file, newFilename])
                    continue
            elif fileEp[0] == 'O' or fileEp[0] == 'o':
                m = re.search('\d{1,2}',fileEp)
                try:
                    ep = m.group(0)
                    ep = '{0:0=2d}'.format(int(ep))
                except AttributeError:
                    ep = '01'
                newFilename = '{0} - OP{1}{2}'.format(
                    seriesName, ep, ext)
                newFilename = os.path.join(outDir, seriesName, newFilename)
                newNames.append([file, newFilename])
                continue
            
            # If it isn't then it is an episode
            for ep, title in titleList:
                if fileEp == ep:
                    newFilename = '{0} - {1} - {2}{3}'.format(
                        seriesName, ep, title, ext)
                    newFilename = os.path.join(outDir, seriesName, newFilename)
                    newNames.append([file, newFilename])
                    break
                elif (fileEp > ep):
                    title = 'Episode {0}'.format(fileEp)
                    newFilename = '{0} - {1} - {2}{3}'.format(
                        seriesName, fileEp, title, ext)
                    newFilename = os.path.join(outDir, seriesName, newFilename)
                    newNames.append([file, newFilename])
        return newNames