Created
July 9, 2013 20:31
-
-
Save wmayner/5961012 to your computer and use it in GitHub Desktop.
StackOverflow workflow for Alfred, derived from http://www.alfredforum.com/topic/1524-stackoverflow-workflow/. This version shows when the question was asked in human-readable format instead of just an actual date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, StringIO, gzip, alfred, urllib, urllib2, datetime, time | |
def voiteImg(votes): | |
if votes < 0: | |
imgStr = 'so--1.png' | |
elif votes == 0: | |
imgStr = 'so-0.png' | |
elif votes >0 and votes <10: | |
imgStr = 'so-1-1.png' | |
elif votes >= 10 and votes <50: | |
imgStr = 'so-10.png' | |
elif votes >= 50 and votes <100: | |
imgStr = 'so-50.png' | |
elif votes >= 100 and votes <500: | |
imgStr = 'so-100.png' | |
elif votes >= 500 and votes <1000: | |
imgStr = 'so-500.png' | |
elif votes >= 1000: | |
imgStr = 'so-1000.png' | |
return imgStr | |
theQuery = u'{query}' | |
theQuery = urllib.quote_plus(theQuery) | |
url = 'https://api.stackexchange.com/2.1/search/advanced?order=desc&sort=votes&site=stackoverflow&q=%s' % theQuery | |
request = urllib2.Request(url) | |
request.add_header('Accept-encoding', 'gzip') | |
req_open = urllib2.build_opener() | |
conn = req_open.open(request) | |
req_data = conn.read() | |
data_stream = StringIO.StringIO(req_data) | |
gzip_stream = gzip.GzipFile(fileobj=data_stream) | |
actual_data = gzip_stream.read() | |
data = json.loads(actual_data) | |
results = [] | |
def convertToHumanReadable(date_time): | |
""" | |
converts a python datetime object to the | |
format "X days, Y hours ago" | |
@param date_time: Python datetime object | |
@return: | |
fancy datetime:: string | |
""" | |
current_datetime = datetime.datetime.now() | |
delta = str(current_datetime - date_time) | |
if delta.find(',') > 0: | |
days, hours = delta.split(',') | |
days = int(days.split()[0].strip()) | |
hours, minutes = hours.split(':')[0:2] | |
else: | |
hours, minutes = delta.split(':')[0:2] | |
days = 0 | |
days, hours, minutes = int(days), int(hours), int(minutes) | |
datelets =[] | |
years, months, xdays = None, None, None | |
plural = lambda x: 's' if x!=1 else '' | |
if days >= 365: | |
years = int(days/365) | |
datelets.append('%d year%s' % (years, plural(years))) | |
days = days%365 | |
if days >= 30 and days < 365: | |
months = int(days/30) | |
datelets.append('%d month%s' % (months, plural(months))) | |
days = days%30 | |
if not years and days > 0 and days < 30: | |
xdays =days | |
datelets.append('%d day%s' % (xdays, plural(xdays))) | |
if not (months or years) and hours != 0: | |
datelets.append('%d hour%s' % (hours, plural(hours))) | |
if not (xdays or months or years): | |
datelets.append('%d minute%s' % (minutes, plural(minutes))) | |
return ', '.join(datelets) + ' ago' | |
for q in data['items']: | |
arg = q['link'] | |
time = convertToHumanReadable(datetime.datetime.fromtimestamp(q['creation_date'])) | |
subtitle = "" + str(q['score']) + " votes " + str(q['answer_count']) + " answers "+ str(q['view_count']) + " views Asked " + time | |
img = voiteImg(q['score']) | |
item = alfred.Item({'uid': 1, 'arg': arg}, q['title'], subtitle.encode('utf-8'), (img, {'type': 'png'})) | |
results.append(item) | |
xml = alfred.xml(results) | |
alfred.write(xml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment