-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
196 lines (152 loc) · 5.19 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import time
import os
import mysql.connector
import json
import openpyxl
import csv
finalContent = {}
#push in finalContent a new entry
def connect_database(environment="development"):
"""Connect to a MySQL database."""
connection_config = {
"development": {
"host": "localhost",
"database": "backend_layout",
"user": "root",
"password": "root",
"port": 8889,
},
"production": {
"host": "database.example.com",
"database": "production",
"user": "prod_user",
"password": "prod_password",
"port": 3306,
},
}
connection_params = connection_config[environment]
try:
connection = mysql.connector.connect(
host=connection_params["host"],
database=connection_params["database"],
user=connection_params["user"],
password=connection_params["password"],
port=connection_params["port"],
)
return connection
except mysql.connector.Error as error:
print("Failed to connect to database {}".format(error))
def getAllIdPagesNews(connection):
try:
cursor = connection.cursor()
select = "SELECT title, uid FROM pages WHERE slug LIKE '%actualites%';"
cursor.execute(select)
result = cursor.fetchall()
for row in result:
getNewsContentByID(connection, row)
print(finalContent)
except mysql.connector.Error as error:
print("Failed to get data from database {}".format(error))
def getNewsContentByID(connection, content):
try:
title = content[0]
uid = content[1]
cursor = connection.cursor()
select = f"SELECT bodytext FROM tt_content WHERE pid = {uid}"
cursor.execute(select)
results = cursor.fetchall() # Récupérer tous les résultats
finalDescription = []
for description in results:
#if description == None then pass
if description[0] == None:
pass
else:
finalDescription.append(description[0])
if len(finalDescription) > 1:
#join all description in one string
finalDescription = " ".join(finalDescription)
finalContent[uid] = {
"title": title,
"description": finalDescription
}
except mysql.connector.Error as error:
print("Failed to get data from database {}".format(error))
connectDatabase = connect_database("development")
getAllIdPagesNews(connectDatabase)
def readJsonFile():
try:
f = open('content.json')
gedItems = json.load(f)
for entry in enumerate(gedItems):
print(entry)
except mysql.connector.Error as error:
print("Failed to read json file {}".format(error))
def readJsonFileWithIndex():
try:
f = open('content.json')
gedItems = json.load(f)
for entry, index in enumerate(gedItems):
print(entry)
except mysql.connector.Error as error:
print("Failed to read json file {}".format(error))
def getPostById(connection, id):
try:
cursor = connection.cursor()
select = f"SELECT * FROM wp_posts WHERE id = {id}"
cursor.execute(select)
result = cursor.fetchone()
except mysql.connector.Error as error:
print("Failed to get data from database {}".format(error))
def read_csv_file(file_path):
try:
with open(file_path, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=';', quotechar='|')
for row in spamreader:
print(', '.join(row))
except FileNotFoundError as error:
print(f"File not found: {error}")
except Exception as error:
print(f"An error occurred: {error}")
def createPostWpPostAndReturnId(connection):
actual_time = time.strftime('%Y-%m-%d %H:%M:%S')
query_post = """
INSERT INTO wp_posts (
post_author,
post_date,
post_date_gmt,
post_content,
post_title,
post_excerpt,
post_name,
to_ping,
pinged,
post_modified,
post_modified_gmt,
post_content_filtered,
guid,
post_type
) VALUES (
1,
'{}',
'{}',
'Mon contenu',
'Post de clément',
'',
'post-de-clement',
'',
'',
'{}',
'{}',
'',
'http://test-python.local/?post_type=product&p=',
'post'
)
""".format(actual_time, actual_time, actual_time)
try:
cursor = connection.cursor()
cursor.execute(query_post)
connection.commit()
return cursor.lastrowid
except mysql.connector.Error as error:
print("Failed to insert data in database {}".format(error))
connectDatabase = connect_database("development")