def walkDown(self, files, romPath, maxFolderDepth): Logutil.log("Running walkdown on: %s" %romPath, util.LOG_LEVEL_INFO) dirs, newFiles, dirname, filemask = self.getFilesByWildcardExt(romPath) files.extend(newFiles) """ dirname = os.path.dirname(romPath) Logutil.log("dirname: " +dirname, util.LOG_LEVEL_INFO) filemask = os.path.basename(romPath) Logutil.log("filemask: " +filemask, util.LOG_LEVEL_INFO) dirs, filesLocal = xbmcvfs.listdir(dirname) Logutil.log("xbmcvfs dirs: %s" %dirs, util.LOG_LEVEL_INFO) Logutil.log("xbmcvfs files: %s" %filesLocal, util.LOG_LEVEL_INFO) for file in filesLocal: if(fnmatch.fnmatch(file, filemask)): #allFiles = [f.decode(sys.getfilesystemencoding()).encode('utf-8') for f in glob.glob(newRomPath)] file = util.joinPath(dirname, file) files.append(file) """ for dir in dirs: newRomPath = util.joinPath(dirname, dir, filemask) maxFolderDepth = maxFolderDepth -1 if(maxFolderDepth > 0): self.walkDown(files, newRomPath, maxFolderDepth) return files
def getFilesByWildcardExt(self, pathName): Logutil.log('Begin getFilesByWildcard. pathName = %s' %pathName, util.LOG_LEVEL_INFO) files = [] dirname = os.path.dirname(pathName) Logutil.log("dirname: " +dirname, util.LOG_LEVEL_INFO) filemask = os.path.basename(pathName) #HACK: escape [] for use with fnmatch filemask = filemask.replace('[', '[[]') filemask = filemask.replace(']', '[]]') #This might be stupid but it was late... filemask = filemask.replace('[[[]]', '[[]') Logutil.log("filemask: " +filemask, util.LOG_LEVEL_INFO) dirs, filesLocal = xbmcvfs.listdir(dirname) Logutil.log("xbmcvfs dirs: %s" %dirs, util.LOG_LEVEL_INFO) Logutil.log("xbmcvfs files: %s" %filesLocal, util.LOG_LEVEL_INFO) for file in filesLocal: if(fnmatch.fnmatch(file, filemask)): #allFiles = [f.decode(sys.getfilesystemencoding()).encode('utf-8') for f in glob.glob(newRomPath)] file = util.joinPath(dirname, file) files.append(file) return dirs, files, dirname, filemask """
def download_thumb(self, thumburl, destfilename): log.info("begin download_thumb using requests module: thumburl = %s" % thumburl) # Download file to tmp folder tmp = util.joinPath(util.getTempDir(), os.path.basename(destfilename)) log.info("download_thumb: start downloading to temp file: %s" % tmp) response = requests.get(thumburl, stream=True) log.info("download_thumb: status code = %s" % response.status_code) if response.status_code != 200: log.info( "download_thumb: invalid response status code. Can't download image." ) return with open(tmp, 'wb') as f: response.raw.decode_content = True shutil.copyfileobj(response.raw, f) log.info( "download_thumb: copy from temp file to final destination: %s" % destfilename) # Copy from the tmp folder to the target location xbmcvfs.copy(tmp, destfilename) xbmcvfs.delete(tmp) log.info("end download_thumb")
def getFilesByGameNameIgnoreCase(self, pathname): files = [] dirname = os.path.dirname(pathname) basename = os.path.basename(pathname) #search all Files that start with the first character of game name newpath = util.joinPath(dirname, basename[0].upper() +'*') filesUpper = glob.glob(newpath) newpath = util.joinPath(dirname, basename[0].lower() +'*') filesLower = glob.glob(newpath) allFiles = filesUpper + filesLower for file in allFiles: if(pathname.lower() == file.lower()): Logutil.log('Found path "%s" by search with ignore case.' %pathname, util.LOG_LEVEL_WARNING) files.append(file) return files
def getFilesByGameNameIgnoreCase(self, pathname): files = [] dirname = os.path.dirname(pathname) basename = os.path.basename(pathname) # search all Files that start with the first character of game name newpath = util.joinPath(dirname, basename[0].upper() + '*') filesUpper = glob.glob(newpath) newpath = util.joinPath(dirname, basename[0].lower() + '*') filesLower = glob.glob(newpath) allFiles = filesUpper + filesLower for file in allFiles: if pathname.lower() == file.lower(): log.warn("Found path '%s' by search with ignore case." % pathname) files.append(file) return files
def getFilesByGameNameIgnoreCase(self, pathname): files = [] dirname = os.path.dirname(pathname) basename = os.path.basename(pathname) # search all Files that start with the first character of game name newpath = util.joinPath(dirname, basename[0].upper() + '*') filesUpper = glob.glob(newpath) newpath = util.joinPath(dirname, basename[0].lower() + '*') filesLower = glob.glob(newpath) allFiles = filesUpper + filesLower for f in allFiles: if pathname.lower() == f.lower(): log.warn("Found path '%s' by search with ignore case." % pathname) files.append(f) return files
def walkDown(self, files, romPath, maxFolderDepth): Logutil.log("Running walkdown on: %s" %romPath, util.LOG_LEVEL_INFO) dirs, newFiles, dirname, filemask = self.getFilesByWildcardExt(romPath) files.extend(newFiles) if(maxFolderDepth > 0): for dir in dirs: newRomPath = util.joinPath(dirname, dir, filemask) self.walkDown(files, newRomPath, (maxFolderDepth -1)) return files
def walkDown(self, files, romPath, maxFolderDepth): log.info("Running walkdown on: %s" % romPath) dirs, newFiles, dirname, filemask = self.getFilesByWildcardExt(romPath) files.extend(newFiles) for dir in dirs: newRomPath = util.joinPath(dirname, dir, filemask) maxFolderDepth = maxFolderDepth - 1 if (maxFolderDepth > 0): self.walkDown(files, newRomPath, maxFolderDepth) return files
def walkDown(self, files, romPath, maxFolderDepth): log.info("Running walkdown on: %s" % romPath) dirs, newFiles, dirname, filemask = self.getFilesByWildcardExt(romPath) files.extend(newFiles) for directory in dirs: newRomPath = util.joinPath(dirname, directory, filemask) maxFolderDepth = maxFolderDepth - 1 if maxFolderDepth > 0: self.walkDown(files, newRomPath, maxFolderDepth) return files
def walkDown(self, files, romPath, maxFolderDepth): Logutil.log("Running walkdown on: %s" %romPath, util.LOG_LEVEL_INFO) dirs, newFiles, dirname, filemask = self.getFilesByWildcardExt(romPath) files.extend(newFiles) for dir in dirs: newRomPath = util.joinPath(dirname, dir, filemask) maxFolderDepth = maxFolderDepth -1 if(maxFolderDepth > 0): self.walkDown(files, newRomPath, maxFolderDepth) return files
def writeNfoElementToFile(self, root, platform, romFile, gameNameFromFile, nfoFile): # write file try: self.indentXml(root) tree = ElementTree(root) log.info(u"Writing NFO file {0}".format(nfoFile)) localFile = util.joinPath(util.getTempDir(), os.path.basename(nfoFile)) tree.write(localFile, encoding="UTF-8", xml_declaration=True) xbmcvfs.copy(localFile, nfoFile) xbmcvfs.delete(localFile) except Exception as exc: log.warn(u"Error: Cannot write game nfo for {0}: {1}".format(gameNameFromFile, exc))
def writeNfoElementToFile(self, root, platform, romFile, gameNameFromFile, nfoFile): # write file try: self.indentXml(root) tree = ElementTree(root) log.info(u"Writing NFO file {0}".format(nfoFile)) localFile = util.joinPath(util.getTempDir(), os.path.basename(nfoFile)) tree.write(localFile, encoding="UTF-8", xml_declaration=True) xbmcvfs.copy(localFile, nfoFile) xbmcvfs.delete(localFile) except Exception as exc: log.warn(u"Error: Cannot write game nfo for {0}: {1}".format( gameNameFromFile, exc))
def getFilesByWildcardExt(self, pathName): log.info("Begin getFilesByWildcard. pathName = %s" % pathName) files = [] dirs = [] dirname = os.path.dirname(pathName) log.info("dirname: %s" % dirname) filemask = os.path.basename(pathName) # HACK: escape [] for use with fnmatch filemask = filemask.replace('[', '[[]') filemask = filemask.replace(']', '[]]') # This might be stupid but it was late... filemask = filemask.replace('[[[]]', '[[]') log.info("filemask: %s" % filemask) dirsLocal, filesLocal = xbmcvfs.listdir(dirname) log.info("xbmcvfs dirs: %s" % dirs) log.info("xbmcvfs files: %s" % filesLocal) for dir in dirsLocal: if type(dir) == str: dirs.append(dir.decode('utf-8')) else: dirs.append(dir) for file in filesLocal: if fnmatch.fnmatch(file, filemask): # allFiles = [f.decode(sys.getfilesystemencoding()).encode('utf-8') for f in glob.glob(newRomPath)] if type(file) == str: file = file.decode('utf-8') file = util.joinPath(dirname, file) # return unicode filenames so relating scraping actions can handle them correctly files.append(file) return dirs, files, dirname, filemask """
def download_thumb(self, thumburl, destfilename): log.info("begin download_thumb using requests module: thumburl = %s" % thumburl) # Download file to tmp folder tmp = util.joinPath(util.getTempDir(), os.path.basename(destfilename)) log.info("download_thumb: start downloading to temp file: %s" % tmp) response = requests.get(thumburl, stream=True) log.info("download_thumb: status code = %s" %response.status_code) if response.status_code != 200: log.info("download_thumb: invalid response status code. Can't download image.") return with open(tmp, 'wb') as f: response.raw.decode_content = True shutil.copyfileobj(response.raw, f) log.info("download_thumb: copy from temp file to final destination: %s" % destfilename) # Copy from the tmp folder to the target location xbmcvfs.copy(tmp, destfilename) xbmcvfs.delete(tmp) log.info("end download_thumb")
def getFilesByWildcardExt(self, pathName): log.info("Begin getFilesByWildcard. pathName = %s" % pathName) files = [] dirs = [] dirname = os.path.dirname(pathName) log.info("dirname: %s" % dirname) filemask = os.path.basename(pathName) # HACK: escape [] for use with fnmatch filemask = filemask.replace('[', '[[]') filemask = filemask.replace(']', '[]]') # This might be stupid but it was late... filemask = filemask.replace('[[[]]', '[[]') log.info("filemask: %s" % filemask) dirsLocal, filesLocal = xbmcvfs.listdir(dirname) log.info("xbmcvfs dirs: %s" % dirs) log.info("xbmcvfs files: %s" % filesLocal) for dir in dirsLocal: if type(dir) == str: dirs.append(dir.decode('utf-8')) else: dirs.append(dir) for file in filesLocal: if fnmatch.fnmatch(file, filemask): # allFiles = [f.decode(sys.getfilesystemencoding()).encode('utf-8') for f in glob.glob(newRomPath)] if type(file) == str: file = file.decode('utf-8') file = util.joinPath(dirname, file) # return unicode filenames so relating scraping actions can handle them correctly files.append(file) return dirs, files, dirname, filemask """
try: if(dialogDict != ''): progDialogRCHeader = dialogDict["dialogHeaderKey"] gamenameFromFile = dialogDict["gameNameKey"] scraperSiteName = dialogDict["scraperSiteKey"] fileCount = dialogDict["fileCountKey"] gui.writeMsg(progDialogRCHeader, util.localize(40023) +": " +gamenameFromFile, str(scraperSiteName[thumbKey]) + " - downloading art", fileCount) except: pass # fetch thumbnail and save to filepath try: target = fileName if(fileName.startswith('smb://')): #download file to local folder and copy it to smb path with xbmcvfs target = util.joinPath(util.getTempDir(), os.path.basename(fileName)) req = urllib2.Request(thumbUrl) req.add_unredirected_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31') f = open(target,'wb') f.write(urllib2.urlopen(req).read()) f.close() if(fileName.startswith('smb://')): xbmcvfs.copy(target, fileName) xbmcvfs.delete(target) except Exception, (exc): # xbmcgui.Dialog().ok(util.localize(35012), util.localize(35011)) Logutil.log("Could not create file: '%s'. Error message: '%s'" %(str(fileName), str(exc)), util.LOG_LEVEL_ERROR) return False, artworkurls
class NfoWriter: Settings = util.getSettings() def __init__(self): pass def exportLibrary(self, gui): Logutil.log("Begin exportLibrary", util.LOG_LEVEL_INFO) gdb = gui.gdb romCollections = gui.config.romCollections progressDialog = dialogprogress.ProgressDialogGUI() progressDialog.writeMsg(util.localize(32169), "", "") continueExport = True rccount = 1 for romCollection in gui.config.romCollections.values(): progDialogRCHeader = util.localize(32170) + " (%i / %i): %s" % ( rccount, len(romCollections), romCollection.name) rccount = rccount + 1 Logutil.log("export Rom Collection: " + romCollection.name, util.LOG_LEVEL_INFO) gameCount = 1 #get all games for this Rom Collection games = Game(gdb).getFilteredGames(romCollection.id, 0, 0, 0, False, '0 = 0') progressDialog.itemCount = len(games) + 1 for gameRow in games: gamename = self.getGameProperty(gameRow[util.ROW_NAME]) continueExport = progressDialog.writeMsg( progDialogRCHeader, util.localize(32171) + ": " + str(gamename), "", gameCount) if (not continueExport): Logutil.log('Game export canceled by user', util.LOG_LEVEL_INFO) break gameCount = gameCount + 1 plot = self.getGameProperty(gameRow[util.GAME_description]) publisher = self.getGamePropertyFromCache( gameRow, gui.publisherDict, util.GAME_publisherId, util.ROW_NAME) developer = self.getGamePropertyFromCache( gameRow, gui.developerDict, util.GAME_developerId, util.ROW_NAME) year = self.getGamePropertyFromCache(gameRow, gui.yearDict, util.GAME_yearId, util.ROW_NAME) genreList = [] try: cachingOptionStr = self.Settings.getSetting( util.SETTING_RCB_CACHINGOPTION) if (cachingOptionStr == 'CACHEALL'): genre = gui.genreDict[gameRow[util.ROW_ID]] else: genres = Genre(gdb).getGenresByGameId( gameRow[util.ROW_ID]) if (genres != None): for i in range(0, len(genres)): genreRow = genres[i] genreList.append(genreRow[util.ROW_NAME]) except: pass players = self.getGameProperty(gameRow[util.GAME_maxPlayers]) rating = self.getGameProperty(gameRow[util.GAME_rating]) votes = self.getGameProperty(gameRow[util.GAME_numVotes]) url = self.getGameProperty(gameRow[util.GAME_url]) region = self.getGameProperty(gameRow[util.GAME_region]) media = self.getGameProperty(gameRow[util.GAME_media]) perspective = self.getGameProperty( gameRow[util.GAME_perspective]) controller = self.getGameProperty( gameRow[util.GAME_controllerType]) originalTitle = self.getGameProperty( gameRow[util.GAME_originalTitle]) alternateTitle = self.getGameProperty( gameRow[util.GAME_alternateTitle]) version = self.getGameProperty(gameRow[util.GAME_version]) #user settings isFavorite = self.getGameProperty( gameRow[util.GAME_isFavorite]) launchCount = self.getGameProperty( gameRow[util.GAME_launchCount]) romFiles = File(gdb).getRomsByGameId(gameRow[util.ROW_ID]) romFile = '' if (romFiles != None and len(romFiles) > 0): romFile = romFiles[0][0] gamenameFromFile = helper.getGamenameFromFilename( romFile, romCollection) artworkfiles = {} artworkurls = [] self.createNfoFromDesc( gamename, plot, romCollection.name, publisher, developer, year, players, rating, votes, url, region, media, perspective, controller, originalTitle, alternateTitle, version, genreList, isFavorite, launchCount, romFile, gamenameFromFile, artworkfiles, artworkurls) progressDialog.writeMsg("", "", "", -1) del progressDialog def createNfoFromDesc(self, gamename, plot, romCollectionName, publisher, developer, year, players, rating, votes, url, region, media, perspective, controller, originalTitle, alternateTitle, version, genreList, isFavorite, launchCount, romFile, gameNameFromFile, artworkfiles, artworkurls): Logutil.log("Begin createNfoFromDesc", util.LOG_LEVEL_INFO) root = Element('game') SubElement(root, 'title').text = gamename SubElement(root, 'originalTitle').text = originalTitle SubElement(root, 'alternateTitle').text = alternateTitle SubElement(root, 'platform').text = romCollectionName SubElement(root, 'plot').text = plot SubElement(root, 'publisher').text = publisher SubElement(root, 'developer').text = developer SubElement(root, 'year').text = year for genre in genreList: SubElement(root, 'genre').text = str(genre) SubElement(root, 'detailUrl').text = url SubElement(root, 'maxPlayer').text = players SubElement(root, 'region').text = region SubElement(root, 'media').text = media SubElement(root, 'perspective').text = perspective SubElement(root, 'controller').text = controller SubElement(root, 'version').text = version SubElement(root, 'rating').text = rating SubElement(root, 'votes').text = votes SubElement(root, 'isFavorite').text = isFavorite SubElement(root, 'launchCount').text = launchCount for artworktype in artworkfiles.keys(): local = '' online = '' try: local = artworkfiles[artworktype][0] online = str(artworkurls[artworktype.name]) except: pass try: SubElement(root, 'thumb', { 'type': artworktype.name, 'local': local }).text = online except Exception, (exc): Logutil.log('Error writing artwork url: ' + str(exc), util.LOG_LEVEL_WARNING) pass #write file try: util.indentXml(root) tree = ElementTree(root) nfoFile = self.getNfoFilePath(romCollectionName, romFile, gameNameFromFile) if (nfoFile != ''): if (nfoFile.startswith('smb://')): localFile = util.joinPath(util.getTempDir(), os.path.basename(nfoFile)) tree.write(localFile) xbmcvfs.copy(localFile, nfoFile) xbmcvfs.delete(localFile) else: tree.write(nfoFile) except Exception, (exc): Logutil.log("Error: Cannot write file game.nfo: " + str(exc), util.LOG_LEVEL_WARNING)