Coverage for src/botadi/mokadi/mokadi_action_news.py: 67%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

58 statements  

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Defines an action for Mokadi. 

5""" 

6from .mokadi_action import MokadiAction 

7from .mokadi_info import MokadiInfo 

8from .mokadi_exceptions import MokadiException 

9from .cognitive_services_helper import call_api_news 

10from .mokadi_helper import convert_into_days 

11 

12 

13class MokadiActionNews(MokadiAction): 

14 """ 

15 Action. News. 

16 """ 

17 

18 def __init__(self, subkey, fLOG=None): 

19 """ 

20 Constructor. 

21 

22 @param subkey subscription key 

23 @param fLOG logging function 

24 """ 

25 MokadiAction.__init__(self, fLOG=fLOG) 

26 self._subkey = subkey 

27 

28 def can_do(self, interpreted, message): 

29 """ 

30 Tells if the class can process the message. 

31 

32 @param interpreted interpreted message 

33 @param message message 

34 @return true if the class can process the message 

35 """ 

36 if len(interpreted) < 2: 

37 return False 

38 word = interpreted[1] 

39 for word in interpreted: 

40 if word[1] == ":news:": 

41 return True 

42 return False 

43 

44 def process_interpreted_message(self, interpretation, message): 

45 """ 

46 Process the interpreted message. 

47 

48 @param interpretation interpretation 

49 @param message original message 

50 @return iterator on Info 

51 """ 

52 done = False 

53 query = "NOT SET" 

54 

55 # We assume 

56 has_news = any(_ for _ in interpretation if _[1] == ":news:") 

57 has_query = any(_ for _ in interpretation if _[1] == ":apropos:") 

58 if has_news: 

59 if has_query: 

60 lasti = None 

61 for i in range(len(interpretation)): 

62 lasti = i 

63 if interpretation[i][1] == ":apropos:": 

64 break 

65 if lasti == len(interpretation): 

66 yield MokadiInfo("error", "", "Je n'ai pas compris ce qu'il fallait chercher.") 

67 done = True 

68 query = None 

69 else: 

70 query = " ".join(_[0] 

71 for _ in interpretation[lasti + 1:-1]) 

72 else: 

73 query = "" 

74 

75 if query is not None: 

76 res = call_api_news(self._subkey, query) 

77 if len(res) == 0: 

78 yield MokadiInfo("ok", "Aucun nouvelle n'a été trouvée à ce sujet.") 

79 done = True 

80 if 'value' not in res or len(res["value"]) == 0: 

81 yield MokadiInfo("ok", "La recherche '{0}' a abouti mais aucun résultat n'a été trouvé.".format(query)) 

82 done = True 

83 else: 

84 displayed = 0 

85 for val in res["value"]: 

86 # Example of a news 

87 # {'_type': 'News', 

88 # 'readLink': 'https://api.cognitive.microsoft.com/api/v5/news/search?q=', 

89 # 'value': [{'name': "Colombie : le bilan de la coulꥠde boue s'alourdit ࠲54 morts", 

90 # 'url': 'http://www.bing.com/cr? 

91 # IG=CEFF9C63FD7A4439B591261606484860&CID=3B75F5FD9A146DBF103CFFA49BF36C83&rd=1&h= 

92 # Pu7c9WbgY9iMPFtyYtszuhDNymO92hG1vbCqLp0qUHw&v=1&r=http%3a%2f%2fwww.lepoint.fr%2fmonde 

93 # %2fcolombie-le-bilan-de-la-coulee-de-boue- 

94 # s-alourdit-a-254-morts-03-04-2017-2116711_24.php&p=DevEx,5019.1', 

95 # 'image': {'thumbnail': {'contentUrl': 

96 # 'https://www.bing.com/th?id=ON.08357B50C029DBC09D053826F9B22658&pid=News', 

97 # 'width': 700, 'height': 304}}, 

98 # 'datePublished': '2017-04-03T10:33:00', 

99 title = val["name"] 

100 url = val["url"] 

101 date = convert_into_days(val['datePublished']) 

102 snippet = val.get("url", None) 

103 yield MokadiInfo("ok", date + ". " + title, url=url, image=snippet) 

104 displayed += 1 

105 if displayed == 0: 

106 yield MokadiInfo("ok", "La recherche '{0}' a abouti mais les résultats sont inattendus.".format(query)) 

107 done = True 

108 

109 if not done: 

110 args = [message, len(interpretation), 

111 interpretation, has_news, has_query, query] 

112 raise MokadiException( 

113 "Unable to interpret '{0}'\n{1} - {2}\nhas_news={3} has_query={4} query={5}.".format(*args))