replace print by logger
This commit is contained in:
parent
9ab33c169e
commit
d58ead52b2
@ -7,9 +7,10 @@ import requests, os, argparse, logging, re, json
|
||||
|
||||
class WPimport:
|
||||
# Constructor
|
||||
def __init__(self, basic, wordpress):
|
||||
def __init__(self, basic, wordpress, logger):
|
||||
self._basic = basic
|
||||
self._wordpress = wordpress
|
||||
self._logger = logger
|
||||
|
||||
# Public method
|
||||
|
||||
@ -23,7 +24,7 @@ class WPimport:
|
||||
def fromFile(self, files):
|
||||
for file in files:
|
||||
if os.path.exists(file):
|
||||
print(file)
|
||||
logger.info("Fichier en cours de traitement : {0}".format(file))
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
@ -84,9 +85,9 @@ class WPimport:
|
||||
data = {"featured_media": id_media}
|
||||
r = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"]), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if r.status_code == 200:
|
||||
print("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
logger.info("Ajout media featured : {0}".format(r.json()["title"]["raw"]))
|
||||
else:
|
||||
print("Aucun media trouvé pour {0}".format(h2))
|
||||
logger.info("Aucun media trouvé pour {0}".format(h2))
|
||||
|
||||
## Association image to post
|
||||
|
||||
@ -95,7 +96,7 @@ class WPimport:
|
||||
data = {"post": post_id}
|
||||
r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"]), auth=self._basic, data=data)
|
||||
if r.status_code == 200:
|
||||
print("Association d'une image à l'article {0}".format(title))
|
||||
logger.info("Association d'une image à l'article {0}".format(title))
|
||||
|
||||
## Add or update img
|
||||
|
||||
@ -111,7 +112,7 @@ class WPimport:
|
||||
params = {"force":1}
|
||||
r = requests.delete("http://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"]), auth=self._basic, params=params)
|
||||
if r.status_code == 200:
|
||||
print("Image supprimé {0}".format(img_name))
|
||||
logger.info("Image supprimé {0}".format(img_name))
|
||||
data = page.content
|
||||
img_type = "image/png"
|
||||
if img_name.split(".")[1] == "jpg" or img_name.split(".")[1] == "jpeg":
|
||||
@ -119,7 +120,7 @@ class WPimport:
|
||||
headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)}
|
||||
r = requests.post("http://{0}/wp-json/wp/v2/media".format(self._wordpress), auth=self._basic, headers=headers, data=data)
|
||||
if r.status_code == 201:
|
||||
print("Ajout d'image {0}".format(img_name))
|
||||
logger.info("Ajout d'image {0}".format(img_name))
|
||||
res = r.json()
|
||||
media["id"] = res["id"]
|
||||
media["rendered"] = res["guid"]["rendered"]
|
||||
@ -241,12 +242,12 @@ class WPimport:
|
||||
if len(result) == 0:
|
||||
page_exist = False
|
||||
else:
|
||||
print("La page {0} existe deja et mis à jour".format(title))
|
||||
logger.info("La page {0} existe deja et mis à jour".format(title))
|
||||
post_id = result[0]["id"]
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
print("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
logger.info("Article mis à jour : {0}".format(result["title"]["raw"]))
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
||||
|
||||
@ -255,12 +256,12 @@ class WPimport:
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, headers=headers, data=json.dumps(data))
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
print("Article ajoute : {0}".format(result["title"]["raw"]))
|
||||
logger.info("Article ajoute : {0}".format(result["title"]["raw"]))
|
||||
for i in comment_post:
|
||||
data = {"post": result["id"], "content": i["content"], "date": i["date"], "author_name": i["author"]}
|
||||
page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self._wordpress), auth=self._basic, data=data)
|
||||
if page.status_code == 201:
|
||||
print("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
|
||||
logger.info("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
|
||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||
|
||||
|
||||
@ -274,14 +275,42 @@ if __name__ == '__main__':
|
||||
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:
|
||||
print("No password error !!! ")
|
||||
logger.error("No password error !!! ")
|
||||
exit(1)
|
||||
|
||||
basic = HTTPBasicAuth(args.user, password)
|
||||
importWp = WPimport(basic, args.wordpress)
|
||||
importWp = WPimport(basic, args.wordpress, logger)
|
||||
if len(args.file) > 0:
|
||||
importWp.fromFile(args.file.split(","))
|
||||
exit(0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user