File size: 1,227 Bytes
85763d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from tqdm import tqdm
from db import *
from playhouse.shortcuts import model_to_dict


def make_csv_header():
    post = Post.get_by_id(1)
    return ",".join([field for field in model_to_dict(post).keys()])


rating_enum = {
    "general": 0,
    "sensitive": 1,
    "questionable": 2,
    "explicit": 3,
}


def read_rows():
    for data in tqdm(db.execute_sql("SELECT * FROM post;").fetchall()):
        data = [
            (
                (" ".join(str(int(tag, 36)) for tag in i[1:-1].split("#$")))
                if isinstance(i, str) and i and i[0] == "$" and i[-1] == "#"
                else int(i)
                if isinstance(i, bool)
                else ""
                if i is None or i == ""
                else rating_enum[i]
                if i in rating_enum
                else '"{}"'.format(i.replace('"', '""'))
                if "," in str(i)
                else i
            )
            for i in data
        ]
        assert len(data) == 38, ",".join([str(i) for i in data])
        csv_str = ",".join([str(i) for i in data])
        yield csv_str


with open("./db.csv", "w", encoding="utf-8") as f:
    f.write(f"{make_csv_header()}\n")
    f.writelines(f"{i}\n" for i in read_rows())