Commit b1bdf098 authored by Benavides Ontiveros's avatar Benavides Ontiveros
Browse files

add extra fields for data

parent fcb9e09c
...@@ -79,14 +79,36 @@ for subreddit_name in subreddit_list: ...@@ -79,14 +79,36 @@ for subreddit_name in subreddit_list:
except Exception as e: except Exception as e:
logger.warning(f"Comments for post {post.id} not fully loaded: {e}") logger.warning(f"Comments for post {post.id} not fully loaded: {e}")
# --- Vote / polarization calculations ---
score = post.score
upvote_ratio = post.upvote_ratio
estimated_upvotes = None
estimated_downvotes = None
vote_polarization = None
if upvote_ratio is not None and upvote_ratio != 0.5:
try:
total_votes = score / (2 * upvote_ratio - 1)
estimated_upvotes = int(total_votes * upvote_ratio)
estimated_downvotes = int(total_votes * (1 - upvote_ratio))
vote_polarization = abs(0.5 - upvote_ratio) * 2
except ZeroDivisionError:
pass
try: try:
post_data = { post_data = {
"id": post.id, "id": post.id,
"title": post.title, "title": post.title,
"score": post.score, "score": score,
"upvote_ratio": upvote_ratio,
"estimated_upvotes": estimated_upvotes,
"estimated_downvotes": estimated_downvotes,
"vote_polarization": vote_polarization,
"url": post.url, "url": post.url,
"num_comments": post.num_comments, "num_comments": post.num_comments,
"created_utc": post.created_utc, "created_utc": post.created_utc,
"created_iso": datetime.fromtimestamp(post.created_utc, timezone.utc).isoformat(),
"comments": [] "comments": []
} }
except Exception as e: except Exception as e:
...@@ -106,7 +128,8 @@ for subreddit_name in subreddit_list: ...@@ -106,7 +128,8 @@ for subreddit_name in subreddit_list:
"id": comment.id, "id": comment.id,
"body": comment.body, "body": comment.body,
"score": comment.score, "score": comment.score,
"created_utc": comment.created_utc "created_utc": comment.created_utc,
"created_iso": datetime.fromtimestamp(comment.created_utc, timezone.utc).isoformat()
}) })
except Exception as e: except Exception as e:
logger.warning(f"Failed to parse a comment in post {post.id}: {e}") logger.warning(f"Failed to parse a comment in post {post.id}: {e}")
...@@ -121,6 +144,7 @@ for subreddit_name in subreddit_list: ...@@ -121,6 +144,7 @@ for subreddit_name in subreddit_list:
# --- Generate Unix timestamp in milliseconds for unique file names --- # --- Generate Unix timestamp in milliseconds for unique file names ---
timestamp_ms = int(datetime.now(timezone.utc).timestamp() * 1000) timestamp_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
timestamp_iso = datetime.now(timezone.utc).isoformat()
# Output data file # Output data file
data_filename = os.path.join(OUTPUT_DIR, f"reddit_{subreddit_name}_{timestamp_ms}.txt") data_filename = os.path.join(OUTPUT_DIR, f"reddit_{subreddit_name}_{timestamp_ms}.txt")
...@@ -140,7 +164,8 @@ for subreddit_name in subreddit_list: ...@@ -140,7 +164,8 @@ for subreddit_name in subreddit_list:
"language": "en", "language": "en",
"classifications": [subreddit_name], "classifications": [subreddit_name],
"properties": { "properties": {
"creation_date": timestamp_ms "creation_date": timestamp_ms,
"creation_date_iso": timestamp_iso
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment