Skip to content

Instantly share code, notes, and snippets.

@Coldsp33d
Created July 31, 2019 05:27
Embed
What would you like to do?
Partially automated comment flagging on Stack Overflow.
import requests
import re
import pandas as pd
import html
import time
import sys
# https://stackapps.com/q/8364/
client_id = '15705'
client_secret = 'iZvNzsXAaSOic6qT*mJMKQ(('
key = 'BTUX)fAFTdhKKhRuF5IKFA(('
def response_has_error(response):
return any('error' in k for k in response.keys())
access_token = ###YOUR ACCESS TOKEN###
df = pd.read_csv('comments.csv')
options_url = ('https://api.stackexchange.com/2.2/comments/{}/flags/options?'
'key={}&access_token={}&site=stackoverflow')
flag_url = 'https://api.stackexchange.com/2.2/comments/{}/flags/add'
MAX_FLAGS = int(sys.argv[1]) if len(sys.argv) > 1 else 95
count = 0
flagged = set()
for t, c in zip(df['Text'], df['Comment Link']):
response = requests.get(options_url.format(c, key, access_token)).json()
if response_has_error(response):
flagged.add(c)
continue
for item in response['items']:
if item['title'] == "It's no longer needed.":
option_id = item['option_id']
response = requests.post(
flag_url.format(c),
data={
'key': key,
'access_token': access_token,
'option_id': option_id,
'site': 'stackoverflow'}).json()
if not response_has_error(response):
count += 1
flagged.add(c)
print(f'Successfully flagged #{count:>3}: {t!r} (CL: {c:>8})')
if count == MAX_FLAGS:
break
time.sleep(5)
print(f'Total flagged: {count}')
df[~df['Comment Link'].isin(flagged)].to_csv('comments.csv', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment