Merge pull request 'replace-exception' (#15) from replace-exception into master
Reviewed-on: #15
This commit is contained in:
commit
f507efce60
@ -72,9 +72,7 @@ class WPExport:
|
||||
def getUrlPage(self, index_thread, max_thread):
|
||||
try:
|
||||
page = self._request.get(self._url)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
page_url = []
|
||||
if page.status_code == 200:
|
||||
soup = BeautifulSoup(page.text, self._parser)
|
||||
@ -86,6 +84,11 @@ class WPExport:
|
||||
else:
|
||||
self._logger.error("{0} : URL did not 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 : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error : {1}".format(self._name, err))
|
||||
|
||||
webpage = {"principal": {"page":[], "article":[]}, "publications": {"page":[], "article":[]}}
|
||||
for i in page_url:
|
||||
@ -97,9 +100,7 @@ class WPExport:
|
||||
section = "principal"
|
||||
try:
|
||||
page = self._request.get(i)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
self._logger.info("{0} : page : {1}".format(self._name, i))
|
||||
if i not in webpage[section]["page"]:
|
||||
@ -147,13 +148,19 @@ class WPExport:
|
||||
try:
|
||||
o = urlparse(href)
|
||||
o = o._replace(scheme="https").geturl()
|
||||
webpage[section]["article"].append(o)
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
webpage[section]["article"].append(o)
|
||||
else:
|
||||
self._logger.error("{0} : web 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 : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
return webpage
|
||||
|
||||
|
||||
@ -183,9 +190,7 @@ class WPExport:
|
||||
def _getScriptCss(self, js, css):
|
||||
try:
|
||||
page = self._request.get(self._url)
|
||||
except Exception as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
|
||||
page_url = []
|
||||
if page.status_code == 200:
|
||||
soup = BeautifulSoup(page.text, self._parser)
|
||||
@ -197,13 +202,14 @@ class WPExport:
|
||||
try:
|
||||
u = urlparse(self._url)
|
||||
o = urlparse(src)
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
if o.netloc == "":
|
||||
o = o._replace(netloc=u.netloc)
|
||||
o = o._replace(scheme=u.scheme)
|
||||
page_url.append(o.geturl())
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
|
||||
if css is True:
|
||||
link = soup.find_all("link")
|
||||
for anchor in link:
|
||||
@ -214,16 +220,22 @@ class WPExport:
|
||||
try:
|
||||
u = urlparse(self._url)
|
||||
o = urlparse(href)
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
if o.netloc == "":
|
||||
o = o._replace(netloc=u.netloc)
|
||||
o = o._replace(scheme=u.scheme)
|
||||
page_url.append(o.geturl())
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
|
||||
else:
|
||||
self._logger.error("JS or CSS did not get due status code : {0}".format(page.status_code))
|
||||
self._logger.debug(page.content)
|
||||
except ConnectionError as err:
|
||||
self._logger.error("Connection error : {0}".format(err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("Exception error : {0}".format(err))
|
||||
|
||||
return page_url
|
||||
|
||||
@ -234,9 +246,7 @@ class WPExport:
|
||||
for i in webpage:
|
||||
try:
|
||||
page = self._request.get(i)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
soup = BeautifulSoup(page.text, self._parser)
|
||||
img = soup.find_all("img")
|
||||
@ -250,6 +260,11 @@ class WPExport:
|
||||
else:
|
||||
self._logger.error("{0} : Image did not 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 : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error : {1}".format(self._name, err))
|
||||
|
||||
return page_img
|
||||
|
||||
@ -260,9 +275,7 @@ class WPExport:
|
||||
for i in range(0, len(webpage)):
|
||||
try:
|
||||
o = urlparse(webpage[i])
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
|
||||
path_web = o.path.split("/")
|
||||
filePageWeb = path_web[len(path_web)-1]
|
||||
path_web.pop(len(path_web)-1)
|
||||
@ -270,11 +283,7 @@ class WPExport:
|
||||
self._mkdirPath("{0}/{1}/{2}".format(backup_dir, o.netloc, dir_page_web))
|
||||
try:
|
||||
r = self._request.get(webpage[i])
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} Exception error : {1}".format(self._name, err))
|
||||
|
||||
if r.status_code == 200:
|
||||
fileDownload = "{0}/{1}/index.html".format(backup_dir, o.netloc)
|
||||
if len(dir_page_web) > 0 and len(filePageWeb) > 0:
|
||||
@ -288,3 +297,11 @@ class WPExport:
|
||||
else:
|
||||
self._logger.error("Not download due status code : {0}".format(r.status_code))
|
||||
self._logger.debug(r.content)
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} Exception error : {1}".format(self._name, err))
|
||||
except Exception as err:
|
||||
self._logger.error("parsing error : {0}".format(err))
|
||||
exit(1)
|
||||
|
210
lib/WPImport.py
210
lib/WPImport.py
@ -39,9 +39,6 @@ class WPimport:
|
||||
for i in range(0, len(webpage)):
|
||||
try:
|
||||
r = self._request.get(webpage[i])
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, webpage[i], err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("{0} : ({1}/{2}) : Page is importing : {3}".format(self._name, i+1, len(webpage), webpage[i]))
|
||||
soup = BeautifulSoup(r.content, self._parser)
|
||||
@ -53,6 +50,11 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Connection error for get url {1} with status code : {2}".format(self._name, webpage[i], r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, webpage[i], err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, webpage[i], err))
|
||||
|
||||
|
||||
def fromDirectory(self, directory="", number_thread=1, max_thread=1):
|
||||
@ -137,9 +139,6 @@ class WPimport:
|
||||
params = {"search":h2, "type":"post"}
|
||||
try:
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/search".format(self._wordpress, self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
if len(result) > 0:
|
||||
@ -149,18 +148,13 @@ class WPimport:
|
||||
img_src = img[0].get("src")
|
||||
try:
|
||||
page = self._request.get(img_src)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for get featured media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
if page.status_code == 200:
|
||||
name_img = img_src.replace("_q", "")
|
||||
name_img = name_img.split("/")[len(name_img.split("/"))-1]
|
||||
params = {"search": name_img}
|
||||
try:
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error search featured media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
res = page.json()
|
||||
if len(res) > 0:
|
||||
@ -168,29 +162,42 @@ class WPimport:
|
||||
data = {"featured_media": id_media}
|
||||
try:
|
||||
r = self._request.post("{2}://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, result[0]["id"], self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for post media featured : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("{0} : Add media featured : {1}".format(self._name, r.json()["title"]["raw"]))
|
||||
else:
|
||||
self._logger.error("{0} : Connection error with status code for featured media : {1}".format(self._name, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for post media featured : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for post media featured : {1}".format(self._name, err))
|
||||
else:
|
||||
self._logger.info("{0} : No media found for {1}".format(self._name, h2))
|
||||
else:
|
||||
self._logger.error("{0} : Connection error with status code for search featured media: {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 search featured media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error search featured media : {1}".format(self._name, err))
|
||||
else:
|
||||
self._logger.error("{0} : Connection error for get featured media with 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 featured media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get featured media : {1}".format(self._name, err))
|
||||
else:
|
||||
self._logger.error("{0} : Connection error with status code for featured media : {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 : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error : {1}".format(self._name, err))
|
||||
|
||||
## Association image to post
|
||||
|
||||
@ -199,14 +206,16 @@ class WPimport:
|
||||
data = {"post": post_id}
|
||||
try:
|
||||
r = self._request.post("{2}://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, i["id"], self._protocol), auth=self._basic, data=data)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for link image to post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("{0} : Link image to post {1}".format(self._name, title))
|
||||
else:
|
||||
self._logger.error("{0} Connection error with status code for link image to post : {1}".format(self._name, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for link image to post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for link image to post : {1}".format(self._name, err))
|
||||
|
||||
|
||||
## Add or update img
|
||||
@ -226,9 +235,7 @@ class WPimport:
|
||||
params = { "search": img_name}
|
||||
try:
|
||||
r = self._request.get("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for search media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
self._logger.debug("{0} : Search for image {1} and his status code {2}".format(self._name, img_name, r.status_code))
|
||||
if r.status_code == 200:
|
||||
res = r.json()
|
||||
@ -237,14 +244,16 @@ class WPimport:
|
||||
params = {"force":1}
|
||||
try:
|
||||
r = self._request.delete("{2}://{0}/wp-json/wp/v2/media/{1}".format(self._wordpress, res[0]["id"], self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} Connection error for delete image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
if r.status_code == 200:
|
||||
self._logger.info("{0} : Image removed {1}".format(self._name, img_name))
|
||||
else:
|
||||
self._logger.error("{0} : Image {1} not removed due status code : {2}".format(self._name, img_name, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} Connection error for delete image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} Exception error for delete image : {1}".format(self._name, err))
|
||||
|
||||
data = page.content
|
||||
img_type = "image/{0}".format(img_type_file)
|
||||
@ -253,9 +262,7 @@ class WPimport:
|
||||
headers={ 'Content-Type': img_type,'Content-Disposition' : 'attachment; filename={0}'.format(img_name)}
|
||||
try:
|
||||
r = self._request.post("{1}://{0}/wp-json/wp/v2/media".format(self._wordpress, self._protocol), auth=self._basic, headers=headers, data=data)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for add image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if r.status_code == 201:
|
||||
self._logger.info("{0} : Image added {1}".format(self._name, img_name))
|
||||
res = r.json()
|
||||
@ -264,11 +271,21 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Image {1}.{2} not added due status code : {3}".format(self._name, img_name, img_type, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for add image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for add image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
else:
|
||||
self._logger.error("{0} : Connection error for search image with status code : {1}".format(self._name, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for search media : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for search media : {1}".format(self._name, err))
|
||||
return media
|
||||
|
||||
## Add or update comment
|
||||
@ -279,28 +296,34 @@ class WPimport:
|
||||
try:
|
||||
params = {"post": post, "author_name":i["author"], "date":i["date"]}
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for search comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
for j in result:
|
||||
try:
|
||||
params = {"force":1}
|
||||
page = self._request.delete("{2}://{0}/wp-json/wp/v2/comments/{1}".format(self._wordpress, j["id"], self._protocol), params=params, auth=self._basic)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for delete comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
self._logger.info("{0} : Comment deleted for {1}".format(self._name, title))
|
||||
self._logger.debug("{0} : Comment deleted : {1}".format(self._name, j))
|
||||
else:
|
||||
self._logger.error("{0} : Comment not deleted for {1} due status code : {2}".format(self._name, title, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for delete comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for delete comment : {1}".format(self._name, err))
|
||||
|
||||
else:
|
||||
self._logger.error("{0} : Comment not listed for {1} due status code : {2}".format(self._name, title, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for search comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for search comment : {1}".format(self._name, err))
|
||||
|
||||
for i in comment:
|
||||
data = {"post": post, "content": i["content"], "date": i["date"], "author_name": i["author"], "status": "approved"}
|
||||
@ -310,9 +333,7 @@ class WPimport:
|
||||
params = {"post": post, "author_name":comment[parent_id]["author"], "date":comment[parent_id]["date"]}
|
||||
try:
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, params=params)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for parent comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
if len(result) > 0:
|
||||
@ -320,18 +341,27 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Connection error for parent comment with 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 parent comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for parent comment : {1}".format(self._name, err))
|
||||
|
||||
try:
|
||||
page = self._request.post("{1}://{0}/wp-json/wp/v2/comments".format(self._wordpress, self._protocol), auth=self._basic, data=data)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for add comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page.status_code == 201:
|
||||
self._logger.info("{0} : Comment added for {1}".format(self._name, title))
|
||||
self._logger.debug("{0} : Data : {1}".format(self._name, data))
|
||||
else:
|
||||
self._logger.error("{0} : Comment not added for {1} due status code : {2}".format(self._name, title, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for add comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for add comment : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
## Check class name
|
||||
|
||||
@ -416,16 +446,17 @@ class WPimport:
|
||||
new_img["old_href"]=href_a
|
||||
try:
|
||||
page_img = self._request.get(href_img)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
if page_img.status_code == 404:
|
||||
href_img = href_a
|
||||
try:
|
||||
page_img = self._request.get(href_a)
|
||||
except Exception as err:
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
self._logger.debug("{0} : Status code for image {1} : {2}".format(self._name, href_img, page_img.status_code))
|
||||
if page_img.status_code == 200:
|
||||
media=self._addOrUpdateMedia(href_img, page_img)
|
||||
@ -440,6 +471,12 @@ class WPimport:
|
||||
if page_img.status_code not in [200, 404]:
|
||||
self._logger.error("{0} : Connection error with status code for get image : {1}".format(self._name, page_img.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page_img.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for get image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get image : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
|
||||
self._logger.debug("{0} : Number of image : {1}".format(self._name, len(list_img)))
|
||||
comment_post = self._getComment(comment)
|
||||
@ -462,11 +499,7 @@ class WPimport:
|
||||
try:
|
||||
params = {"search":title_element, "per_page":"100", "page":index}
|
||||
page = self._request.get("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol), auth=self._basic, params=params)
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for {1} : {2}".format(self._name, i, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for {1} : {2}".format(self._name, i, err))
|
||||
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
self._logger.debug("{0} : content {3} {2} : {1}".format(self._name, result, title_element, i))
|
||||
@ -491,7 +524,11 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : {1} not found due status code : {2}".format(self._name, i, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for {1} : {2}".format(self._name, i, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for {1} : {2}".format(self._name, i, err))
|
||||
self._logger.debug("{0} : Element {3} {2} is {1}".format(self._name, element_exist, title_element, i))
|
||||
if element_exist == False:
|
||||
data = {"name": title_element}
|
||||
@ -499,11 +536,7 @@ class WPimport:
|
||||
self._logger.debug("{0} : Data : {1}".format(self._name, data))
|
||||
try:
|
||||
page = self._request.post("{2}://{0}/wp-json/wp/v2/{1}".format(self._wordpress, i, self._protocol), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for post {1} : {2}".format(self._name, i, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for post {1} : {2}".format(self._name, i, err))
|
||||
|
||||
if page.status_code == 201:
|
||||
self._logger.info("{0} : {1} created : {2}".format(self._name, i, j))
|
||||
result = page.json()
|
||||
@ -511,6 +544,11 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : {1} not added due status code : {2}".format(self._name, i, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, page.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for post {1} : {2}".format(self._name, i, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for post {1} : {2}".format(self._name, i, err))
|
||||
|
||||
title = articletitle[0].text
|
||||
author = articleacreator[0].text.lower()
|
||||
@ -541,13 +579,6 @@ class WPimport:
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/users".format(self._wordpress, self._protocol), auth=self._basic, headers=self._headers_json, params=params)
|
||||
self._logger.debug("{0} : End Search author : {1}".format(self._name, author))
|
||||
self._logger.debug("{0} : Debug requests : {1}".format(self._name, page.content))
|
||||
|
||||
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for get author : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get author : {1}".format(self._name, err))
|
||||
if page.status_code == 200:
|
||||
self._logger.info("{0} : Get author id : {1}".format(self._name, result))
|
||||
result = page.json()
|
||||
@ -556,6 +587,11 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Connection error with status code for get author : {1}".format(self._name, page.status_code))
|
||||
self._logger.debug("{0} : {1}".format(page.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for get author : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for get author : {1}".format(self._name, err))
|
||||
page_is_exist = False
|
||||
|
||||
for index in range(1,10):
|
||||
@ -563,11 +599,6 @@ class WPimport:
|
||||
try:
|
||||
self._logger.info("{0} : Search post with index {2} : {1}".format(self._name, title, index))
|
||||
page = self._request.get("{1}://{0}/wp-json/wp/v2/posts".format(self._wordpress, self._protocol), auth=self._basic, params=params, headers=self._headers_json)
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for search post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for search post : {1}".format(self._name, err))
|
||||
if page.status_code == 200:
|
||||
self._logger.debug("{0} : Encoding : {1}".format(self._name, page.encoding))
|
||||
page.encoding = "utf-8"
|
||||
@ -592,16 +623,16 @@ class WPimport:
|
||||
try:
|
||||
params = {"force":1}
|
||||
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)
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for deleted post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err))
|
||||
if page.status_code == 200:
|
||||
self._logger.info("{0} : Post deleted : {1}".format(self._name, title))
|
||||
else:
|
||||
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))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for deleted post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for deleted post : {1}".format(self._name, err))
|
||||
|
||||
else:
|
||||
self._logger.debug("{0} : Data for post to update : {1}".format(self._name, i))
|
||||
@ -609,11 +640,7 @@ class WPimport:
|
||||
|
||||
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))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err))
|
||||
|
||||
if page.status_code == 200:
|
||||
result = page.json()
|
||||
self._logger.info("{0} : Post updated : {1}".format(self._name, title))
|
||||
@ -622,6 +649,11 @@ class WPimport:
|
||||
else:
|
||||
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))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for update post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for update post : {1}".format(self._name, err))
|
||||
if page.status_code == 400:
|
||||
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))
|
||||
@ -629,16 +661,17 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Connection for update post error with 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 search post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for search post : {1}".format(self._name, err))
|
||||
|
||||
if page_is_exist is False:
|
||||
try:
|
||||
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))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for create post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for create post : {1}".format(self._name, err))
|
||||
|
||||
if page.status_code == 201:
|
||||
result = page.json()
|
||||
self._logger.info("{0} : Post added : {1}".format(self._name, result["title"]["raw"]))
|
||||
@ -647,3 +680,8 @@ class WPimport:
|
||||
else:
|
||||
self._logger.error("{0} : Post not added due status code : {1}".format(self._name, r.status_code))
|
||||
self._logger.debug("{0} : {1}".format(self._name, r.content))
|
||||
except ConnectionError as err:
|
||||
self._logger.error("{0} : Connection error for create post : {1}".format(self._name, err))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
self._logger.error("{0} : Exception error for create post : {1}".format(self._name, err))
|
||||
|
Loading…
x
Reference in New Issue
Block a user