-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit_scraper.py
More file actions
323 lines (264 loc) · 12 KB
/
Copy pathreddit_scraper.py
File metadata and controls
323 lines (264 loc) · 12 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
Scrapes TARGET_AI_SUBREDDITS and TARGET_REAL_SUBREDDITS, saves images,
and records metadata for each image in a CSV for later parquet packaging.
AI subreddits -> label: "fake"
Real subreddits -> label: "real"
Default mode: resumes from the last scraped post per subreddit (reads reddit_metadata.csv).
If a subreddit has never been scraped, goes back MAX_DAYS_AGO days.
Override with --days-ago N to force a fixed lookback for all subreddits.
"""
import praw, os, requests, csv, fcntl, argparse
from datetime import datetime, timezone, timedelta
from collections import defaultdict
import pandas as pd
from dotenv import load_dotenv
import subprocess
import sys
import cv2
load_dotenv()
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
MAX_DAYS_AGO = 1000
TARGET_AI_SUBREDDITS = [
"aigeneratedart", "aiArt", "midjourney", "aiimages", "AiArtwork", "AiGeneratedArt",
"Pro_Ai_Art", "AI_ART", "aivideo", "AIVideos_SFW", "GenAIGallery", "deepdream",
"nanobananaSFW", "VEO3", "Seedance_A",
"KlingAI_Videos", "GrokImage",
]
TARGET_REAL_SUBREDDITS = [
"photography", "itookapicture", "pics", "EarthPorn", "CityPorn",
"FoodPorn", "StreetPhotography", "analog", "photocritique",
"WildlifePhotography", "MacroPorn", "SkyPorn", "portraitphotography", "MODELING",
"AmateurPhotography" , "portraitphotography", "portraits", "casual_photography",
"portraitphotos"
]
REDDIT_STAGING_DIR = "/home/aandrey/links/scratch/data/reddit_images"
METADATA_CSV = os.path.join(REDDIT_STAGING_DIR, "reddit_metadata.csv")
METADATA_FIELDS = ["filename", "label", "subreddit", "post_date", "reddit_id", "packaged"]
NUM_OF_VIDEO_FRAMES = 15
TODAY = datetime.now().strftime("%Y-%m-%d")
# ---------------------------------------------------------------------------
# Args
# ---------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--days-ago", type=int, default=None,
help="Force a fixed lookback in days for all subreddits. "
"Default: resume from last scraped post per subreddit."
)
return parser.parse_args()
# ---------------------------------------------------------------------------
# Resume logic
# ---------------------------------------------------------------------------
def get_last_scraped_dates():
"""
Read reddit_metadata.csv and return the most recent post_date per subreddit.
Returns dict: subreddit (lowercase) -> datetime (UTC).
"""
last_dates = {}
if not os.path.exists(METADATA_CSV):
return last_dates
with open(METADATA_CSV, "r") as f:
reader = csv.DictReader(f)
for row in reader:
sub = row["subreddit"].lower()
try:
post_date = datetime.strptime(row["post_date"], "%Y-%m-%d").replace(tzinfo=timezone.utc)
if sub not in last_dates or post_date > last_dates[sub]:
last_dates[sub] = post_date
except (ValueError, KeyError):
continue
return last_dates
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def append_metadata(row_dict):
"""Append a metadata row to the shared CSV with file locking."""
file_exists = os.path.exists(METADATA_CSV) and os.path.getsize(METADATA_CSV) > 0
with open(METADATA_CSV, "a", newline="") as f:
fcntl.flock(f, fcntl.LOCK_EX)
writer = csv.DictWriter(f, fieldnames=METADATA_FIELDS)
if not file_exists:
writer.writeheader()
writer.writerow(row_dict)
fcntl.flock(f, fcntl.LOCK_UN)
def download_file(url, filepath):
try:
response = requests.get(url, stream=True, timeout=10)
if response.status_code == 200:
with open(filepath, "wb") as f:
for chunk in response.iter_content(1024):
f.write(chunk)
return True
return False
except:
return False
def extract_video_frames(video_path, output_dir, base_filename, num_frames=NUM_OF_VIDEO_FRAMES):
"""Extracts a fixed number of frames evenly distributed across a video."""
cap = cv2.VideoCapture(video_path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
saved_frames = []
if total_frames > 0:
step_size = max(1, total_frames // num_frames)
for i in range(num_frames):
frame_idx = i * step_size
if frame_idx >= total_frames:
frame_idx = total_frames - 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
ret, frame = cap.read()
if ret:
frame_filename = f"{base_filename}_frame_{i}.jpg"
frame_path = os.path.join(output_dir, frame_filename)
cv2.imwrite(frame_path, frame)
saved_frames.append(frame_filename)
print(f"Saved Video Frame: {frame_filename}")
cap.release()
return saved_frames
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def run_reddit_scraper(days_ago=None):
args_parsed = parse_args()
if days_ago is None:
days_ago = args_parsed.days_ago
credsfile = "data/creds/creds.csv"
cdf = pd.read_csv(credsfile, sep=",")
creds = cdf[(cdf["datatype"] == "submissions")].to_dict(orient="records")[0]
reddit = praw.Reddit(
client_id=creds["client_id"],
client_secret=creds["client_secret"],
user_agent=creds["useragent"],
)
os.makedirs(REDDIT_STAGING_DIR, exist_ok=True)
# Determine cutoffs
if days_ago is not None:
# Fixed lookback mode
global_cutoff = (datetime.now(timezone.utc) - timedelta(days=days_ago)).timestamp()
per_sub_cutoffs = None
print(f"Fixed lookback mode: scraping last {days_ago} days for all subreddits.")
else:
# Resume mode: per-subreddit cutoffs from metadata
global_cutoff = (datetime.now(timezone.utc) - timedelta(days=MAX_DAYS_AGO)).timestamp()
last_dates = get_last_scraped_dates()
per_sub_cutoffs = {}
for sub in [s.lower() for s in TARGET_AI_SUBREDDITS + TARGET_REAL_SUBREDDITS]:
if sub in last_dates:
per_sub_cutoffs[sub] = last_dates[sub].timestamp()
else:
per_sub_cutoffs[sub] = global_cutoff
resumed = sum(1 for s in per_sub_cutoffs if s in last_dates)
fresh = len(per_sub_cutoffs) - resumed
print(f"Resume mode: {resumed} subreddits resuming from last post, {fresh} starting from {MAX_DAYS_AGO} days ago.")
# Load existing filenames to skip duplicates
existing_files = set()
if os.path.exists(METADATA_CSV):
with open(METADATA_CSV, "r") as f:
reader = csv.DictReader(f)
for row in reader:
existing_files.add(row["filename"])
print(f"Existing files in metadata: {len(existing_files)}")
# Build combined list with labels
subreddit_list = [(sub, "fake") for sub in TARGET_AI_SUBREDDITS] + \
[(sub, "real") for sub in TARGET_REAL_SUBREDDITS]
failed_subreddits = []
for sub_name, label in subreddit_list:
try:
sub_lower = sub_name.lower()
if per_sub_cutoffs is not None:
cutoff = per_sub_cutoffs[sub_lower]
cutoff_date = datetime.fromtimestamp(cutoff, tz=timezone.utc).strftime("%Y-%m-%d")
print(f"Scanning r/{sub_name} (label={label}, resuming from {cutoff_date})...")
else:
cutoff = global_cutoff
print(f"Scanning r/{sub_name} (label={label})...")
new_count = 0
skipped_count = 0
for submission in reddit.subreddit(sub_name).new(limit=None):
if submission.created_utc < cutoff:
break
date_str = datetime.fromtimestamp(
submission.created_utc, tz=timezone.utc
).strftime("%Y-%m-%d")
base_name = f"reddit_{sub_lower}_{date_str}_{submission.id}"
url = str(submission.url).lower()
# Handle images
if url.endswith((".jpg", ".jpeg", ".png")):
file_extension = os.path.splitext(submission.url)[1]
file_name = f"{base_name}{file_extension}"
if file_name in existing_files:
skipped_count += 1
continue
filepath = os.path.join(REDDIT_STAGING_DIR, file_name)
if download_file(submission.url, filepath):
new_count += 1
append_metadata({
"filename": file_name,
"label": label,
"subreddit": sub_lower,
"post_date": date_str,
"reddit_id": submission.id,
"packaged": False,
})
else:
print(f"Download failed at {submission.url}")
# Handle videos
elif submission.is_video and submission.media and "reddit_video" in submission.media:
video_url = submission.media["reddit_video"]["fallback_url"]
frame_check = f"{base_name}_frame_0.jpg"
if frame_check in existing_files:
skipped_count += 1
continue
video_filepath = os.path.join(REDDIT_STAGING_DIR, f"{base_name}_temp.mp4")
if download_file(video_url, video_filepath):
frame_filenames = extract_video_frames(
video_filepath, REDDIT_STAGING_DIR, base_name, num_frames=NUM_OF_VIDEO_FRAMES
)
for frame_filename in frame_filenames:
new_count += 1
append_metadata({
"filename": frame_filename,
"label": label,
"subreddit": sub_lower,
"post_date": date_str,
"reddit_id": submission.id,
"packaged": False,
})
if os.path.exists(video_filepath):
os.remove(video_filepath)
else:
print(f"Video download failed at {video_url}")
print(f" r/{sub_name}: {new_count} new, {skipped_count} skipped (already scraped)")
except Exception as e:
print(f"Error scanning r/{sub_name}: {e}. Skipping.", flush=True)
failed_subreddits.append((sub_name, str(e)))
continue
if failed_subreddits:
print(f"\n{'='*60}", flush=True)
print(f"WARNING: {len(failed_subreddits)} subreddits failed:", flush=True)
for sub, err in failed_subreddits:
print(f" r/{sub}: {err}", flush=True)
print(f"Consider removing these from the target list.", flush=True)
print(f"{'='*60}", flush=True)
print("Submitted NSFW image filter request")
process = subprocess.run(
[
"sbatch",
"--wait",
f"--output=data/slurm_logs/{TODAY}/reddit_filtering-%x_%j.out",
"submit_nsfw_filtering.sh",
REDDIT_STAGING_DIR,
],
capture_output=True,
text=True,
)
print("job submitted. Filtering images...")
print("see slurm logs dir to see status")
if process.returncode == 0:
print(f"Filtering successful.")
else:
print(f"ERROR: Filtering failed")
print(f"Slurm Error Details: {process.stderr}")
if __name__ == "__main__":
run_reddit_scraper()