def run_main(args: Arguments): api = ApiClient(args.subdomain, args.username, args.token) stats = UserStats() topic_id = None posts = api.get_posts(topic_id, filter_from=args.filter_from, filter_to=args.filter_to) for post in posts: if api_util.included_in_date_range(post['created_at'], filter_from=args.filter_from, filter_to=args.filter_to): stats.observe_post_by_user(post['author_id']) comments = api.get_comments(post['id'], filter_from=args.filter_from, filter_to=args.filter_to) for comment in comments: stats.observe_comment_by_user(comment['author_id']) if args.outputfile: with open(args.outputfile, "w") as f: for line in stats.to_csv(): f.write(line) f.write('\n') print(f"Results saved to {args.outputfile}") else: print("--- RESULTS ---") for line in stats.to_csv(): print(line)
def run_main(args: Arguments): api = ApiClient(args.subdomain, args.username, args.token) selected_user_id = None while True: mode = enquiries.choose('Select an action:', ['Get stats', 'Award badge', 'Exit']) print(mode) if mode == 'Exit': return elif mode == 'Award badge': if selected_user_id: badges = api.get_badges() badge_labels = map(lambda b: f"{b['id']} {b['name']}", badges) badge = enquiries.choose('Select a badge:', badge_labels) badge_id = badge.split(' ', 1)[0] print(f"Assigning badge {badge_id} to user {selected_user_id}") response = api.create_badge_assignment(selected_user_id, badge_id) print(str(response)) else: print("First select a user, for example by looking at stats") elif mode == 'Get stats': stats = UserStats() topic_id = None posts = api.get_posts(topic_id, filter_from=args.filter_from, filter_to=args.filter_to) for post in posts: if api_util.included_in_date_range(post['created_at'], filter_from=args.filter_from, filter_to=args.filter_to): stats.observe_post_by_user(post['author_id']) comments = api.get_comments(post['id'], filter_from=args.filter_from, filter_to=args.filter_to) for comment in comments: stats.observe_comment_by_user(comment['author_id']) users = [] for user_id in stats.stats_by_user: userstats = stats.stats_by_user[user_id] user = f"{user_id} - {userstats.get('posts') or 0} posts, {userstats.get('comments') or 0} comments" users.append(user) select_user = enquiries.choose('Here are your top contributors. Select one:', users) selected_user_id = select_user.split(' ', 1)[0]