This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_scripts.py
159 lines (141 loc) · 5.39 KB
/
sql_scripts.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
# Some useful functions for converting flat files from Reddit corpus into SQL database.
# Also includes function to query from Reddit SQL database.
# Data source: https://files.pushshift.io/reddit/
import pandas as pd
import sqlalchemy
def convert_submissions_json_to_sql(datafile, database="reddit_db", db_user="wes"):
"""Converts Reddit json submissions file to SQL database.
Parameters
----------
datafile : string
Name of Reddit json file to be converted.
database : string, optional, default: reddit_db
Name of the SQL database where the file information will be stored.
db_user : string, optional, default: wes
Name of the SQL database user
"""
mykeys = [
"author",
"created_utc",
"id",
"is_self",
"selftext",
"subreddit",
"subreddit_subscribers",
"title",
]
dtypes = {
"author": sqlalchemy.types.Text,
"created_utc": sqlalchemy.types.Integer,
"id": sqlalchemy.types.String(6),
"is_self": sqlalchemy.types.Boolean,
"selftext": sqlalchemy.types.Text,
"subreddit": sqlalchemy.types.Text,
"subreddit_subscribers": sqlalchemy.types.Integer,
"title": sqlalchemy.types.Text,
"url": sqlalchemy.types.Text,
}
engine = sqlalchemy.create_engine(f"postgresql://{db_user}@localhost/{database}")
reader = pd.read_json(datafile, lines=True, chunksize=10000, dtype=False)
i = 0
for chunk in reader:
# Exclude submissions marked for over 18 and only include keys specified above
chunk.loc[chunk["over_18"] == False, mykeys].to_sql(
"submissions", engine, if_exists="append", dtype=dtypes
)
i += 1
print(i)
engine.dispose()
def convert_subreddits_json_to_sql(
datafile="DATA/subreddits.json", database="reddit_db", db_user="wes"
):
"""Converts Reddit json subreddits file to SQL database. Excludes adult content,
non-English content, and private & archived subreddits.
Parameters
----------
datafile : string
Name of json file containing subreddit information to be converted.
database : string, optional, default: reddit_db
Name of the SQL database where the file information will be stored.
db_user : string, optional, default: wes
Name of the SQL database user
"""
mykeys = ["display_name", "description", "subscribers"]
dtypes = {
"display_name": sqlalchemy.types.Text,
"description": sqlalchemy.types.Text,
"subscribers": sqlalchemy.types.Integer,
}
engine = sqlalchemy.create_engine(f"postgresql://{db_user}@localhost/{database}")
reader = pd.read_json(datafile, lines=True, chunksize=10000, dtype=False)
i = 0
for chunk in reader:
# No adult content, only English, only public subreddits
chunk.loc[
(chunk["over18"] == False)
& (chunk["subreddit_type"] == "public")
& (chunk["lang"] == "en"),
mykeys,
].to_sql("subreddits", engine, if_exists="append", dtype=dtypes)
i += 1
print(i)
engine.dispose()
def query_submissions(
subscribers_llimit=1000,
subscribers_ulimit=1500,
chunksize=None,
db="reddit_db",
db_user="wes",
):
"""Queries the Reddit submission database. Only selects submissions from subreddits
that have:
* number subscribers within a specified range
* not been deleted or removed
* not labeled as over 18
* English as language
* status as public (not private or archived)
* only contain a self post (text post)
This list of subreddits comes from the table titled 'subreddits' in the same
database.
Parameters
----------
subscribers_llimit : integer, optional, default: 1000
Minimum number of subscribers for a subreddit to be included.
subscribers_ulimit : integer, optional, default: 1500
Maximum number of subscribers for a subreddit to be included.
da : string, optional, default: reddit_db
Name of the SQL database where the file information will be stored.
db_user : string, optional, default: wes
Name of the SQL database user
"""
engine = sqlalchemy.create_engine(f"postgresql://{db_user}@localhost/{db}")
if subscribers_ulimit != None:
df = pd.read_sql(
f"""
select title, selftext, subreddit from submissions
where subreddit_subscribers > {subscribers_llimit}
and subreddit_subscribers < {subscribers_ulimit}
and is_self = 'True'
and selftext <> '[deleted]'
and selftext <> '[removed]'
and subreddit in (select display_name from subreddits
where subscribers > {subscribers_llimit}
and subscribers < {subscribers_ulimit});""",
engine,
chunksize=chunksize,
)
else:
df = pd.read_sql(
f"""
select title, selftext, subreddit from submissions
where subreddit_subscribers > {subscribers_llimit}
and is_self = 'True'
and selftext <> '[deleted]'
and selftext <> '[removed]'
and subreddit in (select display_name from subreddits
where subscribers > {subscribers_llimit});""",
engine,
chunksize=chunksize,
)
engine.dispose()
return df