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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: unlicense
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
## Top comments from subbreddits
|
| 6 |
+
|
| 7 |
+
I picked the subreddits that were the most popular. I did not pick NSFW but there is probably some NSFW language in here.
|
| 8 |
+
|
| 9 |
+
Here are the subreddits:
|
| 10 |
+
|
| 11 |
+
* AskReddit
|
| 12 |
+
* worldnews
|
| 13 |
+
* todayilearned
|
| 14 |
+
* Music
|
| 15 |
+
* movies
|
| 16 |
+
* science
|
| 17 |
+
* Showerthoughts
|
| 18 |
+
* Jokes
|
| 19 |
+
* space
|
| 20 |
+
* books
|
| 21 |
+
* WritingPrompts
|
| 22 |
+
* tifu
|
| 23 |
+
* wallstreetbets
|
| 24 |
+
* explainlikeimfive
|
| 25 |
+
* askscience
|
| 26 |
+
* history
|
| 27 |
+
* technology
|
| 28 |
+
* relationship_advice
|
| 29 |
+
* relationships
|
| 30 |
+
* Damnthatsinteresting
|
| 31 |
+
* CryptoCurrency
|
| 32 |
+
* television
|
| 33 |
+
* politics
|
| 34 |
+
* Parenting
|
| 35 |
+
* Bitcoin
|
| 36 |
+
* creepy
|
| 37 |
+
* nosleep
|
| 38 |
+
|
| 39 |
+
## Code to download subredditt's
|
| 40 |
+
|
| 41 |
+
dl_subbreddits.sh:
|
| 42 |
+
```
|
| 43 |
+
#!/bin/bash
|
| 44 |
+
#
|
| 45 |
+
# Directions:
|
| 46 |
+
#./dl_reddit_comments.sh submissions
|
| 47 |
+
# or
|
| 48 |
+
#./dl_reddit_comments.sh comments
|
| 49 |
+
|
| 50 |
+
# Check if the argument is provided and valid
|
| 51 |
+
if [ "$#" -ne 1 ] || { [ "$1" != "submissions" ] && [ "$1" != "comments" ]; }; then
|
| 52 |
+
echo "Usage: $0 <submissions|comments>"
|
| 53 |
+
exit 1
|
| 54 |
+
fi
|
| 55 |
+
|
| 56 |
+
# Create the reddit_data folder if it doesn't exist
|
| 57 |
+
mkdir -p reddit_data
|
| 58 |
+
|
| 59 |
+
# Base URL
|
| 60 |
+
base_url="https://the-eye.eu/redarcs/files/"
|
| 61 |
+
|
| 62 |
+
# Array of subreddit names
|
| 63 |
+
subreddits=(
|
| 64 |
+
"AskReddit"
|
| 65 |
+
"worldnews"
|
| 66 |
+
"todayilearned"
|
| 67 |
+
"Music"
|
| 68 |
+
"movies"
|
| 69 |
+
"science"
|
| 70 |
+
"Showerthoughts"
|
| 71 |
+
"Jokes"
|
| 72 |
+
"space"
|
| 73 |
+
"books"
|
| 74 |
+
"WritingPrompts"
|
| 75 |
+
"tifu"
|
| 76 |
+
"wallstreetbets"
|
| 77 |
+
"explainlikeimfive"
|
| 78 |
+
"askscience"
|
| 79 |
+
"history"
|
| 80 |
+
"technology"
|
| 81 |
+
"relationship_advice"
|
| 82 |
+
"relationships"
|
| 83 |
+
"Damnthatsinteresting"
|
| 84 |
+
"CryptoCurrency"
|
| 85 |
+
"television"
|
| 86 |
+
"politics"
|
| 87 |
+
"Parenting"
|
| 88 |
+
"Bitcoin"
|
| 89 |
+
"creepy"
|
| 90 |
+
"nosleep"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Export base_url so it can be used by xargs
|
| 94 |
+
export base_url
|
| 95 |
+
|
| 96 |
+
# Argument to determine whether to download comments or submissions
|
| 97 |
+
type=$1
|
| 98 |
+
|
| 99 |
+
# Generate file names based on the argument
|
| 100 |
+
file_names=()
|
| 101 |
+
for subreddit in "${subreddits[@]}"; do
|
| 102 |
+
file_names+=("${subreddit}_${type}.zst")
|
| 103 |
+
done
|
| 104 |
+
|
| 105 |
+
# Download each file using wget in parallel
|
| 106 |
+
printf "%s\n" "${file_names[@]}" | xargs -n 1 -P 8 -I {} wget -P reddit_data "${base_url}{}"
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
## Code to process for top comments
|
| 110 |
+
|
| 111 |
+
This may need some work. There is some chunking that needed to be done because some of the comment files are very large.
|
| 112 |
+
|
| 113 |
+
AskReddit subbreddit was 50gb of comments so processing that to a csv was a bit painful.
|
| 114 |
+
|
| 115 |
+
```
|
| 116 |
+
import zstandard
|
| 117 |
+
import os
|
| 118 |
+
import json
|
| 119 |
+
import sys
|
| 120 |
+
import csv
|
| 121 |
+
from datetime import datetime
|
| 122 |
+
import logging
|
| 123 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 124 |
+
|
| 125 |
+
log = logging.getLogger("bot")
|
| 126 |
+
log.setLevel(logging.DEBUG)
|
| 127 |
+
log.addHandler(logging.StreamHandler())
|
| 128 |
+
|
| 129 |
+
def read_and_decode(reader, chunk_size, max_window_size, previous_chunk=None, bytes_read=0):
|
| 130 |
+
chunk = reader.read(chunk_size)
|
| 131 |
+
bytes_read += chunk_size
|
| 132 |
+
if previous_chunk is not None:
|
| 133 |
+
chunk = previous_chunk + chunk
|
| 134 |
+
try:
|
| 135 |
+
return chunk.decode()
|
| 136 |
+
except UnicodeDecodeError:
|
| 137 |
+
if bytes_read > max_window_size:
|
| 138 |
+
raise UnicodeError(f"Unable to decode frame after reading {bytes_read:,} bytes")
|
| 139 |
+
log.info(f"Decoding error with {bytes_read:,} bytes, reading another chunk")
|
| 140 |
+
return read_and_decode(reader, chunk_size, max_window_size, chunk, bytes_read)
|
| 141 |
+
|
| 142 |
+
def read_lines_zst(file_name):
|
| 143 |
+
with open(file_name, 'rb') as file_handle:
|
| 144 |
+
buffer = ''
|
| 145 |
+
reader = zstandard.ZstdDecompressor(max_window_size=2**31).stream_reader(file_handle)
|
| 146 |
+
while True:
|
| 147 |
+
chunk = read_and_decode(reader, 2**27, (2**29) * 2)
|
| 148 |
+
if not chunk:
|
| 149 |
+
break
|
| 150 |
+
lines = (buffer + chunk).split("\n")
|
| 151 |
+
for line in lines[:-1]:
|
| 152 |
+
yield line, file_handle.tell()
|
| 153 |
+
buffer = lines[-1]
|
| 154 |
+
reader.close()
|
| 155 |
+
|
| 156 |
+
def process_file(input_file, output_folder):
|
| 157 |
+
output_file_path = os.path.join(output_folder, os.path.splitext(os.path.basename(input_file))[0] + '.csv')
|
| 158 |
+
log.info(f"Processing {input_file} to {output_file_path}")
|
| 159 |
+
|
| 160 |
+
is_submission = "submission" in input_file
|
| 161 |
+
if is_submission:
|
| 162 |
+
fields = ["author", "title", "score", "created", "link", "text", "url"]
|
| 163 |
+
else:
|
| 164 |
+
fields = ["author", "score", "created", "link", "body"]
|
| 165 |
+
|
| 166 |
+
file_size = os.stat(input_file).st_size
|
| 167 |
+
file_lines, bad_lines = 0, 0
|
| 168 |
+
line, created = None, None
|
| 169 |
+
|
| 170 |
+
# Dictionary to store the top comment for each post
|
| 171 |
+
top_comments = {}
|
| 172 |
+
|
| 173 |
+
with open(output_file_path, "w", encoding='utf-8', newline="") as output_file:
|
| 174 |
+
writer = csv.DictWriter(output_file, fieldnames=fields, quoting=csv.QUOTE_MINIMAL, quotechar='"', escapechar='\\')
|
| 175 |
+
writer.writeheader()
|
| 176 |
+
|
| 177 |
+
try:
|
| 178 |
+
for line, file_bytes_processed in read_lines_zst(input_file):
|
| 179 |
+
try:
|
| 180 |
+
obj = json.loads(line)
|
| 181 |
+
if is_submission:
|
| 182 |
+
# Process submission data
|
| 183 |
+
submission = {
|
| 184 |
+
'author': f"u/{obj['author']}",
|
| 185 |
+
'title': obj.get('title', ''),
|
| 186 |
+
'score': obj.get('score', 0),
|
| 187 |
+
'created': datetime.fromtimestamp(int(obj['created_utc'])).strftime("%Y-%m-%d %H:%M"),
|
| 188 |
+
'link': f"https://www.reddit.com/r/{obj['subreddit']}/comments/{obj['id']}/",
|
| 189 |
+
'text': obj.get('selftext', ''),
|
| 190 |
+
'url': obj.get('url', ''),
|
| 191 |
+
}
|
| 192 |
+
writer.writerow(submission)
|
| 193 |
+
else:
|
| 194 |
+
# Process comment data and look for top comments
|
| 195 |
+
post_id = obj['link_id']
|
| 196 |
+
score = obj.get('score', 0)
|
| 197 |
+
body = obj.get('body', '')
|
| 198 |
+
|
| 199 |
+
if "[deleted]" in body or score <= 1:
|
| 200 |
+
continue
|
| 201 |
+
|
| 202 |
+
comment = {
|
| 203 |
+
'author': f"u/{obj['author']}",
|
| 204 |
+
'score': score,
|
| 205 |
+
'created': datetime.fromtimestamp(int(obj['created_utc'])).strftime("%Y-%m-%d %H:%M"),
|
| 206 |
+
'link': f"https://www.reddit.com/r/{obj['subreddit']}/comments/{obj['link_id'][3:]}/_/{obj['id']}/",
|
| 207 |
+
'body': body,
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
if post_id not in top_comments or score > top_comments[post_id]['score']:
|
| 211 |
+
top_comments[post_id] = comment
|
| 212 |
+
writer.writerow(comment)
|
| 213 |
+
|
| 214 |
+
created = datetime.utcfromtimestamp(int(obj['created_utc']))
|
| 215 |
+
except json.JSONDecodeError as err:
|
| 216 |
+
bad_lines += 1
|
| 217 |
+
file_lines += 1
|
| 218 |
+
if file_lines % 100000 == 0:
|
| 219 |
+
log.info(f"{created.strftime('%Y-%m-%d %H:%M:%S')} : {file_lines:,} : {bad_lines:,} : {(file_bytes_processed / file_size) * 100:.0f}%")
|
| 220 |
+
except KeyError as err:
|
| 221 |
+
log.info(f"Object has no key: {err}")
|
| 222 |
+
log.info(line)
|
| 223 |
+
except Exception as err:
|
| 224 |
+
log.info(err)
|
| 225 |
+
log.info(line)
|
| 226 |
+
|
| 227 |
+
log.info(f"Complete : {file_lines:,} : {bad_lines:,}")
|
| 228 |
+
|
| 229 |
+
def convert_to_csv(input_folder, output_folder):
|
| 230 |
+
input_files = []
|
| 231 |
+
for subdir, dirs, files in os.walk(input_folder):
|
| 232 |
+
for filename in files:
|
| 233 |
+
input_path = os.path.join(subdir, filename)
|
| 234 |
+
if input_path.endswith(".zst"):
|
| 235 |
+
input_files.append(input_path)
|
| 236 |
+
|
| 237 |
+
with ProcessPoolExecutor() as executor:
|
| 238 |
+
futures = [executor.submit(process_file, input_file, output_folder) for input_file in input_files]
|
| 239 |
+
for future in futures:
|
| 240 |
+
future.result()
|
| 241 |
+
|
| 242 |
+
if __name__ == "__main__":
|
| 243 |
+
if len(sys.argv) < 3:
|
| 244 |
+
print("Usage: python script.py <input_folder> <output_folder>")
|
| 245 |
+
sys.exit(1)
|
| 246 |
+
input_folder = sys.argv[1]
|
| 247 |
+
output_folder = sys.argv[2]
|
| 248 |
+
convert_to_csv(input_folder, output_folder)
|
| 249 |
+
```
|
| 250 |
+
|
| 251 |
+
## Combining into one dataset
|
| 252 |
+
|
| 253 |
+
Afer finishing, combined into one parquet:
|
| 254 |
+
|
| 255 |
+
```
|
| 256 |
+
import pandas as pd
|
| 257 |
+
import os
|
| 258 |
+
|
| 259 |
+
# Define the folder containing the CSV files
|
| 260 |
+
folder_path = 'csv'
|
| 261 |
+
|
| 262 |
+
# List of files in the folder
|
| 263 |
+
files = os.listdir(folder_path)
|
| 264 |
+
|
| 265 |
+
# Initialize an empty list to store dataframes
|
| 266 |
+
dfs = []
|
| 267 |
+
|
| 268 |
+
# Process each file
|
| 269 |
+
for file in files:
|
| 270 |
+
if file.endswith('.csv'):
|
| 271 |
+
# Extract subreddit name from the file name
|
| 272 |
+
subreddit = file.split('_')[0]
|
| 273 |
+
|
| 274 |
+
# Read the CSV file
|
| 275 |
+
df = pd.read_csv(os.path.join(folder_path, file))
|
| 276 |
+
|
| 277 |
+
# Add the subreddit name as a new column
|
| 278 |
+
df['subreddit'] = subreddit
|
| 279 |
+
|
| 280 |
+
# Keep only the required columns and rename them
|
| 281 |
+
df = df[['title', 'selftext', 'top_comment_body', 'subreddit']]
|
| 282 |
+
df.columns = ['title', 'selftext', 'top_comment', 'subreddit']
|
| 283 |
+
|
| 284 |
+
# Append the dataframe to the list
|
| 285 |
+
dfs.append(df)
|
| 286 |
+
|
| 287 |
+
# Concatenate all dataframes
|
| 288 |
+
combined_df = pd.concat(dfs, ignore_index=True)
|
| 289 |
+
|
| 290 |
+
# Save the combined dataframe to a Parquet file
|
| 291 |
+
combined_df.to_parquet('reddit_top_comments.parquet', index=False)
|
| 292 |
+
```
|