web_scrap/import_export_canalblog.py

175 lines
6.8 KiB
Python
Raw Normal View History

2023-03-23 23:28:57 +01:00
#!/usr/bin/python3
from requests.auth import HTTPBasicAuth
from getpass import getpass
2023-04-09 23:49:10 +02:00
from urllib.parse import urlparse
2023-04-22 00:07:54 +02:00
import argparse, logging, threading
2023-04-24 23:15:29 +02:00
import multiprocessing
2023-04-11 22:15:36 +02:00
from lib.WPImport import WPimport
from lib.WPExport import WPExport
2023-03-28 22:29:55 +02:00
2023-04-24 23:15:29 +02:00
def download(name_thread, max_thread, exportWp, html, img):
exportWp.setName(name_thread)
webpage = exportWp.getUrlPage(name_thread, max_thread)
2023-04-22 00:07:54 +02:00
if html is False:
exportWp.downloadHTML(webpage)
if args.img is False:
exportWp.downloadImg(webpage)
2023-03-28 22:29:55 +02:00
if __name__ == '__main__':
parser = argparse.ArgumentParser()
2023-04-08 12:17:43 +02:00
parser.add_argument("--debug", help="Verbosity", action="store_true")
parser.add_argument("--logfile", help="Log file", default="")
parser.add_argument("--quiet", help="No console output", action="store_true")
2023-04-09 21:17:49 +02:00
parser.add_argument("--parser", help="Parser content", default="html.parser")
2023-04-24 23:15:29 +02:00
parser.add_argument("--parallel", help="Define number thread (default : 1)", default=1)
2023-04-08 12:17:43 +02:00
2023-04-08 23:43:06 +02:00
subparsers = parser.add_subparsers(dest="command")
2023-04-08 23:20:52 +02:00
import_parser = subparsers.add_parser("import")
import_parser.add_argument("--user", help="wordpress user", required=True)
import_parser.add_argument("--file", help="HTML file", default="")
import_parser.add_argument("--directory", help="HTML directory", default="")
2023-04-09 22:49:44 +02:00
import_parser.add_argument("--canalblog", help="URL Canalblog", default="")
2023-04-08 23:20:52 +02:00
import_parser.add_argument("--wordpress", help="URL Wordpress", required=True)
2023-04-10 16:36:49 +02:00
import_parser.add_argument("--serial", help="Serial execution", action="store_true")
2023-04-08 23:34:56 +02:00
export_parser = subparsers.add_parser("export")
export_parser.add_argument("--url", help="canblog URL to be scraping", required=True)
export_parser.add_argument("--directory",
default="backup",
help="backup file path")
export_parser.add_argument("--no-css", help="No CSS", dest="css", action="store_true")
export_parser.add_argument("--no-js", help="No JS", dest="js", action="store_true")
export_parser.add_argument("--no-img", help="No img", dest="img", action="store_true")
export_parser.add_argument("--no-html", help="No HTML", dest="html", action="store_true")
2023-04-08 23:20:52 +02:00
2023-03-28 22:29:55 +02:00
args = parser.parse_args()
2023-04-08 23:43:06 +02:00
2023-04-10 00:00:01 +02:00
logger = logging.getLogger('import export canalblog')
2023-04-08 12:17:43 +02:00
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
if args.quiet is False:
ch = logging.StreamHandler()
if args.debug is True:
logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
if len(args.logfile) > 0:
fileHandler = logging.FileHandler(args.logfile)
if args.debug is True:
fileHandler.setLevel(logging.DEBUG)
else:
fileHandler.setLevel(logging.INFO)
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
2023-04-09 22:49:44 +02:00
if args.command == "import":
2023-04-08 23:43:06 +02:00
password = getpass()
if len(password) == 0:
logger.error("No password error !!! ")
exit(1)
2023-03-28 22:29:55 +02:00
2023-04-08 23:43:06 +02:00
basic = HTTPBasicAuth(args.user, password)
2023-04-10 16:15:13 +02:00
wordpress = args.wordpress.split(",")
2023-04-11 22:15:36 +02:00
importWp = WPimport(basic, "", logger, args.parser)
2023-04-08 23:43:06 +02:00
if len(args.file) > 0:
2023-04-10 16:15:13 +02:00
for i in wordpress:
importWp.setUrl(i)
importWp.fromFile(args.file.split(","))
2023-04-08 23:43:06 +02:00
exit(0)
if len(args.directory) > 0:
2023-04-10 16:36:49 +02:00
directory = args.directory.split(",")
if args.serial is False:
for i in wordpress:
importWp.setUrl(i)
for j in directory:
importWp.fromDirectory(j)
else:
if len(directory) != len(wordpress):
logger.error("ERREUR : Le nombre de dossier n'est pas equivalent au nombre d'URL wordpress")
exit(1)
for i in range(0, len(wordpress)-1):
importWp.setUrl(wordpress[i])
importWp.fromDirectory(directory[i])
2023-04-10 00:00:01 +02:00
exit(0)
if len(args.canalblog) > 0:
2023-04-11 22:15:36 +02:00
exportWp = WPExport("", logger, args.parser, args.directory)
2023-04-10 16:02:40 +02:00
canalblog = args.canalblog.split(",")
2023-04-10 16:36:49 +02:00
wordpress = args.wordpress.split(",")
2023-04-11 22:15:36 +02:00
if args.serial is False:
2023-04-10 16:36:49 +02:00
for canal in canalblog:
try:
o = urlparse(canal)
o = o._replace(scheme="https")
url = o.geturl().replace(":///", "://")
except Exception as err:
logger.error("parsing error : {0}".format(err))
exit(1)
exportWp.setUrl(url)
webpage = exportWp.getUrlPage()
for j in wordpress:
importWp.setUrl(j)
importWp.fromUrl(webpage)
else:
if len(canalblog) != len(wordpress):
logger.error("ERREUR : Le nombre de dossier n'est pas equivalent au nombre d'URL wordpress")
2023-04-10 16:02:40 +02:00
exit(1)
2023-04-10 16:36:49 +02:00
for i in range(0, len(canalblog)-1):
try:
o = urlparse(canalblog[i])
o = o._replace(scheme="https")
url = o.geturl().replace(":///", "://")
except Exception as err:
logger.error("parsing error : {0}".format(err))
exit(1)
exportWp.setUrl(url)
webpage = exportWp.getUrlPage()
importWp.setUrl(wordpress[i])
importWp.fromUrl(webpage)
2023-04-10 16:02:40 +02:00
if args.command == "export":
canalblog = args.url.split(",")
2023-04-11 22:15:36 +02:00
exportWp = WPExport("", logger, args.parser, args.directory)
2023-04-10 16:02:40 +02:00
for canal in canalblog:
2023-04-10 00:00:01 +02:00
try:
2023-04-10 16:02:40 +02:00
o = urlparse(canal)
2023-04-10 00:00:01 +02:00
o = o._replace(scheme="https")
url = o.geturl().replace(":///", "://")
except Exception as err:
logger.error("parsing error : {0}".format(err))
exit(1)
2023-04-10 16:02:40 +02:00
exportWp.setUrl(url)
if args.js is False:
exportWp.downloadJs()
2023-04-09 22:49:44 +02:00
2023-04-10 16:02:40 +02:00
if args.css is False:
exportWp.downloadCss()
2023-04-09 22:49:44 +02:00
2023-04-10 16:02:40 +02:00
if args.html is False or args.img is False:
2023-04-24 23:15:29 +02:00
threads = []
for i in range(0, int(args.parallel)):
t1 = multiprocessing.Process(name="Process-{0}".format(i + 1), target=download, args=(i, 3, exportWp,args.html, args.img))
threads.append(t1)
for thread in threads:
thread.start()
thread.join()
2023-04-09 22:50:41 +02:00
exit(0)