import gevent
from gevent import monkey
monkey.patch_all()
import urllib2
import requests, json
import util
HACKERBELL_RECEIVER = 'weiyi@papayamobile.com'
HACKERBELL_SNIPPET =""" 
<h4 style="margin:0;margin-bottom:6px;margin-top:6px">
<a style="font-size:14px;line-height:22px;font-weight:bold;text-decoration:none;color:#259;border:none;outline:none" href="%s" target="_blank">%s</a>&nbsp;&nbsp;</h4>
"""

def fetch_top_ids():
    r = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json')
    top = json.loads(r.content)
    return top

def fetch_new_ids():
    r = requests.get('https://hacker-news.firebaseio.com/v0/newstories.json')
    new = json.loads(r.content)
    return new

def fetch_story(id):
    r = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/item/%s.json' % id).read()
    return json.loads(r)

def notify(title='[Hackerbell]Hacker News Top 10', body='N/A'): 
    util.send_email(title, body, HACKERBELL_RECEIVER)

def wrap(items=None):
    if not items:
        return 'N/A'
    body = ''.join([HACKERBELL_SNIPPET % (i['url'], i['title'] + ' [score: %s]' % i['score']) for i in items])
    return body

def task():
    items = fetch_top(10)
    body = wrap(items)
    notify(body=body)

def task1():
    task_top_key(keys=['python', 'nginx', 'google', 'china', 'facebook'])

def task2():
    task_new_key(keys=['python', 'nginx', 'google', 'china', 'facebook'])

def fetch_top(n=500):
    top = fetch_top_ids()[:n]
    jobs = [gevent.spawn(fetch_story, id) for id in top]
    gevent.wait(jobs)
    return [j.value for j in jobs]

def fetch_new(n=500):
    top = fetch_new_ids()[:n]
    jobs = [gevent.spawn(fetch_story, id) for id in top]
    gevent.wait(jobs)
    return [j.value for j in jobs]
    
def task_top_key(keys=None):
    items = fetch_top()
    items = filter_keys(items, keys)
    body = wrap(items)
    notify(title='[Hackerbell]Top keys', body=body)

def task_new_key(keys=None):
    items = fetch_new()
    items = filter_keys(items, keys)
    body = wrap(items)
    notify(title='[Hackerbell]New keys', body=body)

def filter_keys(items, keys):
    if not keys:
        return items
    keys = set(keys)
    r = [i for i in items if _check(i['title'], keys)]
    return r

def _check(title, keys):
    title =title.lower()
    for k in keys:
        if k in title:
            return True
    return False

@@ -101,16 +101,125 @@
 rt util%0A
+HACKERBELL_KEYS = %5B'python', 'amazon', 'nginx', 'aws', 'china', 'amazon', 'performance', 'language', 'hack'%5D%0A
 HACKERBE
@@ -1372,146 +1372,76 @@
 eys=
-%5B'python', 'nginx', 'google', 'china', 'facebook'%5D)%0A%0Adef task2():%0A    task_new_key(keys=%5B'python', 'nginx', 'google', 'china', 'facebook'%5D
+HACKERBELL_KEYS)%0A%0Adef task2():%0A    task_new_key(keys=HACKERBELL_KEYS
 )%0A%0Ad

fix keys