Coverage for src/botadi/mokadi/cognitive_services_helper.py: 80%

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

56 statements  

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

2""" 

3@file 

4@brief Call to Cognitive Services. 

5""" 

6import http.client 

7import urllib.request 

8import urllib.parse 

9import urllib.error 

10import json 

11from .mokadi_exceptions import CognitiveException 

12 

13 

14def bytes2python(answer): 

15 """ 

16 Converts :epkg:`JSON` bytes into a dictionary. 

17 

18 @param answer bytes 

19 @return any type 

20 """ 

21 string = answer.decode('utf-8') 

22 json_obj = json.loads(string) 

23 return json_obj 

24 

25 

26def call_api_news(subscription_key, query, market="fr-FR", count=10, offset=0): 

27 """ 

28 Retrieves resuls for the news API. 

29 

30 @param subscription_key 

31 @param query query 

32 @param market market 

33 @param count number of results 

34 @param offset skip results 

35 @return results 

36 

37 Example of a result: 

38 

39 :: 

40 

41 { 

42 "_type": "Images", 

43 "instrumentation": {}, 

44 "readLink": "https://api.cognitive.microsoft.com/api/v7/images/search?q=puppies", 

45 "webSearchUrl": "https://www.bing.com/images/search?q=puppies&FORM=OIIARP", 

46 "totalEstimatedMatches": 955, 

47 "nextOffset": 1, 

48 "value": [ 

49 { 

50 "webSearchUrl": "https://www.bing.com/images/search?view=detailv2&FORM=...", 

51 "name": "So cute - Puppies Wallpaper (14749028) - Fanpop", 

52 "thumbnailUrl": "https://tse3.mm.bing.net/th?id=OIP.jHrihoDNkXGS1t5e89jNfwEsDh&pid=Api", 

53 "datePublished": "2014-02-01T21:55:00.0000000Z", 

54 "contentUrl": "http://images4.fanpop.com/image/photos/14700000/So-cute-puppies-14749028-1600-1200.jpg", 

55 "hostPageUrl": "http://www.fanpop.com/clubs/puppies/images/14749028/title/cute-wallpaper", 

56 "contentSize": "394455 B", 

57 "encodingFormat": "jpeg", 

58 "hostPageDisplayUrl": "www.fanpop.com/clubs/puppies/images/14749028/title/cute-wallpaper", 

59 "width": 1600, 

60 "height": 1200, 

61 "thumbnail": { 

62 "width": 300, 

63 "height": 225 

64 }, 

65 "imageInsightsToken": "ccid_jHrihoDN*mid_F68CC526226E163FD1EA659747...", 

66 "insightsMetadata": { 

67 "recipeSourcesCount": 0 

68 }, 

69 "imageId": "F68CC526226E163FD1EA659747ADCB8F9FA3CD96", 

70 "accentColor": "8D613E" 

71 } 

72 ], 

73 "queryExpansions": [ 

74 { 

75 "text": "Shih Tzu Puppies", 

76 "displayText": "Shih Tzu", 

77 "webSearchUrl": "https://www.bing.com/images/search?q=Shih+Tzu+Puppies&tq=%7b%22pq...", 

78 "searchLink": "https://api.cognitive.microsoft.com/api/v7/images/search?q=S22%2c%...", 

79 "thumbnail": { 

80 "thumbnailUrl": "https://tse2.mm.bing.net/th?q=Shih+Tzu+Puppies&pid=Api&mkt=en-US&adlt=moderate&t=1" 

81 } 

82 } 

83 ], 

84 ... 

85 """ 

86 headers = { 

87 # Request headers 

88 'Ocp-Apim-Subscription-Key': subscription_key, 

89 } 

90 

91 params = urllib.parse.urlencode({ 

92 # Request parameters 

93 'q': query, 

94 'count': "{0}".format(count), 

95 'offset': "{0}".format(offset), 

96 'mkt': market, 

97 'safeSearch': 'Moderate', 

98 }) 

99 

100 try: 

101 conn = http.client.HTTPSConnection('api.cognitive.microsoft.com') 

102 conn.request("GET", "/bing/v5.0/news/search?%s" % 

103 params, "{body}", headers) 

104 response = conn.getresponse() 

105 data = response.read() 

106 conn.close() 

107 return bytes2python(data) 

108 except Exception as e: 

109 raise CognitiveException("[Errno {0}] {1}".format( 

110 getattr(e, "errno", "-"), getattr(e, 'strerror', '-'))) from e 

111 

112 

113def call_api_images(subscription_key, query, market="fr-FR", count=10, offset=0): 

114 """ 

115 Retrieves resuls for the image API. 

116 

117 @param subscription_key 

118 @param query query 

119 @param market market 

120 @param count number of results 

121 @param offset skip results 

122 @return results 

123 

124 Example of a result: 

125 

126 :: 

127 

128 {'_type': 'News', 

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

130 'value': [{'name': "Colombie : le bilan de la coulée de boue s'alourdit 54 morts", 

131 'url': 'http://www.bing.com/cr?IG=CEFF9C63FD7A4439B591261606484860&CID=3B75F5FD9A146DBF103CFFA49BF36C83&rd=1&', 

132 'image': {'thumbnail': {'contentUrl': 'https://www.bing.com/th?id=ON.08357B50C029DBC09D053826F9B22658&pid=News', 

133 'width': 700, 'height': 304}}, 

134 'description': "Selon les d꤬arations du président colombien ...., le bilan de la gigantesque coulée de ", 

135 'provider': [{'_type': 'Organization', 'name': 'Le Point'}], 

136 'datePublished': '2017-04-03T10:33:00', 

137 'headline': True, 

138 'clusteredArticles': [{'name': "Colombie : le bilan s'alourdit de 54 orts, dont 43 enfants", 

139 'url': 'http://www.bing.com/cr?I.....', 

140 'description': "La ville de Mocoa, capitale ..., a été frappée ces derniers ...", 

141 'provider': [{'_type': 'Organization', 'name': 'Le Figaro'}], 

142 'datePublished': '2017-04-03T09:38:00', 'headline': True}, 

143 ... 

144 

145 """ 

146 headers = { 

147 # Request headers 

148 'Ocp-Apim-Subscription-Key': subscription_key, 

149 } 

150 

151 params = urllib.parse.urlencode({ 

152 # Request parameters 

153 'q': query, 

154 'count': "{0}".format(count), 

155 'offset': "{0}".format(offset), 

156 'mkt': market, 

157 'safeSearch': 'Moderate', 

158 }) 

159 

160 try: 

161 conn = http.client.HTTPSConnection('api.cognitive.microsoft.com') 

162 conn.request("GET", "/bing/v7.0/images/search?%s" % 

163 params, "{body}", headers) 

164 response = conn.getresponse() 

165 data = response.read() 

166 conn.close() 

167 return bytes2python(data) 

168 except Exception as e: 

169 raise CognitiveException("[Errno {0}] {1}".format( 

170 getattr(e, "errno", "-"), getattr(e, 'strerror', '-'))) from e 

171 

172 

173def call_api_speech_reco(subkey, lang="fr-FR", filename=None, memwav=None, 

174 url="https://speech.platform.bing.com/recognize"): 

175 """ 

176 Implemented in module 

177 `ensae_teaching_cs <http://www.xavierdupre.fr/app/ensae_teaching_cs/helpsphinx/py-modindex.html>`_. 

178 Only available on Windows for the time being (relies on C# code). 

179 

180 @param subkey subscription key 

181 @param lang language 

182 @param filename wav file 

183 @param memwav wav memory stream 

184 @param url url of the service 

185 @return dictionary wih the results 

186 

187 Either filename or memwav must be specified. 

188 """ 

189 raise NotImplementedError("Implementation was removed.") 

190 # from ensae_teaching_cs.cspython import vocal_recognition 

191 # return vocal_recognition(subkey, lang=lang, filename=filename, memwav=memwav, url=url) 

192 

193 

194def call_api_emotions(subkey, image_or_bytes): 

195 """ 

196 Retrieves resuls for the emotions API. 

197 

198 @param subkey subscription key 

199 @param image_or_bytes image or bytes 

200 @return results 

201 """ 

202 headers = { 

203 # Request headers 

204 'Content-Type': 'application/octet-stream', 

205 'Ocp-Apim-Subscription-Key': subkey, 

206 } 

207 

208 params = urllib.parse.urlencode({}) 

209 

210 if isinstance(image_or_bytes, str): 

211 with open(image_or_bytes, "rb") as f: 

212 content = f.read() 

213 else: 

214 content = image_or_bytes 

215 

216 try: 

217 conn = http.client.HTTPSConnection( 

218 'westus.api.cognitive.microsoft.com') 

219 conn.request("POST", "/emotion/v1.0/recognize?%s" % 

220 params, content, headers) 

221 response = conn.getresponse() 

222 data = response.read() 

223 conn.close() 

224 return bytes2python(data) 

225 except Exception as e: 

226 import pprint 

227 pprint.pprint(e) 

228 pprint.pprint(e.__dict__) 

229 raise CognitiveException("[Errno {0}] {1}".format( 

230 getattr(e, 'errno', '-'), getattr(e, 'strerror', '-'))) from e