-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews.py
39 lines (33 loc) · 1.25 KB
/
News.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8-*-
import logging, json
from src.plugins import is_all_word_segment_in_text, plugin_output
import requests, sys
from src.config import load_yaml_settings
WORDS = ['今日头条']
PRIORITY = 12
logger = logging.getLogger()
def handle(text, mic, profile, iot_client=None,chatbot=None):
host = 'http://toutiao-ali.juheapi.com'
path = '/toutiao/index'
method = 'GET'
appcode = load_yaml_settings()['aliyun']['api_market']['appcode']
querys = 'type=top'
bodys = {}
url = host + path + '?' + querys
headers = {
'Authorization': 'APPCODE ' + appcode,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
r = requests.request(method=method, url=url, data=bodys, headers=headers)
data = json.loads(r.content.decode('utf8'))
if data['result']['stat'] == '1':
news = []
news_ding = []
for item in data['result']['data']:
news.append(item['title'])
news_ding.append('['+item['title']+']('+item['url']+') ')
plugin_output(text, mic, robot_says='。\n'.join(news), force_ding=True, ding_content=' | '.join(news_ding))
else:
mic.say('接口出错')
def is_valid(text):
return is_all_word_segment_in_text(WORDS, text)