add parameter
This commit is contained in:
parent
4bd6f5c038
commit
2f1c081823
@ -47,7 +47,7 @@ def download(name_thread, max_thread, url, logger, parser, directory, html, img,
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, basic, serial, ssl_wordpress, ssl_canalblog):
|
def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, basic, serial, ssl_wordpress, ssl_canalblog, create, update):
|
||||||
canalblog = canalblog.split(",")
|
canalblog = canalblog.split(",")
|
||||||
wordpress = wordpress.split(",")
|
wordpress = wordpress.split(",")
|
||||||
name = "Thread-{0}".format(int(name_thread) + 1)
|
name = "Thread-{0}".format(int(name_thread) + 1)
|
||||||
@ -67,7 +67,7 @@ def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, bas
|
|||||||
webpage = exportWp.getUrlPage(name_thread, max_thread)
|
webpage = exportWp.getUrlPage(name_thread, max_thread)
|
||||||
del exportWp
|
del exportWp
|
||||||
for j in wordpress:
|
for j in wordpress:
|
||||||
importWp = WPimport(name=name, basic=basic, wordpress=j, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress)
|
importWp = WPimport(name=name, basic=basic, wordpress=j, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update)
|
||||||
for k in ["article", "page"]:
|
for k in ["article", "page"]:
|
||||||
for l in ["publications", "principal"]:
|
for l in ["publications", "principal"]:
|
||||||
importWp.fromUrl(webpage[l][k])
|
importWp.fromUrl(webpage[l][k])
|
||||||
@ -88,7 +88,7 @@ def importUrl(name_thread, max_thread, canalblog, logger, parser, wordpress, bas
|
|||||||
exportWp = WPExport(name=name, url=url, logger=logger, parser=parser, ssl_canalblog=ssl_canalblog)
|
exportWp = WPExport(name=name, url=url, logger=logger, parser=parser, ssl_canalblog=ssl_canalblog)
|
||||||
webpage = exportWp.getUrlPage(name_thread, max_thread)
|
webpage = exportWp.getUrlPage(name_thread, max_thread)
|
||||||
del exportWp
|
del exportWp
|
||||||
importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress)
|
importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update)
|
||||||
|
|
||||||
for k in ["article", "page"]:
|
for k in ["article", "page"]:
|
||||||
for l in ["publications", "principal"]:
|
for l in ["publications", "principal"]:
|
||||||
@ -97,13 +97,13 @@ 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):
|
def importDirectory(name_thread, max_thread, directory, logger, parser, wordpress, basic, serial, ssl_wordpress, create, update):
|
||||||
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(",")
|
||||||
if serial is False:
|
if serial is False:
|
||||||
for i in wordpress:
|
for i in wordpress:
|
||||||
importWp = WPimport(name=name, basic=basic, wordpress=i, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress)
|
importWp = WPimport(name=name, basic=basic, wordpress=i, logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update)
|
||||||
for j in directory:
|
for j in directory:
|
||||||
importWp.fromDirectory(j, name_thread, max_thread)
|
importWp.fromDirectory(j, name_thread, max_thread)
|
||||||
del importWp
|
del importWp
|
||||||
@ -113,7 +113,7 @@ def importDirectory(name_thread, max_thread, directory, logger, parser, wordpres
|
|||||||
logger.error("{0} : Error : Number directory is differant than wordpress".format(name))
|
logger.error("{0} : Error : Number directory is differant than wordpress".format(name))
|
||||||
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)
|
importWp = WPimport(name=name, basic=basic, wordpress=wordpress[i], logger=logger, parser=parser, ssl_wordpress=ssl_wordpress, no_create=create, no_update=update)
|
||||||
importWp.fromDirectory(directory[i])
|
importWp.fromDirectory(directory[i])
|
||||||
del importWp
|
del importWp
|
||||||
|
|
||||||
@ -143,6 +143,8 @@ if __name__ == '__main__':
|
|||||||
import_parser.add_argument("--remove-categories", help="Remove all categories", dest="categories", action="store_true")
|
import_parser.add_argument("--remove-categories", help="Remove all categories", dest="categories", action="store_true")
|
||||||
import_parser.add_argument("--remove-tags", help="Remove all tags", dest="tags", action="store_true")
|
import_parser.add_argument("--remove-tags", help="Remove all tags", dest="tags", action="store_true")
|
||||||
import_parser.add_argument("--remove-media", help="Remove all media", dest="media", action="store_true")
|
import_parser.add_argument("--remove-media", help="Remove all media", dest="media", action="store_true")
|
||||||
|
import_parser.add_argument("--no-create", help="No create post", dest="create", action="store_true")
|
||||||
|
import_parser.add_argument("--no-update", help="No update post", dest="update", action="store_true")
|
||||||
|
|
||||||
|
|
||||||
remove_parser = subparsers.add_parser("remove")
|
remove_parser = subparsers.add_parser("remove")
|
||||||
@ -226,7 +228,7 @@ if __name__ == '__main__':
|
|||||||
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)
|
||||||
wait_for = [
|
wait_for = [
|
||||||
ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress)
|
ex.submit(importDirectory, i, int(args.parallel), args.directory, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, create, update)
|
||||||
for i in range(0, int(args.parallel))
|
for i in range(0, int(args.parallel))
|
||||||
]
|
]
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
@ -237,7 +239,7 @@ if __name__ == '__main__':
|
|||||||
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)
|
||||||
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)
|
ex.submit(importUrl, i, int(args.parallel), args.canalblog, logger, args.parser, args.wordpress, basic, args.serial, ssl_wordpress, ssl_canalblog, create, update)
|
||||||
for i in range(0, int(args.parallel))
|
for i in range(0, int(args.parallel))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ from requests.packages.urllib3.util.retry import Retry
|
|||||||
|
|
||||||
class WPimport:
|
class WPimport:
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self, name="Thread-0", basic=None, wordpress="", logger=None, parser="html.parser", ssl_wordpress=True):
|
def __init__(self, name="Thread-0", basic=None, wordpress="", logger=None, parser="html.parser", ssl_wordpress=True, no_create=False, no_update=False):
|
||||||
self._name = name
|
self._name = name
|
||||||
self._basic = basic
|
self._basic = basic
|
||||||
self._wordpress = wordpress
|
self._wordpress = wordpress
|
||||||
@ -24,7 +24,8 @@ class WPimport:
|
|||||||
status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2)
|
status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2)
|
||||||
|
|
||||||
self._request.mount('{0}://'.format(self._protocol), HTTPAdapter(max_retries=retries))
|
self._request.mount('{0}://'.format(self._protocol), HTTPAdapter(max_retries=retries))
|
||||||
|
self._no_create = no_create
|
||||||
|
self._no_update = no_update
|
||||||
|
|
||||||
# Destructor
|
# Destructor
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@ -615,45 +616,46 @@ class WPimport:
|
|||||||
self._logger.debug("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title))
|
self._logger.debug("{0} : Search title posts for |{2}| : |{1}|".format(self._name, title_rendered, title))
|
||||||
self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(title), len(title_rendered)))
|
self._logger.debug("{0} : SIze of title : {1} - {2}".format(self._name, len(title), len(title_rendered)))
|
||||||
if title_rendered == title:
|
if title_rendered == title:
|
||||||
page_is_exist = True
|
if self._no_update is False:
|
||||||
post_id = i["id"]
|
page_is_exist = True
|
||||||
count = count + 1
|
post_id = i["id"]
|
||||||
if count > 1:
|
count = count + 1
|
||||||
self._logger.info("{0} : Page {1} is double and going to delete".format(self._name, title))
|
if count > 1:
|
||||||
try:
|
self._logger.info("{0} : Page {1} is double and going to delete".format(self._name, title))
|
||||||
params = {"force":1}
|
try:
|
||||||
page = self._request.delete("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, params=params)
|
params = {"force":1}
|
||||||
if page.status_code == 200:
|
page = self._request.delete("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, params=params)
|
||||||
self._logger.info("{0} : Post deleted : {1}".format(self._name, title))
|
if page.status_code == 200:
|
||||||
else:
|
self._logger.info("{0} : Post deleted : {1}".format(self._name, title))
|
||||||
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code))
|
else:
|
||||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code))
|
||||||
except ConnectionError as err:
|
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||||
self._logger.error("{0} : Connection error for deleted post : {1}".format(self._name, err))
|
except ConnectionError as err:
|
||||||
exit(1)
|
self._logger.error("{0} : Connection error for deleted post : {1}".format(self._name, err))
|
||||||
except Exception as err:
|
exit(1)
|
||||||
self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err))
|
except Exception as err:
|
||||||
|
self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i))
|
self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i))
|
||||||
self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title))
|
self._logger.info("{0} : Page {1} already exist and going to update".format(self._name, title))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
page = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
page = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, post_id, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
||||||
|
|
||||||
if page.status_code == 200:
|
if page.status_code == 200:
|
||||||
result = page.json()
|
result = page.json()
|
||||||
self._logger.info("{0} : Post updated : {1}".format(self._name, title))
|
self._logger.info("{0} : Post updated : {1}".format(self._name, title))
|
||||||
self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"])
|
self._addOrUpdateComment(result["id"], comment_post, result["title"]["raw"])
|
||||||
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
|
||||||
else:
|
else:
|
||||||
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code))
|
self._logger.error("{0} : Post not updated due status code : {1}".format(self._name, page.status_code))
|
||||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||||
except ConnectionError as err:
|
except ConnectionError as err:
|
||||||
self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err))
|
self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err))
|
||||||
exit(1)
|
exit(1)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err))
|
self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err))
|
||||||
if page.status_code == 400:
|
if page.status_code == 400:
|
||||||
self._logger.error("{0} : Connection for update post unauthorized : {1}".format(self._name, page.status_code))
|
self._logger.error("{0} : Connection for update post unauthorized : {1}".format(self._name, page.status_code))
|
||||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||||
@ -667,7 +669,7 @@ class WPimport:
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
self._logger.error("{0} : Exception error for search post : {1}".format(self._name, err))
|
self._logger.error("{0} : Exception error for search post : {1}".format(self._name, err))
|
||||||
|
|
||||||
if page_is_exist is False:
|
if page_is_exist is False and self._no_create is False:
|
||||||
try:
|
try:
|
||||||
self._logger.info("{0} : Creating posts : {1}".format(self._name, data["title"]))
|
self._logger.info("{0} : Creating posts : {1}".format(self._name, data["title"]))
|
||||||
page = self._request.post("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
page = self._request.post("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user