Compare commits
17 Commits
3.0.0
...
5059a15826
Author | SHA1 | Date | |
---|---|---|---|
5059a15826 | |||
20c4adb3cf | |||
74fa87ea73 | |||
03f833a2c3 | |||
9acb620f93 | |||
c6ccf98b1b | |||
95f5203727 | |||
9bbf769b40 | |||
f0b4ba5e27 | |||
47f504beb5 | |||
4b6b06aade | |||
d10867a983 | |||
6fba5f009a | |||
699cecad4f | |||
9f87f38347 | |||
55d62cebfb | |||
193b0e6ef7 |
@@ -10,10 +10,23 @@ from lib.WPImport import WPimport
|
|||||||
from lib.WPExport import WPExport
|
from lib.WPExport import WPExport
|
||||||
from lib.WPRemove import WPRemove
|
from lib.WPRemove import WPRemove
|
||||||
from lib.WPChange import WPChange
|
from lib.WPChange import WPChange
|
||||||
|
from lib.WPMenu import WPMenu
|
||||||
|
|
||||||
def change(index, number, args, logger):
|
def errorRevert(logger, revert, tmp):
|
||||||
changeWp = WPChange(logger=logger, index_name=index, number_thread=number)
|
if revert is True:
|
||||||
changeWp.fromDirectory(args.directory)
|
files_tmp = glob.glob("{0}/*.json".format(tmp))
|
||||||
|
if len(files_tmp) == 0:
|
||||||
|
logger.error("Error revert, because files not found")
|
||||||
|
exit(1)
|
||||||
|
if len(files_tmp) != int(args.parallel):
|
||||||
|
for file_r in files_tmp:
|
||||||
|
os.remove(file_r)
|
||||||
|
logger.error("Error revert, because number files tmp is incompatible with parallel number")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
def change(index, number, args, logger, tmp, revert):
|
||||||
|
changeWp = WPChange(logger=logger, index_name=index, number_thread=number, tmp=tmp)
|
||||||
|
changeWp.fromDirectory(args.directory, revert)
|
||||||
|
|
||||||
del changeWp
|
del changeWp
|
||||||
|
|
||||||
@@ -106,7 +119,7 @@ def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, bas
|
|||||||
del importWp
|
del importWp
|
||||||
|
|
||||||
|
|
||||||
def importDirectory(name_thread, max_thread, directory, logger, parser, wordpress, basic, serial, ssl_wordpress, create, update, image):
|
def importDirectory(name_thread, max_thread, directory, logger, parser, wordpress, basic, serial, ssl_wordpress, create, update, image, revert):
|
||||||
name = "Thread-{0}".format(int(name_thread) + 1)
|
name = "Thread-{0}".format(int(name_thread) + 1)
|
||||||
directory = directory.split(",")
|
directory = directory.split(",")
|
||||||
wordpress = wordpress.split(",")
|
wordpress = wordpress.split(",")
|
||||||
@@ -114,7 +127,7 @@ def importDirectory(name_thread, max_thread, directory, logger, parser, wordpres
|
|||||||
for i in wordpress:
|
for i in wordpress:
|
||||||
importWp = WPimport(name=name, basic=basic, wordpress=i, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image)
|
importWp = WPimport(name=name, basic=basic, wordpress=i, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image)
|
||||||
for j in directory:
|
for j in directory:
|
||||||
importWp.fromDirectory(j, name_thread, max_thread)
|
importWp.fromDirectory(j, name_thread, max_thread, revert)
|
||||||
del importWp
|
del importWp
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -123,7 +136,7 @@ def importDirectory(name_thread, max_thread, directory, logger, parser, wordpres
|
|||||||
exit(1)
|
exit(1)
|
||||||
for i in range(0, len(wordpress)-1):
|
for i in range(0, len(wordpress)-1):
|
||||||
importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image)
|
importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update, no_image=image)
|
||||||
importWp.fromDirectory(directory[i])
|
importWp.fromDirectory(directory[i], name_thread, max_thread, revert)
|
||||||
del importWp
|
del importWp
|
||||||
|
|
||||||
|
|
||||||
@@ -190,8 +203,14 @@ if __name__ == '__main__':
|
|||||||
change_parser.add_argument("--file",
|
change_parser.add_argument("--file",
|
||||||
default="",
|
default="",
|
||||||
help="File")
|
help="File")
|
||||||
|
|
||||||
|
|
||||||
|
menu_parser = subparsers.add_parser("menu")
|
||||||
|
menu_parser.add_argument("--user", help="wordpress user", required=True)
|
||||||
|
menu_parser.add_argument("--password", help="password wordpress's user", default="")
|
||||||
|
menu_parser.add_argument("--file", help="HTML file", default="")
|
||||||
|
menu_parser.add_argument("--canalblog", help="URL Canalblog", default="")
|
||||||
|
menu_parser.add_argument("--wordpress", help="URL Wordpress", required=True)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
logger = logging.getLogger('import export canalblog')
|
logger = logging.getLogger('import export canalblog')
|
||||||
@@ -228,7 +247,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
os.makedirs(args.tmp, exist_ok=True)
|
os.makedirs(args.tmp, exist_ok=True)
|
||||||
|
|
||||||
if args.command == "import" or args.command == "remove":
|
if args.command == "import" or args.command == "remove" or args.command == "menu":
|
||||||
password = args.password
|
password = args.password
|
||||||
if len(args.password) == 0:
|
if len(args.password) == 0:
|
||||||
password = getpass()
|
password = getpass()
|
||||||
@@ -249,8 +268,9 @@ if __name__ == '__main__':
|
|||||||
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
||||||
wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ]
|
wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ]
|
||||||
wait(wait_for, return_when=ALL_COMPLETED)
|
wait(wait_for, return_when=ALL_COMPLETED)
|
||||||
|
errorRevert(logger, args.revert, args.tmp)
|
||||||
wait_for = [
|
wait_for = [
|
||||||
ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image)
|
ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, args.create, args.update, args.image, args.revert)
|
||||||
for i in range(0, int(args.parallel))
|
for i in range(0, int(args.parallel))
|
||||||
]
|
]
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
@@ -260,15 +280,7 @@ if __name__ == '__main__':
|
|||||||
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
||||||
wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ]
|
wait_for = [ ex.submit(remove, i, args.parallel, args, basic, logger, ssl_wordpress) for i in range(0, int(args.parallel)) ]
|
||||||
wait(wait_for, return_when=ALL_COMPLETED)
|
wait(wait_for, return_when=ALL_COMPLETED)
|
||||||
if args.revert is True:
|
errorRevert(logger, args.revert, args.tmp)
|
||||||
files_tmp = glob.glob("{0}/*.json".format(args.tmp))
|
|
||||||
if len(files_tmp) == 0:
|
|
||||||
logger.error("Error revert, because files not found")
|
|
||||||
exit(1)
|
|
||||||
if len(files_tmp) != int(args.parallel):
|
|
||||||
for file_r in files_tmp:
|
|
||||||
os.remove(file_r)
|
|
||||||
|
|
||||||
wait_for = [
|
wait_for = [
|
||||||
ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, args.create, args.update, args.image, args.revert, args.tmp)
|
ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, args.create, args.update, args.image, args.revert, args.tmp)
|
||||||
for i in range(0, int(args.parallel))
|
for i in range(0, int(args.parallel))
|
||||||
@@ -328,11 +340,20 @@ if __name__ == '__main__':
|
|||||||
if len(args.directory) > 0:
|
if len(args.directory) > 0:
|
||||||
try:
|
try:
|
||||||
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
with futures.ThreadPoolExecutor(max_workers=int(args.parallel)) as ex:
|
||||||
wait_for = [ ex.submit(change, i, args.parallel, args, logger) for i in range(0, int(args.parallel)) ]
|
errorRevert(logger, args.revert, args.tmp)
|
||||||
|
wait_for = [ ex.submit(change, i, args.parallel, args, logger, args.tmp, args.revert) for i in range(0, int(args.parallel)) ]
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logger.error("Thread error for remove : {0}".format(err))
|
logger.error("Thread error for remove : {0}".format(err))
|
||||||
if len(args.file) > 0:
|
if len(args.file) > 0:
|
||||||
changeWp = WPChange(logger=logger)
|
changeWp = WPChange(logger=logger)
|
||||||
for filei in args.file.split(","):
|
for filei in args.file.split(","):
|
||||||
changeWp.fromFile(filei)
|
changeWp.fromFile(filei)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if args.command == "menu":
|
||||||
|
menuWp = WPMenu(name="Thread-1", basic=basic, wordpress=args.wordpress, logger=logger, parser=args.parser, ssl_canalblog=ssl_canalblog, ssl_wordpress=ssl_wordpress)
|
||||||
|
if len(args.file) > 0:
|
||||||
|
menuWp.fromFile(args.file)
|
||||||
|
if len(args.canalblog) > 0:
|
||||||
|
menuWp.fromUrl(args.canalblog)
|
||||||
exit(0)
|
exit(0)
|
@@ -4,11 +4,13 @@ import requests, os, logging, re, json
|
|||||||
|
|
||||||
class WPChange:
|
class WPChange:
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self, index_name=1, number_thread=1, logger=None, parser="html.parser"):
|
def __init__(self, index_name=1, number_thread=1, logger=None, parser="html.parser", tmp="/tmp/import_export_canablog"):
|
||||||
self._name = "Thread-{0}".format(index_name)
|
self._name = "Thread-{0}".format(index_name)
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
self._number_thread = number_thread
|
self._number_thread = number_thread
|
||||||
self._parser = parser
|
self._parser = parser
|
||||||
|
self._tmp = tmp
|
||||||
|
self._index_name = index_name
|
||||||
|
|
||||||
# Destructor
|
# Destructor
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@@ -37,19 +39,62 @@ class WPChange:
|
|||||||
|
|
||||||
|
|
||||||
## From directory
|
## From directory
|
||||||
|
|
||||||
def fromDirectory(self, directory="", number_thread=1, max_thread=1):
|
|
||||||
|
def fromDirectory(self, directory="", revert=False):
|
||||||
|
self._directory = directory
|
||||||
directory = "{0}/archives".format(directory)
|
directory = "{0}/archives".format(directory)
|
||||||
directories = self._getDirectories([], "{0}".format(directory))
|
directories = self._getDirectories([], "{0}".format(directory))
|
||||||
if len(directories) > 0:
|
if len(directories) > 0:
|
||||||
files = self._getFiles(directories)
|
files = self._getFiles(directories)
|
||||||
self.fromFile(files, number_thread, max_thread)
|
if revert is False:
|
||||||
|
self._tmpFiles(files=files, number_thread=self._index_name, max_thread=self._number_thread)
|
||||||
|
self._fromFileTmp()
|
||||||
else:
|
else:
|
||||||
self._logger.error("{0} : No files for {1}".format(self._name, directory))
|
self._logger.error("{0} : No files for {1}".format(self._name, directory))
|
||||||
|
|
||||||
|
|
||||||
|
def fromFile(self, files=[]):
|
||||||
|
for i in range(0, len(files)):
|
||||||
|
if os.path.exists(files[i]):
|
||||||
|
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i]))
|
||||||
|
self._change(files[i])
|
||||||
|
|
||||||
|
|
||||||
# Private method
|
# Private method
|
||||||
|
|
||||||
|
def _fromFileTmp(self):
|
||||||
|
try:
|
||||||
|
with open("{0}/{1}.json".format(self._tmp, self._name)) as file:
|
||||||
|
files = json.loads(file.read())
|
||||||
|
self._logger.debug("{0} : size of webpage : {1}".format(self._name, len(files)))
|
||||||
|
for i in range(0, len(files)):
|
||||||
|
if os.path.exists(files[i]):
|
||||||
|
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i]))
|
||||||
|
self._change(files[i])
|
||||||
|
except Exception as ex:
|
||||||
|
self._logger.error("{0} : Read file json from tmp : {1}".format(self._name, ex))
|
||||||
|
|
||||||
|
|
||||||
|
def _tmpFiles(self, files=[], number_thread=1, max_thread=1):
|
||||||
|
print()
|
||||||
|
divFiles = int(len(files) / int(max_thread))
|
||||||
|
currentRangeFiles = int(divFiles * (int(number_thread)+1))
|
||||||
|
firstRange = int(currentRangeFiles - divFiles)
|
||||||
|
self._logger.debug("{0} : index : {1}".format(self._name,number_thread))
|
||||||
|
|
||||||
|
self._logger.debug("{0} : first range : {1}".format(self._name,firstRange))
|
||||||
|
self._logger.debug("{0} : last range : {1}".format(self._name,currentRangeFiles))
|
||||||
|
webpage = []
|
||||||
|
for i in range(firstRange, currentRangeFiles):
|
||||||
|
webpage.append(files[i])
|
||||||
|
|
||||||
|
try:
|
||||||
|
string_webpage = json.dumps(webpage)
|
||||||
|
open("{0}/{1}.json".format(self._tmp, self._name), "wt").write(string_webpage)
|
||||||
|
except Exception as ex:
|
||||||
|
self._logger.error("{0} : Error for writing webpage : {1}".format(self._name, ex))
|
||||||
|
|
||||||
## Get all files
|
## Get all files
|
||||||
|
|
||||||
def _getFiles(self, item):
|
def _getFiles(self, item):
|
||||||
|
@@ -74,33 +74,23 @@ class WPimport:
|
|||||||
self._logger.error("{0} : Read file json from tmp : {1}".format(self._name, ex))
|
self._logger.error("{0} : Read file json from tmp : {1}".format(self._name, ex))
|
||||||
|
|
||||||
|
|
||||||
def fromDirectory(self, directory="", number_thread=1, max_thread=1):
|
def fromDirectory(self, directory="", number_thread=1, max_thread=1, revert=False):
|
||||||
self._directory = directory
|
self._directory = directory
|
||||||
directory = "{0}/archives".format(directory)
|
directory = "{0}/archives".format(directory)
|
||||||
directories = self._getDirectories([], "{0}".format(directory))
|
directories = self._getDirectories([], "{0}".format(directory))
|
||||||
if len(directories) > 0:
|
if len(directories) > 0:
|
||||||
files = self._getFiles(directories)
|
files = self._getFiles(directories)
|
||||||
self.fromFile(files=files, number_thread=number_thread, max_thread=max_thread)
|
if revert is False:
|
||||||
|
self._tmpFiles(files=files, number_thread=number_thread, max_thread=max_thread)
|
||||||
|
self._fromFileTmp()
|
||||||
else:
|
else:
|
||||||
self._logger.error("{0} : No files for {1}".format(self._name, directory))
|
self._logger.error("{0} : No files for {1}".format(self._name, directory))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def fromFile(self, files=[]):
|
||||||
|
for i in range(0, len(files)):
|
||||||
|
|
||||||
def fromFile(self, files=[], number_thread=1, max_thread=1):
|
|
||||||
divFiles = int(len(files) / max_thread)
|
|
||||||
currentRangeFiles = int(divFiles * (number_thread+1))
|
|
||||||
firstRange = int(currentRangeFiles - divFiles)
|
|
||||||
self._logger.debug("{0} : index : {1}".format(self._name,number_thread))
|
|
||||||
|
|
||||||
self._logger.debug("{0} : first range : {1}".format(self._name,firstRange))
|
|
||||||
self._logger.debug("{0} : last range : {1}".format(self._name,currentRangeFiles))
|
|
||||||
|
|
||||||
for i in range(firstRange, currentRangeFiles):
|
|
||||||
if os.path.exists(files[i]):
|
if os.path.exists(files[i]):
|
||||||
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, currentRangeFiles + 1, files[i]))
|
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i]))
|
||||||
with open(files[i], 'r') as f:
|
with open(files[i], 'r') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
self._logger.debug("{0} : Size of article : {1}".format(self._name, len(content)))
|
self._logger.debug("{0} : Size of article : {1}".format(self._name, len(content)))
|
||||||
@@ -111,10 +101,52 @@ class WPimport:
|
|||||||
self._addOrUpdatePost(soup)
|
self._addOrUpdatePost(soup)
|
||||||
else:
|
else:
|
||||||
self._addOrUpdateFeaturedMedia(soup)
|
self._addOrUpdateFeaturedMedia(soup)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Private method
|
# Private method
|
||||||
|
|
||||||
|
def _fromFileTmp(self):
|
||||||
|
try:
|
||||||
|
with open("{0}/{1}.json".format(self._tmp, self._name)) as file:
|
||||||
|
files = json.loads(file.read())
|
||||||
|
self._logger.debug("{0} : size of webpage : {1}".format(self._name, len(files)))
|
||||||
|
for i in range(0, len(files)):
|
||||||
|
if os.path.exists(files[i]):
|
||||||
|
self._logger.info("{0} : ({1}/{2}) File is being processed : {3}".format(self._name, i+1, len(files), files[i]))
|
||||||
|
with open(files[i], 'r') as f:
|
||||||
|
content = f.read()
|
||||||
|
self._logger.debug("{0} : Size of article : {1}".format(self._name, len(content)))
|
||||||
|
soup = BeautifulSoup(content, self._parser)
|
||||||
|
articlebody = soup.find_all("div", class_="articlebody")
|
||||||
|
self._logger.debug("{0} : Number of article : {1}".format(self._name, len(articlebody)))
|
||||||
|
if len(articlebody) > 0:
|
||||||
|
self._addOrUpdatePost(soup)
|
||||||
|
else:
|
||||||
|
self._addOrUpdateFeaturedMedia(soup)
|
||||||
|
except Exception as ex:
|
||||||
|
self._logger.error("{0} : Read file json from tmp : {1}".format(self._name, ex))
|
||||||
|
|
||||||
|
|
||||||
|
def _tmpFiles(self, files=[], number_thread=1, max_thread=1):
|
||||||
|
divFiles = int(len(files) / max_thread)
|
||||||
|
currentRangeFiles = int(divFiles * (number_thread+1))
|
||||||
|
firstRange = int(currentRangeFiles - divFiles)
|
||||||
|
self._logger.debug("{0} : index : {1}".format(self._name,number_thread))
|
||||||
|
|
||||||
|
self._logger.debug("{0} : first range : {1}".format(self._name,firstRange))
|
||||||
|
self._logger.debug("{0} : last range : {1}".format(self._name,currentRangeFiles))
|
||||||
|
webpage = []
|
||||||
|
for i in range(firstRange, currentRangeFiles):
|
||||||
|
webpage.append(files[i])
|
||||||
|
|
||||||
|
try:
|
||||||
|
string_webpage = json.dumps(webpage)
|
||||||
|
open("{0}/{1}.json".format(self._tmp, self._name), "wt").write(string_webpage)
|
||||||
|
except Exception as ex:
|
||||||
|
self._logger.error("{0} : Error for writing webpage : {1}".format(self._name, ex))
|
||||||
|
|
||||||
|
|
||||||
## replace caracter
|
## replace caracter
|
||||||
|
|
||||||
def _replaceCaracter(self, title_rendered):
|
def _replaceCaracter(self, title_rendered):
|
||||||
|
136
lib/WPMenu.py
Normal file
136
lib/WPMenu.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
import requests, os, logging, re, json
|
||||||
|
from requests.adapters import HTTPAdapter
|
||||||
|
from requests.packages.urllib3.util.retry import Retry
|
||||||
|
|
||||||
|
class WPMenu:
|
||||||
|
# Constructor
|
||||||
|
def __init__(self, name="Thread-0", basic=None, canalblog="", wordpress="", logger=None, parser="html.parser", ssl_canalblog=True, ssl_wordpress=True):
|
||||||
|
self._name = name
|
||||||
|
self._basic = basic
|
||||||
|
self._canalblog = canalblog
|
||||||
|
self._wordpress = wordpress
|
||||||
|
self._logger = logger
|
||||||
|
self._parser = parser
|
||||||
|
self._headers_json = {'Content-Type': 'application/json; charset=utf-8', 'Accept':'application/json'}
|
||||||
|
self._protocol_wordpress = "https"
|
||||||
|
self._protocol_canalblog = "https"
|
||||||
|
self._directory = "backup"
|
||||||
|
|
||||||
|
if ssl_wordpress is False:
|
||||||
|
self._protocol_wordpress = "http"
|
||||||
|
if ssl_canalblog is False:
|
||||||
|
self._protocol_canalblog = "http"
|
||||||
|
self._request_canalblog = requests.Session()
|
||||||
|
self._request_wordpress = requests.Session()
|
||||||
|
|
||||||
|
retries = Retry(connect=10, read=10, redirect=5,
|
||||||
|
status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2)
|
||||||
|
|
||||||
|
self._request_canalblog.mount('{0}://'.format(self._protocol_canalblog), HTTPAdapter(max_retries=retries))
|
||||||
|
self._request_wordpress.mount('{0}://'.format(self._protocol_wordpress), HTTPAdapter(max_retries=retries))
|
||||||
|
|
||||||
|
|
||||||
|
# Destructor
|
||||||
|
def __del__(self):
|
||||||
|
print("{0} : Import finished for {1}".format(self._name, self._wordpress))
|
||||||
|
|
||||||
|
# Public method
|
||||||
|
|
||||||
|
## From file
|
||||||
|
|
||||||
|
def fromFile(self, files):
|
||||||
|
if os.path.exists(files):
|
||||||
|
with open(files, 'r') as f:
|
||||||
|
self._logger.info("{0} : File is being processed : {1}".format(self._name, files))
|
||||||
|
content = f.read()
|
||||||
|
self._menu(content)
|
||||||
|
else:
|
||||||
|
self._logger.error("{0} : File isn't exist : {1}".format(self._name, files))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Get from URL
|
||||||
|
|
||||||
|
def fromUrl(self, canalblog):
|
||||||
|
self._canalblog = canalblog
|
||||||
|
try:
|
||||||
|
o = urlparse(canalblog)
|
||||||
|
o = o._replace(scheme=self._protocol_canalblog)
|
||||||
|
i = o.geturl().replace(":///", "://")
|
||||||
|
page = self._request_canalblog.get(i)
|
||||||
|
if page.status_code == 200:
|
||||||
|
self._logger.info("{0} : Page web is being processed : {1}".format(self._name, i))
|
||||||
|
self._menu(page.content)
|
||||||
|
else:
|
||||||
|
self._logger.error("{0} : index didn't get due status code : {1}".format(self._name, page.status_code))
|
||||||
|
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||||
|
except ConnectionError as err:
|
||||||
|
self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, canalblog, err))
|
||||||
|
exit(1)
|
||||||
|
except Exception as err:
|
||||||
|
self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, canalblog, err))
|
||||||
|
|
||||||
|
def _getId(self, title, parent):
|
||||||
|
menu = {"id":"", "type":"", "title": "", "parent":"", "children": []}
|
||||||
|
exist = False
|
||||||
|
for i in ["categories", "tags"]:
|
||||||
|
if exist is False:
|
||||||
|
try:
|
||||||
|
params = {"search":title, "per_page":"100"}
|
||||||
|
page = self._request_wordpress.get("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), auth=self._basic, params=params)
|
||||||
|
if page.status_code == 200:
|
||||||
|
result = page.json()
|
||||||
|
if len(result) > 0:
|
||||||
|
menu = {"id":result[0]["id"], "type":i, "title": title, "parent":parent, "children": []}
|
||||||
|
exist = True
|
||||||
|
else:
|
||||||
|
self._logger.error("{0} : {2} didn't get due status code : {1}".format(self._name, page.status_code, i))
|
||||||
|
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||||
|
except ConnectionError as err:
|
||||||
|
self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err))
|
||||||
|
exit(1)
|
||||||
|
except Exception as err:
|
||||||
|
self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol_wordpress), err))
|
||||||
|
return menu
|
||||||
|
|
||||||
|
def _menu(self, content):
|
||||||
|
|
||||||
|
soup = BeautifulSoup(content, self._parser)
|
||||||
|
ul = soup.find("ul", id="listsmooth")
|
||||||
|
menu = list()
|
||||||
|
children = list()
|
||||||
|
for anchor in ul.find_all("li"):
|
||||||
|
|
||||||
|
parent = anchor.find("a").get_text().replace(" \xa0", "")
|
||||||
|
href = anchor.find("a").get("href")
|
||||||
|
if href == "{0}://{1}/".format(self._protocol_canalblog, self._canalblog):
|
||||||
|
parent = "home"
|
||||||
|
itemMenu = {"id":"", "type":"", "title": parent, "children":list()}
|
||||||
|
|
||||||
|
|
||||||
|
#menu = self._child(parent, parent)
|
||||||
|
if href == "#":
|
||||||
|
li = anchor.find("ul").find_all("li")
|
||||||
|
for child in li:
|
||||||
|
a = child.find("a")
|
||||||
|
self._logger.info("{0} Parent {1} : Child {2}".format(self._name, parent, a.get_text()))
|
||||||
|
children.append({"title": a.get_text(), "parent": parent})
|
||||||
|
menu.append(itemMenu)
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(0, len(children)-1):
|
||||||
|
self._logger.info("{0} : Child {1}".format(self._name, children[i]))
|
||||||
|
for j in range(0, len(menu)-1):
|
||||||
|
if menu[j]["title"] == children[i]["title"]:
|
||||||
|
self._logger.info("{0} : Parent {1}".format(self._name, menu[j]))
|
||||||
|
del menu[j]
|
||||||
|
for j in range(0, len(menu)-1):
|
||||||
|
if menu[j]["title"] == children[i]["parent"]:
|
||||||
|
menu[j]["children"].append({"id":"", "title":children[i]["title"], "parent": children[i]["parent"]})
|
||||||
|
|
||||||
|
for i in menu:
|
||||||
|
self._logger.info("{0} : Menu {1} {2}".format(self._name, i["title"], len(i["children"])))
|
Reference in New Issue
Block a user