class Activity: def __init__(self, reddit: Reddit): self.reddit = reddit self.api = PushshiftAPI(reddit) def combined(self, username: str, limit: int = 5) -> Counter: activity = self.api.redditor_subreddit_activity(username) counts = activity.get("comment", Counter([])) counts.update(activity.get("submission", Counter([]))) return counts.most_common(limit) def combined_formatted(self, username: str, limit: int = 5) -> str: data = self.combined(username, limit) output = [ f"Here are the top 5 most active subreddits for /u/{username}:", "", "Subreddit | Total activity", "---|:---:", ] try: output.extend([f"/r/{subreddit} | {count:,}" for subreddit, count in data]) except ValueError: output.extend([f"/r/{subreddit} | {count}" for subreddit, count in data]) output.extend( [" ", "> I am a bot, this action was performed automatically"] ) return "\n".join(output)
df = pd.read_csv(file_name, sep='\t', header=None, names=cols) for index, row in df.iterrows(): all_comments_count = 0 aita_comments_count = 0 all_submissions_count = 0 aita_submissions_count = 0 author = row['author'] created_ts = row['created_ts'] if author and created_ts and not pd.isnull(author) and not pd.isnull( created_ts): submission_date_epoch_timestamp = arrow.get(created_ts).timestamp user_activity_profile = api.redditor_subreddit_activity( author, before=submission_date_epoch_timestamp) if 'comment' in user_activity_profile: all_comments_count = sum( user_activity_profile['comment'].values()) aita_comments_count = user_activity_profile['comment'][ 'amitheasshole'] if 'submission' in user_activity_profile: all_submissions_count = sum( user_activity_profile['submission'].values()) aita_submissions_count = user_activity_profile['submission'][ 'amitheasshole'] df.loc[index, 'all_comments_count'] = int(all_comments_count) df.loc[index, 'all_submissions_count'] = int(all_submissions_count)