Skip to content

Instantly share code, notes, and snippets.

@klenwell
Created July 20, 2014 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klenwell/16dd32f4438c53042b6e to your computer and use it in GitHub Desktop.
Save klenwell/16dd32f4438c53042b6e to your computer and use it in GitHub Desktop.
Python script to search Stack Overflow by tags for questions still worth answering using API
"""
Search Stack Overflow by tags for questions still worth answering
References:
search API: http://api.stackexchange.com/docs/advanced-search
"""
import stackexchange
import pdb
from datetime import datetime, timedelta
import time
import json
#
# To get API key: https://stackapps.com/
#
API_KEY = 'PASTE HERE'
def search(tag='python', exclude='.net'):
questions = []
results = []
interesting = []
titles = []
HOURS_FROM = 24
HOURS_TO = 4
MAX_ANSWERS = 1
MIN_OWNER_KARMA = 20
fromdate = int(time.mktime((datetime.now() - timedelta(hours=HOURS_FROM)).timetuple()))
todate = int(time.mktime((datetime.now() - timedelta(hours=HOURS_TO)).timetuple()))
questions = so.search(
accepted=False,
closed=False,
fromdate=fromdate,
todate=todate,
tagged=tag,
nottagged=exclude)
for question in questions:
title = question.json.get('title')
if title in titles:
continue
else:
titles.append(title)
owner = question.json.get('owner', {})
result = dict(
question=title,
url=question.json.get('link'),
owner_karma=owner.get('reputation', 0),
owner_accept= owner.get('accept_rate', 0),
answers=question.json.get('answer_count', 999),
votes=question.json.get('score', -1),
tags=question.json.get('tags'),
createdat=question.creation_date
)
results.append(result)
if (result['owner_karma'] > MIN_OWNER_KARMA) and (result['answers'] <= MAX_ANSWERS) and (
result['votes'] >= 0) and (result['owner_accept'] > 50):
interesting.append(result)
print("search returned %d results" % (len(results)))
print("search returned %d interesting results" % (len(interesting)))
return {
'all': results,
'interesting': interesting
}
def interesting(tag='kineticjs', exclude='.net'):
results = search(tag, exclude)
return iter(results['interesting'])
USAGE = """
USAGE:
(Pdb) results = search('tag1;tag2')
(Pdb) len(results['all'])
(Pdb) len(results['interesting'])
(Pdb) results = interesting('tag1;tag2')
(Pdb) results.next()
SAMPLE TAGS:
ajax, jquery, python, javascript, php
"""
if __name__ == '__main__':
so = stackexchange.Site(stackexchange.StackOverflow, API_KEY)
print(USAGE)
pdb.set_trace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment