Datasets:
Upload reddit_data_collection.py
Browse files- reddit_data_collection.py +95 -0
reddit_data_collection.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import praw
|
2 |
+
import pandas as pd
|
3 |
+
from datetime import datetime, timezone
|
4 |
+
from textblob import TextBlob
|
5 |
+
import csv
|
6 |
+
import time
|
7 |
+
|
8 |
+
# Replace these with your own values
|
9 |
+
client_id = ''
|
10 |
+
client_secret = ''
|
11 |
+
user_agent = ''
|
12 |
+
|
13 |
+
# Create a Reddit instance
|
14 |
+
reddit = praw.Reddit(
|
15 |
+
client_id=client_id,
|
16 |
+
client_secret=client_secret,
|
17 |
+
user_agent=user_agent
|
18 |
+
)
|
19 |
+
|
20 |
+
# Create a list of subreddits to collect data from
|
21 |
+
subreddits = ["climate", "energy","renewableenergy","climatechange"]
|
22 |
+
|
23 |
+
# Create an empty list to store the data dictionaries
|
24 |
+
data = []
|
25 |
+
|
26 |
+
# Example of an enhanced backoff strategy
|
27 |
+
def process_request():
|
28 |
+
retry_count = 0
|
29 |
+
max_retries = 5 # You can adjust this based on your needs
|
30 |
+
|
31 |
+
while retry_count < max_retries:
|
32 |
+
try:
|
33 |
+
# Iterate over each subreddit in the subreddits list
|
34 |
+
for subreddit_name in subreddits:
|
35 |
+
# Choose the subreddit you want to interact with
|
36 |
+
subreddit = reddit.subreddit(subreddit_name)
|
37 |
+
|
38 |
+
# Get the top 100 posts in the subreddit
|
39 |
+
top_posts = subreddit.top(limit=10000)
|
40 |
+
count = 0
|
41 |
+
# Iterate over each post in the top_posts list
|
42 |
+
for post in top_posts:
|
43 |
+
count += 1
|
44 |
+
print(count)
|
45 |
+
# Get the title of the post
|
46 |
+
post_title = post.title
|
47 |
+
# Iterate over the first 10 comments in the post
|
48 |
+
for comment in post.comments[:20]:
|
49 |
+
# Get the body of the comment
|
50 |
+
comment_body = comment.body
|
51 |
+
# Get the number of upvotes for the comment
|
52 |
+
upvotes = comment.score
|
53 |
+
data_dict = {
|
54 |
+
"ID": comment.id,
|
55 |
+
"Author": comment.author.name if comment.author else 'N/A',
|
56 |
+
"Subreddit": subreddit_name,
|
57 |
+
"Post Title": post_title,
|
58 |
+
"Comment Body": comment_body,
|
59 |
+
"Timestamp": datetime.utcfromtimestamp(comment.created_utc),
|
60 |
+
"Upvotes": upvotes,
|
61 |
+
"Number of Replies": len(list(comment.replies))
|
62 |
+
}
|
63 |
+
data.append(data_dict)
|
64 |
+
print(data_dict)
|
65 |
+
except praw.exceptions.RedditAPIException as e:
|
66 |
+
if 'ratelimit' in str(e).lower():
|
67 |
+
# If a rate limit error is encountered, wait and then retry
|
68 |
+
retry_count += 1
|
69 |
+
wait_time = 2 ** retry_count # Exponential backoff
|
70 |
+
print(f"Rate limit exceeded. Waiting {wait_time} seconds and retrying...")
|
71 |
+
time.sleep(wait_time)
|
72 |
+
else:
|
73 |
+
# Handle other API exceptions if needed
|
74 |
+
print(f"Error: {e}")
|
75 |
+
# If a rate limit error is encountered, wait and then retry
|
76 |
+
retry_count += 1
|
77 |
+
wait_time = 2 ** retry_count # Exponential backoff
|
78 |
+
print(f"Rate limit exceeded. Waiting {wait_time} seconds and retrying...")
|
79 |
+
time.sleep(wait_time)
|
80 |
+
else:
|
81 |
+
# If the request was successful, break out of the loop
|
82 |
+
break
|
83 |
+
else:
|
84 |
+
# If max_retries is reached, consider logging an error or taking appropriate action
|
85 |
+
print("Max retries reached. Consider adjusting your backoff strategy or rate limits.")
|
86 |
+
process_request()
|
87 |
+
|
88 |
+
# Save the data as a CSV file
|
89 |
+
with open('', mode='w', newline='', encoding='utf-8') as csv_file:
|
90 |
+
fieldnames = ["ID","Author","Subreddit", "Post Title", "Comment Body",
|
91 |
+
"Timestamp","Upvotes","Number of Replies"]
|
92 |
+
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
93 |
+
writer.writeheader()
|
94 |
+
for d in data:
|
95 |
+
writer.writerow(d)
|