Ver código fonte

Merge pull request #251 from Cqoicebordel/moar-engines

Moar engines
Adam Tauber 10 anos atrás
pai
commit
98aa70cd41

+ 60
- 0
searx/engines/spotify.py Ver arquivo

@@ -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

+ 4
- 11
searx/settings.yml Ver arquivo

@@ -84,12 +84,6 @@ engines:
84 84
 #    shortcut : fa
85 85
 #    api_key : 'apikey' # required!
86 86
 
87
-# down - website is under criminal investigation by the UK
88
-#  - name : filecrop
89
-#    engine : filecrop
90
-#    categories : files
91
-#    shortcut : fc
92
-
93 87
   - name : 500px
94 88
     engine : www500px
95 89
     shortcut : px
@@ -109,11 +103,6 @@ engines:
109 103
 # Or you can use the html non-stable engine, activated by default
110 104
     engine : flickr_noapi
111 105
 
112
-  - name : general-file
113
-    engine : generalfile
114
-    shortcut : gf
115
-    disabled : True
116
-
117 106
   - name : gigablast
118 107
     engine : gigablast
119 108
     shortcut : gb
@@ -201,6 +190,10 @@ engines:
201 190
     shortcut : scc
202 191
     disabled : True
203 192
 
193
+  - name : spotify
194
+    engine : spotify
195
+    shortcut : stf
196
+
204 197
   - name : subtitleseeker
205 198
     engine : subtitleseeker
206 199
     shortcut : ss

+ 8
- 3
searx/tests/engines/test_blekko_images.py Ver arquivo

@@ -12,9 +12,14 @@ class TestBlekkoImagesEngine(SearxTestCase):
12 12
         dicto['pageno'] = 0
13 13
         dicto['safesearch'] = 1
14 14
         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'])
18 23
 
19 24
     def test_response(self):
20 25
         self.assertRaises(AttributeError, blekko_images.response, None)

+ 8
- 3
searx/tests/engines/test_google_images.py Ver arquivo

@@ -11,9 +11,14 @@ class TestGoogleImagesEngine(SearxTestCase):
11 11
         dicto = defaultdict(dict)
12 12
         dicto['pageno'] = 1
13 13
         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'])
17 22
 
18 23
     def test_response(self):
19 24
         self.assertRaises(AttributeError, google_images.response, None)

+ 124
- 0
searx/tests/engines/test_spotify.py Ver arquivo

@@ -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)

+ 2
- 12
searx/tests/engines/test_yahoo.py Ver arquivo

@@ -75,12 +75,6 @@ class TestYahooEngine(SearxTestCase):
75 75
     <li>
76 76
         <div class="dd algo lst Sr">
77 77
             <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>
84 78
             </div>
85 79
             <div class="compText aAbs">
86 80
                 <p class="lh-18">This is the second content</p>
@@ -102,16 +96,12 @@ class TestYahooEngine(SearxTestCase):
102 96
         """
103 97
         response = mock.Mock(text=html)
104 98
         results = yahoo.response(response)
105
-        print results
106 99
         self.assertEqual(type(results), list)
107
-        self.assertEqual(len(results), 3)
100
+        self.assertEqual(len(results), 2)
108 101
         self.assertEqual(results[0]['title'], 'This is the title')
109 102
         self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
110 103
         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')
115 105
 
116 106
         html = """
117 107
 <ol class="reg mb-15 searchCenterMiddle">

+ 1
- 0
searx/tests/test_engines.py Ver arquivo

@@ -28,6 +28,7 @@ from searx.tests.engines.test_piratebay import *  # noqa
28 28
 from searx.tests.engines.test_searchcode_code import *  # noqa
29 29
 from searx.tests.engines.test_searchcode_doc import *  # noqa
30 30
 from searx.tests.engines.test_soundcloud import *  # noqa
31
+from searx.tests.engines.test_spotify import *  # noqa
31 32
 from searx.tests.engines.test_stackoverflow import *  # noqa
32 33
 from searx.tests.engines.test_startpage import *  # noqa
33 34
 from searx.tests.engines.test_subtitleseeker import *  # noqa