/
query_data.py
72 lines (59 loc) · 1.97 KB
/
query_data.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
from convert import Convert
import mysql.connector
from mysql.connector import MySQLConnection, Error
import dotenv
import os
dotenv.load_dotenv()
class QueryData:
def __init__(self):
self.table = os.getenv("DB_TABLE")
self.tagging = os.getenv("TAGGING")
self.conn = mysql.connector.connect(
host=os.getenv("DB_HOST"),
database=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASS")
)
self.convert = Convert()
self.cursor = self.conn.cursor()
def handle(self):
""" get all the data and iterate over the content """
try:
rows = self.all()
for row in rows:
item = self.parse_item(row)
print(f"Writing item {item['title']}")
item = self.convert.convert_item(item)
self.convert.write_converted_item(item)
except Error as e:
print(e)
finally:
self.cursor.close()
self.conn.close()
def all(self):
""" connect to db """
self.cursor.execute(f"SELECT * FROM {self.table}")
return self.cursor.fetchall()
def get_tags(self, model_id):
""" get all the tags from the system """
self.cursor.execute(
f"SELECT tags.name FROM post_tag LEFT JOIN tags on tags.id=post_tag.tag_id where post_id={model_id}")
tags = self.cursor.fetchall()
all_tags = []
for tag in tags:
all_tags.append(tag[0])
return ", ".join(all_tags)
def parse_item(self, item):
""" pass the one item to convert """
tags = None
if self.tagging == True:
tags = self.get_tags(item[0])
model = {
"id": item[0],
"title": item[1],
"body": item[2],
"active": item[4],
"tags": tags,
"created_at": item[5].strftime("%Y-%m-%d")
}
return model