2023-04-08 12:27:30 +02:00
#!/usr/bin/python3
from bs4 import BeautifulSoup
from urllib . parse import urlparse
import requests , os , logging , re , json
2023-04-13 21:54:35 +02:00
from requests . adapters import HTTPAdapter
from requests . packages . urllib3 . util . retry import Retry
2023-04-08 12:27:30 +02:00
class WPimport :
# Constructor
2023-04-09 21:17:49 +02:00
def __init__ ( self , basic , wordpress , logger , parser ) :
2023-04-08 12:27:30 +02:00
self . _basic = basic
self . _wordpress = wordpress
self . _logger = logger
2023-04-09 21:17:49 +02:00
self . _parser = parser
2023-04-18 00:00:32 +02:00
self . _headers_json = { ' Content-Type ' : ' application/json ' , ' Accept ' : ' application/json ' }
2023-04-08 12:27:30 +02:00
2023-04-13 21:54:35 +02:00
self . _request = requests . Session ( )
2023-04-13 21:59:12 +02:00
retries = Retry ( total = 10 ,
status_forcelist = [ 429 , 500 , 502 , 503 , 504 ] , backoff_factor = 2 )
2023-04-13 21:54:35 +02:00
self . _request . mount ( ' http:// ' , HTTPAdapter ( max_retries = retries ) )
2023-04-08 12:27:30 +02:00
# Public method
2023-04-10 15:41:14 +02:00
def setUrl ( self , wordpress ) :
self . _wordpress = wordpress
2023-04-10 00:00:01 +02:00
def fromUrl ( self , webpage ) :
2023-04-10 11:05:32 +02:00
for i in range ( 0 , len ( webpage ) ) :
2023-04-13 22:14:30 +02:00
try :
r = self . _request . get ( webpage [ i ] )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-10 00:00:01 +02:00
if r . status_code == 200 :
2023-04-10 11:05:32 +02:00
self . _logger . info ( " ( {0} / {1} : Page en cours d ' import : {2} " . format ( i + 1 , len ( webpage ) , webpage [ i ] ) )
2023-04-10 00:00:01 +02:00
soup = BeautifulSoup ( r . content , self . _parser )
articlebody = soup . find_all ( " div " , class_ = " articlebody " )
if len ( articlebody ) > 0 :
self . _addOrUpdatePost ( soup )
else :
self . _addOrUpdateFeaturedMedia ( soup )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( r . status_code ) )
2023-04-10 00:00:01 +02:00
2023-04-08 12:27:30 +02:00
def fromDirectory ( self , directory ) :
directory = " {0} /archives " . format ( directory )
directories = self . _getDirectories ( [ ] , " {0} " . format ( directory ) )
files = self . _getFiles ( directories )
self . fromFile ( files )
def fromFile ( self , files ) :
for file in files :
if os . path . exists ( file ) :
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Fichier en cours de traitement : {0} " . format ( file ) )
2023-04-08 12:27:30 +02:00
with open ( file , ' r ' ) as f :
content = f . read ( )
2023-04-09 21:17:49 +02:00
soup = BeautifulSoup ( content , self . _parser )
2023-04-08 12:27:30 +02:00
articlebody = soup . find_all ( " div " , class_ = " articlebody " )
if len ( articlebody ) > 0 :
self . _addOrUpdatePost ( soup )
else :
self . _addOrUpdateFeaturedMedia ( soup )
# Private method
## Get all files
def _getFiles ( self , item ) :
files = [ ]
for i in item :
for j in os . listdir ( i ) :
if os . path . isfile ( " {0} / {1} " . format ( i , j ) ) :
files . append ( " {0} / {1} " . format ( i , j ) )
return files
## Get directories
def _getDirectories ( self , subdirectory , item ) :
sub = subdirectory
for i in os . listdir ( item ) :
if os . path . isdir ( " {0} / {1} " . format ( item , i ) ) :
sub . append ( " {0} / {1} " . format ( item , i ) )
subdirectory = self . _getDirectories ( sub , " {0} / {1} " . format ( item , i ) )
return subdirectory
## Add or update featured media
def _addOrUpdateFeaturedMedia ( self , soup ) :
item_div = soup . find_all ( " div " , { " data-edittype " : " post " } )
for i in item_div :
h2 = i . find_all ( " h2 " ) [ 0 ] . text
params = { " search " : h2 , " type " : " post " }
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/search " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 200 :
result = page . json ( )
if len ( result ) > 0 :
if h2 == result [ 0 ] [ " title " ] :
img = i . find_all ( " img " )
if len ( img ) > 0 :
img_src = img [ 0 ] . get ( " src " )
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( img_src )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
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 }
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/media " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 200 :
res = page . json ( )
if len ( res ) > 0 :
id_media = res [ 0 ] [ " id " ]
data = { " featured_media " : id_media }
2023-04-13 22:14:30 +02:00
try :
2023-04-18 00:00:32 +02:00
r = self . _request . post ( " http:// {0} /wp-json/wp/v2/posts/ {1} " . format ( self . _wordpress , result [ 0 ] [ " id " ] ) , auth = self . _basic , headers = self . _headers_json , data = json . dumps ( data ) )
2023-04-13 22:14:30 +02:00
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if r . status_code == 200 :
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Ajout media featured : {0} " . format ( r . json ( ) [ " title " ] [ " raw " ] ) )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( r . status_code ) )
2023-04-08 12:27:30 +02:00
else :
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Aucun media trouvé pour {0} " . format ( h2 ) )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
2023-04-08 12:27:30 +02:00
## Association image to post
def _linkImgPost ( self , title , list_img , post_id ) :
for i in list_img :
data = { " post " : post_id }
2023-04-13 22:14:30 +02:00
try :
r = self . _request . post ( " http:// {0} /wp-json/wp/v2/media/ {1} " . format ( self . _wordpress , i [ " id " ] ) , auth = self . _basic , data = data )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if r . status_code == 200 :
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Association d ' une image à l ' article {0} " . format ( title ) )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( r . status_code ) )
2023-04-08 12:27:30 +02:00
## Add or update img
def _addOrUpdateMedia ( self , href_img , page ) :
media = { " id " : " " , " rendered " : " " }
split_fileimg = href_img . split ( " / " )
img_name = split_fileimg [ len ( split_fileimg ) - 1 ]
params = { " search " : img_name }
2023-04-13 22:14:30 +02:00
try :
r = self . _request . get ( " http:// {0} /wp-json/wp/v2/media " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if r . status_code == 200 :
res = r . json ( )
if len ( res ) > 0 :
params = { " force " : 1 }
2023-04-13 22:14:30 +02:00
try :
r = self . _request . delete ( " http:// {0} /wp-json/wp/v2/media/ {1} " . format ( self . _wordpress , res [ 0 ] [ " id " ] ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if r . status_code == 200 :
2023-04-18 00:00:32 +02:00
self . _logger . info ( " Image removed {0} " . format ( img_name ) )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Image not removed due status code : {0} " . format ( r . status_code ) )
2023-04-08 12:27:30 +02:00
data = page . content
img_type = " image/png "
if img_name . split ( " . " ) [ 1 ] == " jpg " or img_name . split ( " . " ) [ 1 ] == " jpeg " :
img_type = " image/jpg "
headers = { ' Content-Type ' : img_type , ' Content-Disposition ' : ' attachment; filename= {0} ' . format ( img_name ) }
2023-04-13 22:14:30 +02:00
try :
r = self . _request . post ( " http:// {0} /wp-json/wp/v2/media " . format ( self . _wordpress ) , auth = self . _basic , headers = headers , data = data )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if r . status_code == 201 :
2023-04-18 00:00:32 +02:00
self . _logger . info ( " Image added {0} " . format ( img_name ) )
2023-04-08 12:27:30 +02:00
res = r . json ( )
media [ " id " ] = res [ " id " ]
media [ " rendered " ] = res [ " guid " ] [ " rendered " ]
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Image not added due status code : {0} " . format ( r . status_code ) )
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( r . status_code ) )
2023-04-08 12:27:30 +02:00
return media
2023-04-11 22:30:00 +02:00
## Add or update comment
2023-04-11 23:26:40 +02:00
def _addOrUpdateComment ( self , post , comment , title ) :
params = { " post " : post }
block = True
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/comments " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-11 23:26:40 +02:00
if page . status_code == 200 :
result = page . json ( )
for i in comment :
comment_exist = False
for j in result :
if i [ " author " ] == j [ " author_name " ] and i [ " date " ] == j [ " date " ] :
comment_exist = True
id_comment = j [ " id " ]
data = { " post " : post , " content " : i [ " content " ] , " date " : i [ " date " ] , " author_name " : i [ " author " ] }
if comment_exist is True :
2023-04-13 22:14:30 +02:00
try :
page = page = self . _request . post ( " http:// {0} /wp-json/wp/v2/comments/ {1} " . format ( self . _wordpress , id_comment ) , auth = self . _basic , data = data )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-11 23:26:40 +02:00
if page . status_code == 200 :
2023-04-18 00:00:32 +02:00
self . _logger . info ( " Comment updated for {0} " . format ( title ) )
2023-04-17 23:44:09 +02:00
else :
2023-04-18 00:00:32 +02:00
self . _logger . error ( " Comment not updated for {0} due status code : {1} " . format ( title , page . status_code ) )
2023-04-11 23:26:40 +02:00
else :
2023-04-16 21:25:32 +02:00
if i [ " parent_id " ] != - 1 :
parent_id = int ( i [ " parent_id " ] )
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/comments " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
if page . status_code == 200 :
result = page . json ( )
for j in result :
if comment [ parent_id ] [ " author " ] == j [ " author_name " ] and comment [ parent_id ] [ " date " ] == j [ " date " ] :
data [ " parent " ] = j [ " id " ]
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
self . _logger . info ( data )
2023-04-13 22:14:30 +02:00
try :
page = self . _request . post ( " http:// {0} /wp-json/wp/v2/comments " . format ( self . _wordpress ) , auth = self . _basic , data = data )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-17 23:44:09 +02:00
self . _logger . info ( page . status_code )
2023-04-11 23:26:40 +02:00
if page . status_code == 201 :
2023-04-18 00:00:32 +02:00
self . _logger . info ( " Comment added for {0} " . format ( title ) )
2023-04-17 23:44:09 +02:00
else :
2023-04-18 00:00:32 +02:00
self . _logger . error ( " Comment not added for {0} due status code : {1} " . format ( title , page . status_code ) )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
2023-04-08 12:27:30 +02:00
2023-04-16 19:16:23 +02:00
## Check class name
def _hasClassName ( self , tag , className ) :
for i in tag [ " class " ] :
if i == className :
return True
return False
## Get class name
def _getClassName ( self , tag , className ) :
for i in tag [ " class " ] :
if re . match ( className , i ) :
return i
return " "
2023-04-14 23:10:07 +02:00
2023-04-16 19:16:23 +02:00
## Get all comments
2023-04-14 23:10:07 +02:00
def _getComment ( self , comment ) :
comment_post = [ ]
2023-04-16 19:32:00 +02:00
for i in range ( 0 , len ( comment ) ) :
comment_div = comment [ i ] . find ( " div " , class_ = " comment_item " )
2023-04-14 23:10:07 +02:00
comment_item = comment_div . text . split ( " \n " )
footer = comment_div . find_all ( " div " , class_ = " itemfooter " )
comment_author = footer [ 0 ] . text . split ( " , " ) [ 0 ] . replace ( " Posté par " , " " )
comment_date = footer [ 0 ] . find_all ( " abbr " ) [ 0 ] . get ( " title " )
comment_content = " <p> "
for j in range ( 0 , len ( comment_item ) - 2 ) :
if len ( comment_item [ j ] ) > 0 :
comment_content = comment_content + comment_item [ j ] + " <br /> "
comment_content = comment_content + " </p> "
parent = - 1
2023-04-16 21:06:04 +02:00
if self . _hasClassName ( comment [ i ] , " level-1 " ) is False :
2023-04-16 19:16:23 +02:00
block = False
2023-04-16 19:32:00 +02:00
className = self . _getClassName ( comment [ i ] , " level- " ) . split ( " - " )
2023-04-16 19:16:23 +02:00
level = 1
if len ( className ) > 0 :
level = int ( className [ 1 ] )
2023-04-16 21:06:04 +02:00
for j in range ( i - 1 , 0 , - 1 ) :
2023-04-16 19:16:23 +02:00
if block is False :
levelName = " level- {0} " . format ( level - 1 )
2023-04-16 21:06:04 +02:00
if self . _hasClassName ( comment [ j ] , levelName ) is True :
2023-04-16 19:16:23 +02:00
parent = j
block = True
2023-04-14 23:10:07 +02:00
2023-04-16 21:06:04 +02:00
comment_post . append ( { " author " : comment_author , " date " : comment_date , " content " : comment_content , " parent_id " : parent } )
2023-04-14 23:10:07 +02:00
return comment_post
2023-04-08 12:27:30 +02:00
## Add or Update post
def _addOrUpdatePost ( self , soup ) :
tags = [ ]
month = { " janvier " : " 01 " , " février " : " 02 " , " mars " : " 03 " , " avril " : " 04 " , " mai " : " 05 " , " juin " : " 06 " , " juillet " : " 07 " , " août " : " 08 " , " septembre " : " 09 " , " octobre " : " 10 " , " novembre " : " 11 " , " décembre " : " 12 " }
liste = [ " categories " , " tags " ]
elements = { }
element = { }
listelement = { }
for i in liste :
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/ {1} " . format ( self . _wordpress , i ) )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 200 :
elements [ i ] = page . json ( )
element [ i ] = [ ]
listelement [ i ] = [ ]
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
2023-04-08 12:27:30 +02:00
articletitle = soup . find_all ( " h2 " , class_ = " articletitle " )
articlebody = soup . find_all ( " div " , class_ = " articlebody " )
articledate = soup . find_all ( " span " , class_ = " articledate " )
articleacreator = soup . find_all ( " span " , class_ = " articlecreator " )
dateheader = soup . find_all ( " div " , class_ = " dateheader " )
itemfooter = soup . find_all ( " div " , class_ = " itemfooter " )
2023-04-14 23:10:07 +02:00
comment = soup . find_all ( " li " , class_ = " comment " )
2023-04-08 12:27:30 +02:00
img_a = articlebody [ 0 ] . find_all ( " a " , { " target " : " _blank " } )
list_img = [ ]
for i in img_a :
new_img = { }
img = i . find_all ( " img " )
if len ( img ) > 0 :
href_a = i . get ( " href " )
href_img = img [ 0 ] . get ( " src " )
new_img [ " old_src " ] = href_img
new_img [ " old_href " ] = href_a
2023-04-13 22:14:30 +02:00
try :
page_img = self . _request . get ( href_img )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page_img . status_code == 404 :
href_img = href_a
2023-04-13 22:14:30 +02:00
try :
page_img = self . _request . get ( href_a )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-18 00:00:32 +02:00
2023-04-08 12:27:30 +02:00
if page_img . status_code == 200 :
media = self . _addOrUpdateMedia ( href_img , page_img )
new_img [ " id " ] = media [ " id " ]
new_img [ " new_src " ] = media [ " rendered " ]
list_img . append ( new_img )
if href_img != href_a :
media = self . _addOrUpdateMedia ( href_a , page_img )
new_img [ " id " ] = media [ " id " ]
new_img [ " new_src " ] = media [ " rendered " ]
list_img . append ( new_img )
2023-04-18 00:00:32 +02:00
if page_img . status_code not in [ 200 , 404 ] :
self . _logger . error ( " Connection error with status code : {0} " . format ( page_img . status_code ) )
2023-04-08 12:27:30 +02:00
2023-04-14 23:10:07 +02:00
comment_post = self . _getComment ( comment )
2023-04-08 12:27:30 +02:00
a = itemfooter [ 0 ] . find_all ( " a " , { " rel " : True } )
for i in a :
rel = i . get ( " rel " )
if rel [ 0 ] == ' tag ' :
href = i . get ( " href " )
if re . search ( r ' /tag/ ' , href ) :
element [ " tags " ] . append ( i . text )
if re . search ( r ' /archives/ ' , href ) :
element [ " categories " ] . append ( i . text )
for i in liste :
for j in element [ i ] :
element_exist = False
for k in elements [ i ] :
if k [ " name " ] == j :
element_exist = True
listelement [ i ] . append ( k [ " id " ] )
if element_exist is False :
data = { " name " : j }
2023-04-18 00:00:32 +02:00
self . _logger . debug ( " URL : {0} with data : {1} and with headers : {2} " . format ( " http:// {0} /wp-json/wp/v2/ {1} " . format ( self . _wordpress , i ) , data , self . _headers_json ) )
2023-04-13 22:14:30 +02:00
try :
2023-04-18 00:00:32 +02:00
page = self . _request . post ( " http:// {0} /wp-json/wp/v2/ {1} " . format ( self . _wordpress , i ) , auth = self . _basic , headers = self . _headers_json , data = data )
2023-04-13 22:14:30 +02:00
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 201 :
result = page . json ( )
listelement [ i ] . append ( result [ " id " ] )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " {0} not added due status code : {1} " . format ( i , page . status_code ) )
2023-04-08 12:27:30 +02:00
title = articletitle [ 0 ] . text
author = articleacreator [ 0 ] . text . lower ( )
body = articlebody [ 0 ] . find_all ( " p " )
bodyhtml = " <p> "
for i in body :
if len ( i . text ) == 1 :
bodyhtml = bodyhtml + " <br /> "
else :
bodyhtml = bodyhtml + str ( i ) . replace ( " <p> " , " " ) . replace ( " </p> " , " " ) . replace ( " <br> " , " <br /> " ) + " <br /> "
bodyhtml = bodyhtml + " </p> "
for i in list_img :
o = urlparse ( i [ " new_src " ] )
bodyhtml = bodyhtml . replace ( i [ " old_href " ] , o . path )
bodyhtml = bodyhtml . replace ( i [ " old_src " ] , o . path )
hour = articledate [ 0 ] . text
time = dateheader [ 0 ] . text . split ( " " )
data = { " title " : title , " content " : bodyhtml , " status " : " publish " , " date " : " {0} - {1} - {2} T {3} :00 " . format ( time [ 2 ] , month [ time [ 1 ] ] , time [ 0 ] , hour ) , " tags " : listelement [ " tags " ] , " categories " : listelement [ " categories " ] }
params = { " search " : author }
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/users " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 200 :
result = page . json ( )
data [ " author " ] = result [ 0 ] [ " id " ]
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
2023-04-08 12:27:30 +02:00
params = { " search " : title }
2023-04-13 22:14:30 +02:00
try :
page = self . _request . get ( " http:// {0} /wp-json/wp/v2/posts " . format ( self . _wordpress ) , auth = self . _basic , params = params )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
page_exist = True
headers = { ' Content-Type ' : ' application/json ' , ' Accept ' : ' application/json ' }
if page . status_code == 200 :
result = page . json ( )
if len ( result ) == 0 :
page_exist = False
else :
2023-04-08 21:27:35 +02:00
self . _logger . info ( " La page {0} existe deja et mis à jour " . format ( title ) )
2023-04-08 12:27:30 +02:00
post_id = result [ 0 ] [ " id " ]
2023-04-13 22:14:30 +02:00
try :
page = self . _request . post ( " http:// {0} /wp-json/wp/v2/posts/ {1} " . format ( self . _wordpress , post_id ) , auth = self . _basic , headers = headers , data = json . dumps ( data ) )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 200 :
result = page . json ( )
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Article mis à jour : {0} " . format ( result [ " title " ] [ " raw " ] ) )
2023-04-11 23:26:40 +02:00
self . _addOrUpdateComment ( result [ " id " ] , comment_post , result [ " title " ] [ " raw " ] )
2023-04-08 12:27:30 +02:00
self . _linkImgPost ( result [ " title " ] [ " raw " ] , list_img , result [ " id " ] )
2023-04-17 23:44:09 +02:00
else :
self . _logger . error ( " Post not updated due status code : {0} " . format ( page . status_code ) )
else :
self . _logger . error ( " Connection error with status code : {0} " . format ( page . status_code ) )
2023-04-11 23:27:41 +02:00
2023-04-08 12:27:30 +02:00
if page_exist == False :
2023-04-13 22:14:30 +02:00
try :
page = self . _request . post ( " http:// {0} /wp-json/wp/v2/posts " . format ( self . _wordpress ) , auth = self . _basic , headers = headers , data = json . dumps ( data ) )
except Exception as err :
self . _logger . error ( " Connection error : {0} " . format ( err ) )
exit ( 1 )
2023-04-08 12:27:30 +02:00
if page . status_code == 201 :
result = page . json ( )
2023-04-08 21:27:35 +02:00
self . _logger . info ( " Article ajoute : {0} " . format ( result [ " title " ] [ " raw " ] ) )
2023-04-11 23:26:40 +02:00
self . _addOrUpdateComment ( result [ " id " ] , comment_post , result [ " title " ] [ " raw " ] )
2023-04-17 23:44:09 +02:00
self . _linkImgPost ( result [ " title " ] [ " raw " ] , list_img , result [ " id " ] )
else :
self . _logger . error ( " Post not added due status code : {0} " . format ( r . status_code ) )