Skip to content

Instantly share code, notes, and snippets.

@jeasinema
Created June 13, 2018 12:30
Show Gist options
  • Save jeasinema/b7688abc7a86c9a2e1d251767abef24c to your computer and use it in GitHub Desktop.
Save jeasinema/b7688abc7a86c9a2e1d251767abef24c to your computer and use it in GitHub Desktop.
Core script of an alfred workflow crawling news from 163.com
#! /usr/bin/env python
# coding=utf-8
__author__ = 'kingson_jeasinema'
import sys
import alfred
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
}
topic = {
'headline': ('headline/T1348647853363',),
'sport': ('list/T1348649079062',),
'finance': ('list/T1348648756099',),
'gossip': ('list/T1348648517839',),
'keji': ('list/T1348649580692',),
'beijing': ('local/5YyX5Lqs',u'北京'),
# 'soccer': ('list/T1399700447917',)
'movie': ('list/T1348648650048',),
'car': ('list/T1348654060988',),
'joke': ('list/T1350383429665',),
'game': ('list/T1348654151579',),
'featured': ('list/T1370583240249',),
'radio': ('list/T1379038288239',),
'nba': ('list/T1348649145984',),
'electronics': ('list/T1348649776727',),
'mobile': ('list/T1351233117091',),
'education': ('list/T1348654225495',),
# 'forum': ('list/T1349837670307',),
'travel': ('list/T1348654204705',),
'phone': ('list/T1348649654285',),
'blog': ('list/T1349837698345',),
'society': ('list/T1348648037603',),
'message': ('list/T1371543208049',),
'mil': ('list/T1348648141035',),
}
def set_cache(query):
"""
获取头条新闻并缓存
"""
if query in list(topic.keys()):
pid = topic[query][0].split('/')[-1] if len(topic[query]) == 1 else topic[query][1]
result = requests.get('http://c.m.163.com/nc/article/{}/0-20.html'.format(topic[query][0]), headers=headers).json()
response = result[pid]
cache1 = []
for i in range(len(response)):
if 'url_3w' in response[i].keys():
cache1.append(dict(title=response[i]['title'], digest=response[i]['digest'],
url=response[i]['url_3w']))
alfred.cache.set('{}.list'.format(query), cache1, expire=1)
def get_cache(query):
"""
获取缓存信息
"""
if query in list(topic.keys()):
if alfred.cache.timeout('{}.list'.format(query)) == -1:
set_cache(query)
return alfred.cache.get('{}.list'.format(query))
def output(query):
"""
返回结果给Alfred
"""
workflows = get_cache(query)
workflows = [w for w in workflows if query]
feedback = alfred.Feedback()
for w in workflows:
feedback.addItem(
title=w['title'],
subtitle=w['digest'],
arg=w['url']
)
feedback.output()
if __name__ == '__main__':
output(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment