Skip to content

Instantly share code, notes, and snippets.

@badp
Created March 5, 2011 22:34
Show Gist options
  • Select an option

  • Save badp/856790 to your computer and use it in GitHub Desktop.

Select an option

Save badp/856790 to your computer and use it in GitHub Desktop.
import pyso, random, mechanize, time
#our targets
bad_tags = ["strategy", "tips", "how-to"]
#install site
site = pyso.APISite("api.gaming.stackexchange.com", "1.1")
pyso.install_site(site)
#determine if our latest removal request has been handled. After a day,
#assume it's been rejected.
previous_state = eval(open("last-state", "r").read())
if previous_state["timestamp"] + 86400 < time.time():
last_question = pyso.get_question(previous_state["question_id"])
for tag in last_question["tags"]:
if tag in bad_tags:
print("Bailing out - latest suggestion not yet approved")
exit()
#determine if we should run just yet: our last edit must have dropped
#five items down in the homepage.
top_questions_on_homepage = pyso.get_all_questions(order_by="activity")
for i in range(5):
question = top_questions_on_homepage.next()
#was it us that bumped the question? Check the last two events,
#just in case the editor improved the suggestion.
for i in range(2):
event = pyso.get_questions_timeline(str(question["question_id"]),
start_date = question["last_activity_date"]).next()
#well, it was an anonymous edit. Bail.
if event['user']['user_id'] == -1 && event['action'] == 'revised':
print("Bailing out - latest edit too up in the homepage.")
exit()
#okay, then. Let's find the next question with that evil tag.
bad_tag = random.choice(bad_tag)
try:
purge_this_question = pyso.get_all_questions(tags = [bad_tag]]).next()
except StopIteration:
print("Bailing out - no more questions tagged %s" % bad_tag)
exit()
question_id = purge_this_question["question_id"]
new_tags = [tag for tag in purge_this_question["tags"] if tag not in bad_tags]
#suggest the edit
browser = mechanize.Browser()
browser.set_handle_robots(False) #:/
browser.open("http://gaming.stackexchange.com/posts/%d/edit" % question_id)
browser.select_form(nr = 1) #:\
browser["tagnames"] = " ".join(new_tags)
browser["edit-comment"] = "badp's killWithFire.py tag-murderer-o-matic suggests applying fire to these horrible tags."
browser.submit()
#deed is done, now remember this question ID
open("last-state", "w").write(repr({"question_id": question_id, "timestamp": time.time()}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment