From 9ab33c169eecfe246d3b2616df8f6b74bb852919 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 7 Apr 2023 22:55:27 +0200 Subject: [PATCH] add directory parameter --- insert_wordpress.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/insert_wordpress.py b/insert_wordpress.py index db2e8a0..2c29e80 100644 --- a/insert_wordpress.py +++ b/insert_wordpress.py @@ -15,13 +15,15 @@ class WPimport: def fromDirectory(self, directory): directory = "{0}/archives".format(directory) - subdirectory = self._checkDirectory([], "{0}".format(directory)) - print(subdirectory) - + directories = self._getDirectories([], "{0}".format(directory)) + files = self._getFiles(directories) + self.fromFile(files) + def fromFile(self, files): - for file in files.split(","): + for file in files: if os.path.exists(file): + print(file) with open(file, 'r') as f: content = f.read() soup = BeautifulSoup(content, 'html.parser') @@ -33,12 +35,24 @@ class WPimport: # Private method - def _checkDirectory(self, subdirectory, item): + ## Get all files + + def _getFiles(self, item): + files = [] + for i in item: + for j in os.listdir(i): + if os.path.isfile("{0}/{1}".format(i, j)): + files.append("{0}/{1}".format(i, j)) + return files + + ## Get directories + + def _getDirectories(self, subdirectory, item): sub = subdirectory for i in os.listdir(item): if os.path.isdir("{0}/{1}".format(item, i)): sub.append("{0}/{1}".format(item, i)) - subdirectory = self._checkDirectory(sub, "{0}/{1}".format(item, i)) + subdirectory = self._getDirectories(sub, "{0}/{1}".format(item, i)) return subdirectory ## Add or update featured media @@ -269,7 +283,7 @@ if __name__ == '__main__': basic = HTTPBasicAuth(args.user, password) importWp = WPimport(basic, args.wordpress) if len(args.file) > 0: - importWp.fromFile(args.file) + importWp.fromFile(args.file.split(",")) exit(0) if len(args.directory) > 0: importWp.fromDirectory(args.directory) \ No newline at end of file