@@ -0,0 +1,502 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
4+from searx.engines import twitter
5+from searx.testing import SearxTestCase
8+class TestTwitterEngine(SearxTestCase):
10+ def test_request(self):
12+ dicto = defaultdict(dict)
14+ dicto['language'] = 'fr_FR'
15+ params = twitter.request(query, dicto)
16+ self.assertIn('url', params)
17+ self.assertIn(query, params['url'])
18+ self.assertIn('twitter.com', params['url'])
19+ self.assertIn('cookies', params)
20+ self.assertIn('lang', params['cookies'])
21+ self.assertIn('fr', params['cookies']['lang'])
23+ dicto['language'] = 'all'
24+ params = twitter.request(query, dicto)
25+ self.assertIn('cookies', params)
26+ self.assertIn('lang', params['cookies'])
27+ self.assertIn('en', params['cookies']['lang'])
29+ def test_response(self):
30+ self.assertRaises(AttributeError, twitter.response, None)
31+ self.assertRaises(AttributeError, twitter.response, [])
32+ self.assertRaises(AttributeError, twitter.response, '')
33+ self.assertRaises(AttributeError, twitter.response, '[]')
35+ response = mock.Mock(text='<html></html>')
36+ self.assertEqual(twitter.response(response), [])
39+ <li class="js-stream-item stream-item stream-item expanding-stream-item" data-item-id="563005573290287105"
40+ id="stream-item-tweet-563005573290287105" data-item-type="tweet">
41+ <div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable
42+ js-original-tweet has-cards has-native-media" data-tweet-id="563005573290287105" data-disclosure-type=""
43+ data-item-id="563005573290287105" data-screen-name="Jalopnik" data-name="Jalopnik"
44+ data-user-id="3060631" data-has-native-media="true" data-has-cards="true" data-card-type="photo"
45+ data-expanded-footer="<div class="js-tweet-details-fixer
46+ tweet-details-fixer">
47+ <div class="cards-media-container js-media-container"><div
48+ data-card-url="//twitter.com/Jalopnik/status/563005573290287105/photo/1" data-card-type="
49+ photo" class="cards-base cards-multimedia" data-element-context="platform_photo_card
50+ "> <a class="media media-thumbnail twitter-timeline-link is-preview
51+ " data-url="https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large"
52+ data-resolved-url-large="https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large"
53+ href="//twitter.com/Jalopnik/status/563005573290287105/photo/1">
54+ <div class=""> <img src="
55+ https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg"
56+ alt="Embedded image permalink" width="636" height="309">
57+ </div> </a> <div class="cards-content">
58+ <div class="byline"> </div> </div>
59+ </div> </div> <div
60+ class="js-machine-translated-tweet-container"></div> <div
61+ class="js-tweet-stats-container tweet-stats-container "> </div>
62+ <div class="client-and-actions"> <span class="metadata">
63+ <span>5:06 PM - 4 Feb 2015</span> &middot; <a
64+ class="permalink-link js-permalink js-nav" href="/Jalopnik/status/563005573290287105
65+ "tabindex="-1">Details</a>
66+ </span> </div> </div> " data-you-follow="false"
67+ data-you-block="false">
68+ <div class="context">
70+ <div class="content">
71+ <div class="stream-item-header">
72+ <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav"
73+ href="/Jalopnik" data-user-id="3060631">
74+ <img class="avatar js-action-profile-avatar"
75+ src="https://pbs.twimg.com/profile_images/2976430168/5cd4a59_bigger.jpeg" alt="">
76+ <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part>
80+ <span class="username js-action-profile-name" data-aria-label-part>
81+ <s>@</s><b>TitleName</b>
85+ <a href="/this.is.the.url"
86+ class="tweet-timestamp js-permalink js-nav js-tooltip" title="5:06 PM - 4 Feb 2015" >
87+ <span class="u-hiddenVisually" data-aria-label-part="last">17 minutes ago</span>
91+ <p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0">
92+ This is the content étude à€
93+ <a href="http://t.co/nRWsqQAwBL" rel="nofollow" dir="ltr"
94+ data-expanded-url="http://jalo.ps/ReMENu4" class="twitter-timeline-link"
95+ target="_blank" title="http://jalo.ps/ReMENu4" >
96+ <span class="tco-ellipsis">
98+ <span class="invisible">http://</span><span class="js-display-url">link.in.tweet</span>
99+ <span class="invisible"></span>
100+ <span class="tco-ellipsis">
101+ <span class="invisible"> </span>
104+ <a href="http://t.co/rbFsfeE0l3" class="twitter-timeline-link u-hidden"
105+ data-pre-embedded="true" dir="ltr">
106+ pic.twitter.com/rbFsfeE0l3
109+ <div class="expanded-content js-tweet-details-dropdown">
111+ <div class="stream-item-footer">
112+ <a class="details with-icn js-details" href="/Jalopnik/status/563005573290287105">
113+ <span class="Icon Icon--photo">
116+ <span class="expand-stream-item js-view-details">
119+ <span class="collapse-stream-item js-hide-details">
124+ <span class="ProfileTweet-action--reply u-hiddenVisually">
125+ <span class="ProfileTweet-actionCount" aria-hidden="true" data-tweet-stat-count="0">
126+ <span class="ProfileTweet-actionCountForAria" >0 replies</span>
129+ <span class="ProfileTweet-action--retweet u-hiddenVisually">
130+ <span class="ProfileTweet-actionCount" data-tweet-stat-count="8">
131+ <span class="ProfileTweet-actionCountForAria" data-aria-label-part>8 retweets</span>
134+ <span class="ProfileTweet-action--favorite u-hiddenVisually">
135+ <span class="ProfileTweet-actionCount" data-tweet-stat-count="14">
136+ <span class="ProfileTweet-actionCountForAria" data-aria-label-part>14 favorites</span>
139+ <div role="group" aria-label="Tweet actions" class="ProfileTweet-actionList u-cf js-actions">
140+ <div class="ProfileTweet-action ProfileTweet-action--reply">
141+ <button class="ProfileTweet-actionButton u-textUserColorHover js-actionButton
142+ js-actionReply" data-modal="ProfileTweet-reply" type="button" title="Reply">
143+ <span class="Icon Icon--reply">
145+ <span class="u-hiddenVisually">Reply</span>
146+ <span class="ProfileTweet-actionCount u-textUserColorHover
147+ ProfileTweet-actionCount--isZero">
148+ <span class="ProfileTweet-actionCountForPresentation" aria-hidden="true">
153+ <div class="ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt">
154+ <button class="ProfileTweet-actionButton js-actionButton js-actionRetweet js-tooltip"
155+ title="Retweet" data-modal="ProfileTweet-retweet" type="button">
156+ <span class="Icon Icon--retweet">
158+ <span class="u-hiddenVisually">Retweet</span>
159+ <span class="ProfileTweet-actionCount">
160+ <span class="ProfileTweet-actionCountForPresentation">8</span>
163+ <button class="ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet"
164+ data-modal="ProfileTweet-retweet" title="Undo retweet" type="button">
165+ <span class="Icon Icon--retweet">
167+ <span class="u-hiddenVisually">Retweeted</span>
168+ <span class="ProfileTweet-actionCount">
169+ <span class="ProfileTweet-actionCountForPresentation">8</span>
173+ <div class="ProfileTweet-action ProfileTweet-action--favorite js-toggleState">
174+ <button class="ProfileTweet-actionButton js-actionButton js-actionFavorite js-tooltip"
175+ title="Favorite" type="button">
176+ <span class="Icon Icon--favorite">
178+ <span class="u-hiddenVisually">Favorite</span>
179+ <span class="ProfileTweet-actionCount">
180+ <span class="ProfileTweet-actionCountForPresentation">14</span>
183+ <button class="ProfileTweet-actionButtonUndo u-linkClean js-actionButton
184+ js-actionFavorite" title="Undo favorite" type="button">
185+ <span class="Icon Icon--favorite">
187+ <span class="u-hiddenVisually">Favorited</span>
188+ <span class="ProfileTweet-actionCount">
189+ <span class="ProfileTweet-actionCountForPresentation">
195+ <div class="ProfileTweet-action ProfileTweet-action--more js-more-ProfileTweet-actions">
196+ <div class="dropdown">
197+ <button class="ProfileTweet-actionButton u-textUserColorHover dropdown-toggle
198+ js-tooltip js-dropdown-toggle" type="button" title="More">
199+ <span class="Icon Icon--dots">
201+ <span class="u-hiddenVisually">More</span>
203+ <div class="dropdown-menu">
204+ <div class="dropdown-caret">
205+ <div class="caret-outer">
207+ <div class="caret-inner">
211+ <li class="share-via-dm js-actionShareViaDM" data-nav="share_tweet_dm">
212+ <button type="button" class="dropdown-link">
213+ Share via Direct Message
216+ <li class="embed-link js-actionEmbedTweet" data-nav="embed_tweet">
217+ <button type="button" class="dropdown-link">
221+ <li class="mute-user-item pretty-link">
222+ <button type="button" class="dropdown-link">
226+ <li class="unmute-user-item pretty-link">
227+ <button type="button" class="dropdown-link">
231+ <li class="block-or-report-link js-actionBlockOrReport"
232+ data-nav="block_or_report">
233+ <button type="button" class="dropdown-link">
247+ response = mock.Mock(text=html)
248+ results = twitter.response(response)
249+ self.assertEqual(type(results), list)
250+ self.assertEqual(len(results), 1)
251+ self.assertEqual(results[0]['title'], '@TitleName')
252+ self.assertEqual(results[0]['url'], 'https://twitter.com/this.is.the.url')
253+ self.assertIn(u'This is the content', results[0]['content'])
254+ # self.assertIn(u'This is the content étude à€', results[0]['content'])
257+ <li class="js-stream-item stream-item stream-item expanding-stream-item" data-item-id="563005573290287105"
258+ id="stream-item-tweet-563005573290287105" data-item-type="tweet">
259+ <div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable
260+ js-original-tweet has-cards has-native-media" data-tweet-id="563005573290287105" data-disclosure-type=""
261+ data-item-id="563005573290287105" data-screen-name="Jalopnik" data-name="Jalopnik"
262+ data-user-id="3060631" data-has-native-media="true" data-has-cards="true" data-card-type="photo"
263+ data-expanded-footer="<div class="js-tweet-details-fixer
264+ tweet-details-fixer">
265+ <div class="cards-media-container js-media-container"><div
266+ data-card-url="//twitter.com/Jalopnik/status/563005573290287105/photo/1" data-card-type="
267+ photo" class="cards-base cards-multimedia" data-element-context="platform_photo_card
268+ "> <a class="media media-thumbnail twitter-timeline-link is-preview
269+ " data-url="https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large"
270+ data-resolved-url-large="https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large"
271+ href="//twitter.com/Jalopnik/status/563005573290287105/photo/1">
272+ <div class=""> <img src="
273+ https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg"
274+ alt="Embedded image permalink" width="636" height="309">
275+ </div> </a> <div class="cards-content">
276+ <div class="byline"> </div> </div>
277+ </div> </div> <div
278+ class="js-machine-translated-tweet-container"></div> <div
279+ class="js-tweet-stats-container tweet-stats-container "> </div>
280+ <div class="client-and-actions"> <span class="metadata">
281+ <span>5:06 PM - 4 Feb 2015</span> &middot; <a
282+ class="permalink-link js-permalink js-nav" href="/Jalopnik/status/563005573290287105
283+ "tabindex="-1">Details</a>
284+ </span> </div> </div> " data-you-follow="false"
285+ data-you-block="false">
286+ <div class="context">
288+ <div class="content">
289+ <div class="stream-item-header">
290+ <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav"
291+ href="/Jalopnik" data-user-id="3060631">
292+ <img class="avatar js-action-profile-avatar"
293+ src="https://pbs.twimg.com/profile_images/2976430168/5cd4a59_bigger.jpeg" alt="">
294+ <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part>
298+ <span class="username js-action-profile-name" data-aria-label-part>
299+ <s>@</s><b>TitleName</b>
302+ <small class="time">
303+ <a href="/this.is.the.url"
304+ class="tweet-timestamp js-permalink js-nav js-tooltip" title="5:06 PM - 4 Feb 2015" >
305+ <span class="_timestamp js-short-timestamp js-relative-timestamp" data-time="1423065963"
306+ data-time-ms="1423065963000" data-long-form="true" aria-hidden="true">
309+ <span class="u-hiddenVisually" data-aria-label-part="last">17 minutes ago</span>
313+ <p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0">
314+ This is the content étude à€
315+ <a href="http://t.co/nRWsqQAwBL" rel="nofollow" dir="ltr"
316+ data-expanded-url="http://jalo.ps/ReMENu4" class="twitter-timeline-link"
317+ target="_blank" title="http://jalo.ps/ReMENu4" >
318+ <span class="tco-ellipsis">
320+ <span class="invisible">http://</span><span class="js-display-url">link.in.tweet</span>
321+ <span class="invisible"></span>
322+ <span class="tco-ellipsis">
323+ <span class="invisible"> </span>
326+ <a href="http://t.co/rbFsfeE0l3" class="twitter-timeline-link u-hidden"
327+ data-pre-embedded="true" dir="ltr">
328+ pic.twitter.com/rbFsfeE0l3
331+ <div class="expanded-content js-tweet-details-dropdown">
333+ <div class="stream-item-footer">
334+ <a class="details with-icn js-details" href="/Jalopnik/status/563005573290287105">
335+ <span class="Icon Icon--photo">
338+ <span class="expand-stream-item js-view-details">
341+ <span class="collapse-stream-item js-hide-details">
346+ <span class="ProfileTweet-action--reply u-hiddenVisually">
347+ <span class="ProfileTweet-actionCount" aria-hidden="true" data-tweet-stat-count="0">
348+ <span class="ProfileTweet-actionCountForAria" >0 replies</span>
351+ <span class="ProfileTweet-action--retweet u-hiddenVisually">
352+ <span class="ProfileTweet-actionCount" data-tweet-stat-count="8">
353+ <span class="ProfileTweet-actionCountForAria" data-aria-label-part>8 retweets</span>
356+ <span class="ProfileTweet-action--favorite u-hiddenVisually">
357+ <span class="ProfileTweet-actionCount" data-tweet-stat-count="14">
358+ <span class="ProfileTweet-actionCountForAria" data-aria-label-part>14 favorites</span>
361+ <div role="group" aria-label="Tweet actions" class="ProfileTweet-actionList u-cf js-actions">
362+ <div class="ProfileTweet-action ProfileTweet-action--reply">
363+ <button class="ProfileTweet-actionButton u-textUserColorHover js-actionButton
364+ js-actionReply" data-modal="ProfileTweet-reply" type="button" title="Reply">
365+ <span class="Icon Icon--reply">
367+ <span class="u-hiddenVisually">Reply</span>
368+ <span class="ProfileTweet-actionCount u-textUserColorHover
369+ ProfileTweet-actionCount--isZero">
370+ <span class="ProfileTweet-actionCountForPresentation" aria-hidden="true">
375+ <div class="ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt">
376+ <button class="ProfileTweet-actionButton js-actionButton js-actionRetweet js-tooltip"
377+ title="Retweet" data-modal="ProfileTweet-retweet" type="button">
378+ <span class="Icon Icon--retweet">
380+ <span class="u-hiddenVisually">Retweet</span>
381+ <span class="ProfileTweet-actionCount">
382+ <span class="ProfileTweet-actionCountForPresentation">8</span>
385+ <button class="ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet"
386+ data-modal="ProfileTweet-retweet" title="Undo retweet" type="button">
387+ <span class="Icon Icon--retweet">
389+ <span class="u-hiddenVisually">Retweeted</span>
390+ <span class="ProfileTweet-actionCount">
391+ <span class="ProfileTweet-actionCountForPresentation">8</span>
395+ <div class="ProfileTweet-action ProfileTweet-action--favorite js-toggleState">
396+ <button class="ProfileTweet-actionButton js-actionButton js-actionFavorite js-tooltip"
397+ title="Favorite" type="button">
398+ <span class="Icon Icon--favorite">
400+ <span class="u-hiddenVisually">Favorite</span>
401+ <span class="ProfileTweet-actionCount">
402+ <span class="ProfileTweet-actionCountForPresentation">14</span>
405+ <button class="ProfileTweet-actionButtonUndo u-linkClean js-actionButton
406+ js-actionFavorite" title="Undo favorite" type="button">
407+ <span class="Icon Icon--favorite">
409+ <span class="u-hiddenVisually">Favorited</span>
410+ <span class="ProfileTweet-actionCount">
411+ <span class="ProfileTweet-actionCountForPresentation">
417+ <div class="ProfileTweet-action ProfileTweet-action--more js-more-ProfileTweet-actions">
418+ <div class="dropdown">
419+ <button class="ProfileTweet-actionButton u-textUserColorHover dropdown-toggle
420+ js-tooltip js-dropdown-toggle" type="button" title="More">
421+ <span class="Icon Icon--dots">
423+ <span class="u-hiddenVisually">More</span>
425+ <div class="dropdown-menu">
426+ <div class="dropdown-caret">
427+ <div class="caret-outer">
429+ <div class="caret-inner">
433+ <li class="share-via-dm js-actionShareViaDM" data-nav="share_tweet_dm">
434+ <button type="button" class="dropdown-link">
435+ Share via Direct Message
438+ <li class="embed-link js-actionEmbedTweet" data-nav="embed_tweet">
439+ <button type="button" class="dropdown-link">
443+ <li class="mute-user-item pretty-link">
444+ <button type="button" class="dropdown-link">
448+ <li class="unmute-user-item pretty-link">
449+ <button type="button" class="dropdown-link">
453+ <li class="block-or-report-link js-actionBlockOrReport"
454+ data-nav="block_or_report">
455+ <button type="button" class="dropdown-link">
469+ response = mock.Mock(text=html)
470+ results = twitter.response(response)
471+ self.assertEqual(type(results), list)
472+ self.assertEqual(len(results), 1)
473+ self.assertEqual(results[0]['title'], '@TitleName')
474+ self.assertEqual(results[0]['url'], 'https://twitter.com/this.is.the.url')
475+ self.assertIn(u'This is the content', results[0]['content'])
478+ <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
480+ <div class="sb_tlst">
482+ <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
483+ <strong>This</strong> should be the title</a>
486+ <div class="sb_meta">
488+ <strong>this</strong>.meta.com</cite>
489+ <span class="c_tlbxTrg">
490+ <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
495+ <strong>This</strong> should be the content.</p>
499+ response = mock.Mock(text=html)
500+ results = twitter.response(response)
501+ self.assertEqual(type(results), list)
502+ self.assertEqual(len(results), 0)