Datasets:
Upload reddit_collect_data.py
Browse files- reddit_collect_data.py +101 -0
reddit_collect_data.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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","climateactionplan","environment","sustainability","zerowaste"]
|
| 22 |
+
data = []
|
| 23 |
+
|
| 24 |
+
# Example of an enhanced backoff strategy
|
| 25 |
+
def process_request():
|
| 26 |
+
retry_count = 0
|
| 27 |
+
max_retries = 5 # You can adjust this based on your needs
|
| 28 |
+
|
| 29 |
+
while retry_count < max_retries:
|
| 30 |
+
try:
|
| 31 |
+
# Iterate over each subreddit in the subreddits list
|
| 32 |
+
for subreddit_name in subreddits:
|
| 33 |
+
# Choose the subreddit you want to interact with
|
| 34 |
+
subreddit = reddit.subreddit(subreddit_name)
|
| 35 |
+
# Get the top 100 posts in the subreddit
|
| 36 |
+
top_posts = subreddit.top(limit=1000)
|
| 37 |
+
count = 0
|
| 38 |
+
# Iterate over each post in the top_posts list
|
| 39 |
+
for post in top_posts:
|
| 40 |
+
count += 1
|
| 41 |
+
print(count)
|
| 42 |
+
# Get the title of the post
|
| 43 |
+
post_id = post.id
|
| 44 |
+
if hasattr(post, 'post_hint') and post.post_hint == 'image':
|
| 45 |
+
image_url = post.url
|
| 46 |
+
else:
|
| 47 |
+
image_url = ""
|
| 48 |
+
for comment in post.comments[:len(list(post.comments))-1]:
|
| 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 |
+
for reply in comment.replies[:len(list(comment.replies))-1]:
|
| 54 |
+
data_dict = {
|
| 55 |
+
"Subreddit": subreddit_name,
|
| 56 |
+
"Post Title": post.title,
|
| 57 |
+
"Post ID": post.id,
|
| 58 |
+
"Post Author": post.author,
|
| 59 |
+
"Post Body": post.selftext,
|
| 60 |
+
"Post Url": post.url,
|
| 61 |
+
"Post Pic": image_url,
|
| 62 |
+
"Post Timestamp": datetime.utcfromtimestamp(post.created_utc),
|
| 63 |
+
"Post Upvotes": post.score,
|
| 64 |
+
"Post Permalink": post.permalink,
|
| 65 |
+
"Comment ID": comment.id,
|
| 66 |
+
"Comment Author": comment.author.name if comment.author else 'N/A',
|
| 67 |
+
"Comment Body": comment_body,
|
| 68 |
+
"Comment Timestamp": datetime.utcfromtimestamp(comment.created_utc),
|
| 69 |
+
"Comment Upvotes": upvotes,
|
| 70 |
+
"Comment Permalink": comment.permalink,
|
| 71 |
+
"Reply ID": reply.id,
|
| 72 |
+
"Reply Author": reply.author,
|
| 73 |
+
"Reply Body": reply.body,
|
| 74 |
+
"Reply Timestamp": datetime.utcfromtimestamp(reply.created_utc),
|
| 75 |
+
"Reply Upvotes": reply.score,
|
| 76 |
+
"Reply Permalink": reply.permalink
|
| 77 |
+
}
|
| 78 |
+
data.append(data_dict)
|
| 79 |
+
print(data_dict)
|
| 80 |
+
except praw.exceptions.RedditAPIException as e:
|
| 81 |
+
if 'ratelimit' in str(e).lower():
|
| 82 |
+
# If a rate limit error is encountered, wait and then retry
|
| 83 |
+
retry_count += 1
|
| 84 |
+
wait_time = 2 ** retry_count # Exponential backoff
|
| 85 |
+
print(f"Rate limit exceeded. Waiting {wait_time} seconds and retrying...")
|
| 86 |
+
time.sleep(wait_time)
|
| 87 |
+
else:
|
| 88 |
+
# Handle other API exceptions if needed
|
| 89 |
+
print(f"Error: {e}")
|
| 90 |
+
# If a rate limit error is encountered, wait and then retry
|
| 91 |
+
retry_count += 1
|
| 92 |
+
wait_time = 2 ** retry_count # Exponential backoff
|
| 93 |
+
print(f"Rate limit exceeded. Waiting {wait_time} seconds and retrying...")
|
| 94 |
+
time.sleep(wait_time)
|
| 95 |
+
else:
|
| 96 |
+
# If the request was successful, break out of the loop
|
| 97 |
+
break
|
| 98 |
+
else:
|
| 99 |
+
# If max_retries is reached, consider logging an error or taking appropriate action
|
| 100 |
+
print("Max retries reached. Consider adjusting your backoff strategy or rate limits.")
|
| 101 |
+
process_request()
|