web_scrap/insert_wordpress.py

54 lines
1.9 KiB
Python

#!/usr/bin/python3
from requests.auth import HTTPBasicAuth
from getpass import getpass
import argparse, logging
import WPImport
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--user", help="wordpress user", required=True)
parser.add_argument("--file", help="HTML file", default="")
parser.add_argument("--directory", help="HTML directory", default="")
parser.add_argument("--wordpress", help="URL Wordpress", required=True)
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")
args = parser.parse_args()
logger = logging.getLogger('insert wordpress')
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)
password = getpass()
if len(password) == 0:
logger.error("No password error !!! ")
exit(1)
basic = HTTPBasicAuth(args.user, password)
importWp = WPimport(basic, args.wordpress, logger)
if len(args.file) > 0:
importWp.fromFile(args.file.split(","))
exit(0)
if len(args.directory) > 0:
importWp.fromDirectory(args.directory)