diff --git a/lib/WPImport.py b/lib/WPImport.py index f8d8a35..6108582 100644 --- a/lib/WPImport.py +++ b/lib/WPImport.py @@ -86,34 +86,6 @@ class WPimport: else: self._addOrUpdateFeaturedMedia(soup) - def removeAll(self): - params = {"per_page":100} - try: - self._logger.info("{0} : List posts to remove for url : {1}".format(self._name, self._wordpress)) - - r = self._request.get("http://{0}/wp-json/wp/v2/posts".format(self._wordpress), auth=self._basic, params=params, headers=self._headers_json) - except Exception as err: - self._logger.error("{0} : Connection error for list post to remove : {1}".format(self._name, err)) - if r.status_code == 200: - result = r.json() - if len(result) > 0: - for i in result: - self._logger.info("{0} : Remove article for url {1} : {2}".format(self._name, self._wordpress, i["title"]["rendered"])) - params = {"force":1} - try: - r = self._request.delete("http://{0}/wp-json/wp/v2/posts/{1}".format(self._wordpress, i["id"]), auth=self._basic, headers=self._headers_json , params=params) - if r.status_code == 200: - self._logger.info("{0} : Post removed for URL {1} : {2}".format(self._name, self._wordpress, i["title"]["rendered"])) - else: - self._logger.error("{0} : Connection error for post {1} with status code {2}".format(self._name, self._wordpress, i["title"]["rendered"])) - except Exception as err: - self._logger.error("{0} : Connection error for post remove : {1}".format(self._name, err)) - exit(1) - self.removeAll() - else: - self._logger.error("{0} : Error for list to remove due status code {1}".format(self._name, r.status_code)) - self._logger.debug("{0} : Content error : {1}".format(self._name, r.content)) - # Private method diff --git a/lib/WPRemove.py b/lib/WPRemove.py new file mode 100644 index 0000000..ad4ab8a --- /dev/null +++ b/lib/WPRemove.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 + +from bs4 import BeautifulSoup +from urllib.parse import urlparse +import requests, os, logging, re, json +from requests.adapters import HTTPAdapter +from requests.packages.urllib3.util.retry import Retry + +class WPRemove: + # Constructor + def __init__(self, name="Thread-0", basic=None, wordpress="", logger=None): + self._name = name + self._basic = basic + self._wordpress = wordpress + self._logger = logger + self._headers_json = {'Content-Type': 'application/json', 'Accept':'application/json'} + + self._request = requests.Session() + + retries = Retry(connect=10, read=10, redirect=5, + status_forcelist=[429, 500, 502, 503, 504], backoff_factor=2) + + self._request.mount('http://', HTTPAdapter(max_retries=retries)) + + + # Destructor + def __del__(self): + print("{0} : Import finished for {1}".format(self._name, self._wordpress)) + + # Public method + + def setUrl(self, wordpress): + self._wordpress = wordpress + + def cleanPosts(self): + self._removeAll("posts") + + def cleanTags(self): + self._removeAll("tags") + + def cleanCategories(self): + self._removeAll("categories") + + def cleanMedia(self): + self._removeAll("media") + + # Private method + + def _removeAll(self, composant): + params = {"per_page":100} + try: + self._logger.info("{0} : List {2} to remove for url : {1}".format(self._name, self._wordpress, composant)) + + r = self._request.get("http://{0}/wp-json/wp/v2/{1}".format(self._wordpress, composant), auth=self._basic, params=params, headers=self._headers_json) + except Exception as err: + self._logger.error("{0} : Connection error for list {1} to remove : {2}".format(self._name, composant, err)) + if r.status_code == 200: + result = r.json() + if len(result) > 0: + for i in result: + self._logger.info("{0} : Remove {2} for url {1} : {3}".format(self._name, self._wordpress, composant, i["title"]["rendered"])) + params = {"force":1} + try: + r = self._request.delete("http://{0}/wp-json/wp/v2/{1}/{2}".format(self._wordpress, composant, i["id"]), auth=self._basic, headers=self._headers_json , params=params) + if r.status_code == 200: + self._logger.info("{0} : Post removed for URL {1} {2} : {3}".format(self._name, self._wordpress, composant, i["title"]["rendered"])) + else: + self._logger.error("{0} : Connection error for post {1} {2} {3} with status code {4}".format(self._name, self._wordpress, composant, i["title"]["rendered"], r.status_code)) + except Exception as err: + self._logger.error("{0} : Connection error for {1} remove : {2}".format(self._name, composant, err)) + exit(1) + self._removeAll(composant) + else: + self._logger.error("{0} : Error for list to remove {1} due status code {2}".format(self._name, composant, r.status_code)) + self._logger.debug("{0} : Content error for {1} : {2}".format(self._name, composant, r.content))