-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmissions-db.py
More file actions
86 lines (78 loc) · 3.02 KB
/
Copy pathsubmissions-db.py
File metadata and controls
86 lines (78 loc) · 3.02 KB
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
import sqlite3
import json
import argparse
from tqdm import tqdm
def create_table(cursor):
cursor.execute("""
CREATE TABLE IF NOT EXISTS posts (
id TEXT PRIMARY KEY,
subreddit TEXT,
subreddit_id TEXT,
title TEXT,
selftext TEXT,
url TEXT,
permalink TEXT,
created_utc INTEGER,
score INTEGER,
num_comments INTEGER,
ups REAL,
downs REAL,
author TEXT,
author_flair_text TEXT,
is_self INTEGER,
domain TEXT,
over_18 INTEGER,
media TEXT,
edited INTEGER,
stickied INTEGER,
distinguished TEXT
)
""")
def insert_data(cursor, jsonl_file):
with open(jsonl_file, "r", encoding="utf-8") as file:
lines = file.readlines()
for line in tqdm(lines, desc="Inserting posts"):
post = json.loads(line.strip())
cursor.execute("""
INSERT OR IGNORE INTO posts (
id, subreddit, subreddit_id, title, selftext, url, permalink,
created_utc, score, num_comments, ups, downs, author, author_flair_text,
is_self, domain, over_18, media, edited, stickied, distinguished
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
post.get("id", "unknown"),
post.get("subreddit", "unknown"),
post.get("subreddit_id", "unknown"),
post.get("title", "unknown"),
post.get("selftext", ""),
post.get("url", "unknown"),
post.get("permalink", "unknown"),
post.get("created_utc", -1),
post.get("score", -1),
post.get("num_comments", -1),
post.get("ups", -1.0),
post.get("downs", -1.0),
post.get("author", "unknown"),
post.get("author_flair_text", "unknown"),
int(post.get("is_self", False)),
post.get("domain", "unknown"),
int(post.get("over_18", False)),
json.dumps(post.get("media")) if post.get("media") else "unknown",
int(post.get("edited", -1)),
int(post["stickied"]) if post.get("stickied") is not None else -1,
post.get("distinguished", "unknown")
))
def main():
parser = argparse.ArgumentParser(description="Insert JSONL data into a SQLite database.")
parser.add_argument("folder", type=str, help="Folder containing the JSONL data files")
args = parser.parse_args()
conn = sqlite3.connect(f"{args.folder}/{args.folder}_submissions.db")
cursor = conn.cursor()
create_table(cursor)
insert_data(cursor, f"{args.folder}/{args.folder}_submissions.jsonl")
conn.commit()
conn.close()
print("Data successfully inserted into the database.")
if __name__ == "__main__":
main()