-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect_dadjokes.py
More file actions
70 lines (58 loc) · 2.28 KB
/
collect_dadjokes.py
File metadata and controls
70 lines (58 loc) · 2.28 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
import os
import time
import datetime
import praw # reddit api
import pandas as pd
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Initialize the Reddit client
reddit = praw.Reddit(
client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
user_agent="dad_joke_generator_app"
)
subreddit = reddit.subreddit("dadjokes")
post_data = []
after = None
# Determine the path to the 'data' folder which is one level above the current script directory.
data_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data")
if not os.path.exists(data_folder):
os.makedirs(data_folder)
csv_path = os.path.join(data_folder, "dadjokes_partial_data.csv")
try:
while True:
try:
# Fetch the next batch of posts using the 'after' parameter for pagination.
top_posts = list(subreddit.top(time_filter="all", limit=250, params={"after": after}))
except Exception as e:
print(f"Error fetching posts: {e}")
break # Exit the loop if there's an error fetching posts
if not top_posts:
print("No more posts available.")
break
for post in top_posts:
post_date = datetime.datetime.fromtimestamp(post.created_utc).strftime('%Y-%m-%d %H:%M:%S')
post_data.append({
"id": post.id,
"upvotes": post.ups,
"title": post.title,
"selftext": post.selftext,
"date": post_date
})
# Update 'after' with the fullname of the last post to paginate.
after = top_posts[-1].fullname
# Save the current batch to CSV in the data folder.
df_batch = pd.DataFrame(post_data)
df_batch.to_csv(csv_path, index=False)
print(f"Saved partial data to {csv_path}. Waiting 5 minutes before next batch...")
time.sleep(300) # Wait 5 minutes before fetching the next batch
except KeyboardInterrupt:
print("Process interrupted by user (KeyboardInterrupt).")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Save whatever data has been collected so far.
df = pd.DataFrame(post_data)
df.to_csv(csv_path, index=False)
print(f"Data saved to {csv_path}")