-
Notifications
You must be signed in to change notification settings - Fork 1
/
subreddit_scraper.py
106 lines (86 loc) · 2.97 KB
/
subreddit_scraper.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
import multiprocessing
import praw
from psaw import PushshiftAPI
import mysql.connector
import datetime as dt
from multiprocessing import Pool
import sys
from config import *
# parallelization parameters
NUMBER_OF_PROCESSES = NUMBER_OF_PROCESSES_OVERRIDE or multiprocessing.cpu_count()
reddit = praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
user_agent=USER_AGENT,
)
reddit_db = mysql.connector.connect(
host=HOST, user=USER, password=PASSWORD, database=DATABASE_NAME
)
def add_subreddit_posts(subreddit_name):
api = PushshiftAPI()
for post in api.search_submissions(subreddit=subreddit_name):
sql = "INSERT INTO posts (id, title, body, subreddit, timestamp) VALUES (%s, %s, %s, %s, %s)"
try:
val = (
post.id,
post.title,
post.selftext,
post.subreddit,
dt.datetime.utcfromtimestamp(post.created),
)
cursor.execute(sql, val)
reddit_db.commit()
except AttributeError:
continue
def add_post_comments(post_id):
cursor = reddit_db.cursor()
post = reddit.submission(post_id)
post.comments.replace_more(limit=None)
for comment in post.comments.list():
try:
parent_id = comment.parent_id[3:]
if parent_id == post_id:
parent_id = None
sql = "INSERT INTO comments (id, body, post_id, timestamp, parent_comment) VALUES (%s, %s, %s, %s, %s)"
val = (
comment.id,
comment.body,
post_id,
dt.datetime.utcfromtimestamp(comment.created),
parent_id
)
cursor.execute(sql, val)
except:
e = sys.exc_info()[0]
print(e)
print(post_id)
continue
reddit_db.commit()
def is_table_present(table_name):
cursor = reddit_db.cursor()
sql = f"SHOW tables like '{table_name}'"
cursor.execute(sql)
return len(cursor.fetchall()) != 0
cursor = reddit_db.cursor()
# create posts table if not present
if not is_table_present("posts"):
cursor.execute(
"CREATE TABLE posts(id VARCHAR(255) PRIMARY KEY, title TEXT, body TEXT, subreddit VARCHAR(255))"
)
# create comments table if not present
if not is_table_present("comments"):
cursor.execute(
"CREATE TABLE comments(id VARCHAR(255), body TEXT, timestamp TIMESTAMP, post_id VARCHAR(200) references posts(id)"
)
# adds posts from the specified subreddit
subreddit_name = input("name of the subreddit you want to scrape: ")
print("scraping posts...")
add_subreddit_posts(SUBREDDIT_NAME)
sql = "SELECT id from posts"
cursor.execute(sql)
post_ids = cursor.fetchall()
post_ids = [post_id[0] for post_id in post_ids]
# adds in parallel comments from pre-specified posts
print("scraping comments (this is gonna take a while)...")
with Pool(NUMBER_OF_PROCESSES) as pool:
pool.map(add_post_comments, post_ids)