on master help sign in
Merge pull request #251 from Cqoicebordel/moar-engines

Moar engines

commit98aa70cd41de5ff26a0d1def77a45153ebf8b712 authorAdam Tauber <asciimoo@gmail.com> dateSun 22 Feb 2015, 23:29 (12 years ago) parent03579c256ad00b
7 files changed+207−29
searx/engines/spotify.py+60 searx/settings.yml+4−11 searx/tests/engines/test_blekko_images.py+8−3 searx/tests/engines/test_google_images.py+8−3 searx/tests/engines/test_spotify.py+124 searx/tests/engines/test_yahoo.py+2−12 searx/tests/test_engines.py+1
searx/engines/spotify.pynew file+60
@@ -0,0 +1,60 @@
1+## Spotify (Music)
2+#
3+# @website https://spotify.com
4+# @provide-api yes (https://developer.spotify.com/web-api/search-item/)
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes
9+# @parse url, title, content, embedded
10+
11+from json import loads
12+from urllib import urlencode
13+
14+# engine dependent config
15+categories = ['music']
16+paging = True
17+
18+# search-url
19+url = 'https://api.spotify.com/'
20+search_url = url + 'v1/search?{query}&type=track&offset={offset}'
21+
22+embedded_url = '<iframe data-src="https://embed.spotify.com/?uri=spotify:track:{audioid}"\
23+ width="300" height="80" frameborder="0" allowtransparency="true"></iframe>'
24+
25+
26+# do search-request
27+def request(query, params):
28+ offset = (params['pageno'] - 1) * 20
29+
30+ params['url'] = search_url.format(query=urlencode({'q': query}),
31+ offset=offset)
32+
33+ return params
34+
35+
36+# get response from search-request
37+def response(resp):
38+ results = []
39+
40+ search_res = loads(resp.text)
41+
42+ # parse results
43+ for result in search_res.get('tracks', {}).get('items', {}):
44+ if result['type'] == 'track':
45+ title = result['name']
46+ url = result['external_urls']['spotify']
47+ content = result['artists'][0]['name'] +\
48+ " &bull; " +\
49+ result['album']['name'] +\
50+ " &bull; " + result['name']
51+ embedded = embedded_url.format(audioid=result['id'])
52+
53+ # append result
54+ results.append({'url': url,
55+ 'title': title,
56+ 'embedded': embedded,
57+ 'content': content})
58+
59+ # return results
60+ return results
searx/settings.yml+4−11
@@ -84,12 +84,6 @@ engines:
8484# shortcut : fa
8585# api_key : 'apikey' # required!
8686
87−# down - website is under criminal investigation by the UK
88−# - name : filecrop
89−# engine : filecrop
90−# categories : files
91−# shortcut : fc
92−
9387 - name : 500px
9488 engine : www500px
9589 shortcut : px
@@ -109,11 +103,6 @@ engines:
109103# Or you can use the html non-stable engine, activated by default
110104 engine : flickr_noapi
111105
112− - name : general-file
113− engine : generalfile
114− shortcut : gf
115− disabled : True
116−
117106 - name : gigablast
118107 engine : gigablast
119108 shortcut : gb
@@ -201,6 +190,10 @@ engines:
201190 shortcut : scc
202191 disabled : True
203192
193+ - name : spotify
194+ engine : spotify
195+ shortcut : stf
196+
204197 - name : subtitleseeker
205198 engine : subtitleseeker
206199 shortcut : ss
searx/tests/engines/test_blekko_images.py+8−3
@@ -12,9 +12,14 @@ class TestBlekkoImagesEngine(SearxTestCase):
1212 dicto['pageno'] = 0
1313 dicto['safesearch'] = 1
1414 params = blekko_images.request(query, dicto)
15− self.assertTrue('url' in params)
16− self.assertTrue(query in params['url'])
17− self.assertTrue('blekko.com' in params['url'])
15+ self.assertIn('url', params)
16+ self.assertIn(query, params['url'])
17+ self.assertIn('blekko.com', params['url'])
18+ self.assertIn('page', params['url'])
19+
20+ dicto['pageno'] = 1
21+ params = blekko_images.request(query, dicto)
22+ self.assertNotIn('page', params['url'])
1823
1924 def test_response(self):
2025 self.assertRaises(AttributeError, blekko_images.response, None)
searx/tests/engines/test_google_images.py+8−3
@@ -11,9 +11,14 @@ class TestGoogleImagesEngine(SearxTestCase):
1111 dicto = defaultdict(dict)
1212 dicto['pageno'] = 1
1313 params = google_images.request(query, dicto)
14− self.assertTrue('url' in params)
15− self.assertTrue(query in params['url'])
16− self.assertTrue('googleapis.com' in params['url'])
14+ self.assertIn('url', params)
15+ self.assertIn(query, params['url'])
16+ self.assertIn('googleapis.com', params['url'])
17+ self.assertIn('safe=on', params['url'])
18+
19+ dicto['safesearch'] = 0
20+ params = google_images.request(query, dicto)
21+ self.assertIn('safe=off', params['url'])
1722
1823 def test_response(self):
1924 self.assertRaises(AttributeError, google_images.response, None)
searx/tests/engines/test_spotify.pynew file+124
@@ -0,0 +1,124 @@
1+from collections import defaultdict
2+import mock
3+from searx.engines import spotify
4+from searx.testing import SearxTestCase
5+
6+
7+class TestSpotifyEngine(SearxTestCase):
8+
9+ def test_request(self):
10+ query = 'test_query'
11+ dicto = defaultdict(dict)
12+ dicto['pageno'] = 0
13+ params = spotify.request(query, dicto)
14+ self.assertIn('url', params)
15+ self.assertIn(query, params['url'])
16+ self.assertIn('spotify.com', params['url'])
17+
18+ def test_response(self):
19+ self.assertRaises(AttributeError, spotify.response, None)
20+ self.assertRaises(AttributeError, spotify.response, [])
21+ self.assertRaises(AttributeError, spotify.response, '')
22+ self.assertRaises(AttributeError, spotify.response, '[]')
23+
24+ response = mock.Mock(text='{}')
25+ self.assertEqual(spotify.response(response), [])
26+
27+ response = mock.Mock(text='{"data": []}')
28+ self.assertEqual(spotify.response(response), [])
29+
30+ json = """
31+ {
32+ "tracks": {
33+ "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
34+ "items": [
35+ {
36+ "album": {
37+ "album_type": "album",
38+ "external_urls": {
39+ "spotify": "https://open.spotify.com/album/5c9ap1PBkSGLxT3J73toxA"
40+ },
41+ "href": "https://api.spotify.com/v1/albums/5c9ap1PBkSGLxT3J73toxA",
42+ "id": "5c9ap1PBkSGLxT3J73toxA",
43+ "name": "Album Title",
44+ "type": "album",
45+ "uri": "spotify:album:5c9ap1PBkSGLxT3J73toxA"
46+ },
47+ "artists": [
48+ {
49+ "external_urls": {
50+ "spotify": "https://open.spotify.com/artist/0bMc6b75FfZEpQHG1jifKu"
51+ },
52+ "href": "https://api.spotify.com/v1/artists/0bMc6b75FfZEpQHG1jifKu",
53+ "id": "0bMc6b75FfZEpQHG1jifKu",
54+ "name": "Artist Name",
55+ "type": "artist",
56+ "uri": "spotify:artist:0bMc6b75FfZEpQHG1jifKu"
57+ }
58+ ],
59+ "disc_number": 1,
60+ "duration_ms": 202386,
61+ "explicit": false,
62+ "external_ids": {
63+ "isrc": "FRV640600067"
64+ },
65+ "external_urls": {
66+ "spotify": "https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa"
67+ },
68+ "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
69+ "id": "1000",
70+ "is_playable": true,
71+ "name": "Title of track",
72+ "popularity": 6,
73+ "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
74+ "track_number": 3,
75+ "type": "track",
76+ "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
77+ }
78+ ],
79+ "limit": 20,
80+ "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
81+ "offset": 0,
82+ "previous": null,
83+ "total": 107
84+ }
85+ }
86+ """
87+ response = mock.Mock(text=json)
88+ results = spotify.response(response)
89+ self.assertEqual(type(results), list)
90+ self.assertEqual(len(results), 1)
91+ self.assertEqual(results[0]['title'], 'Title of track')
92+ self.assertEqual(results[0]['url'], 'https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa')
93+ self.assertEqual(results[0]['content'], 'Artist Name &bull; Album Title &bull; Title of track')
94+ self.assertIn('1000', results[0]['embedded'])
95+
96+ json = """
97+ {
98+ "tracks": {
99+ "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
100+ "items": [
101+ {
102+ "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
103+ "id": "1000",
104+ "is_playable": true,
105+ "name": "Title of track",
106+ "popularity": 6,
107+ "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
108+ "track_number": 3,
109+ "type": "album",
110+ "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
111+ }
112+ ],
113+ "limit": 20,
114+ "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
115+ "offset": 0,
116+ "previous": null,
117+ "total": 107
118+ }
119+ }
120+ """
121+ response = mock.Mock(text=json)
122+ results = spotify.response(response)
123+ self.assertEqual(type(results), list)
124+ self.assertEqual(len(results), 0)
searx/tests/engines/test_yahoo.py+2−12
@@ -75,12 +75,6 @@ class TestYahooEngine(SearxTestCase):
7575 <li>
7676 <div class="dd algo lst Sr">
7777 <div class="compTitle">
78− <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=AwrBT7zgEudUW.wAe2ZXNyoA;
79− _ylu=X3oDMTBybGY3bmpvBGNvbG8DYmYxBHBvcwMyBHZ0aWQDBHNlYwNzcg--/RV=2\/RE=1424458593/RO=10
80− /RU=https%3a%2f%2fthis.is.the.second.url%2f/RK=0/RS=jIctjj_cBH1Efj88GCgHKp3__Qk-"
81− target="_blank" data-bid="54e712e136926">
82− This is the second <b><b>title</b></b></a>
83− </h3>
8478 </div>
8579 <div class="compText aAbs">
8680 <p class="lh-18">This is the second content</p>
@@ -102,16 +96,12 @@ class TestYahooEngine(SearxTestCase):
10296 """
10397 response = mock.Mock(text=html)
10498 results = yahoo.response(response)
105− print results
10699 self.assertEqual(type(results), list)
107− self.assertEqual(len(results), 3)
100+ self.assertEqual(len(results), 2)
108101 self.assertEqual(results[0]['title'], 'This is the title')
109102 self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
110103 self.assertEqual(results[0]['content'], 'This is the content')
111− self.assertEqual(results[1]['title'], 'This is the second title')
112− self.assertEqual(results[1]['url'], 'https://this.is.the.second.url/')
113− self.assertEqual(results[1]['content'], 'This is the second content')
114− self.assertEqual(results[2]['suggestion'], 'This is the suggestion')
104+ self.assertEqual(results[1]['suggestion'], 'This is the suggestion')
115105
116106 html = """
117107<ol class="reg mb-15 searchCenterMiddle">
searx/tests/test_engines.py+1
@@ -28,6 +28,7 @@ from searx.tests.engines.test_piratebay import * # noqa
2828from searx.tests.engines.test_searchcode_code import * # noqa
2929from searx.tests.engines.test_searchcode_doc import * # noqa
3030from searx.tests.engines.test_soundcloud import * # noqa
31+from searx.tests.engines.test_spotify import * # noqa
3132from searx.tests.engines.test_stackoverflow import * # noqa
3233from searx.tests.engines.test_startpage import * # noqa
3334from searx.tests.engines.test_subtitleseeker import * # noqa