-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
144 lines (122 loc) · 3.96 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
import time
import os
import mysql.connector
import json
import openpyxl
import csv
def connect_database(environment="development"):
"""Connect to a MySQL database."""
connection_config = {
"development": {
"host": "localhost",
"database": "local",
"user": "main",
"password": "main",
"port": 10043,
},
"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 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 getAllPost(connection):
try:
cursor = connection.cursor()
select = "SELECT * FROM wp_posts"
cursor.execute(select)
result = cursor.fetchall()
return result
except mysql.connector.Error as error:
print("Failed to get data from database {}".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 = connectDatabase()