Kaynağa Gözat

Add INA search engine

François Revol 8 yıl önce
ebeveyn
işleme
b538de568a
3 değiştirilmiş dosya ile 153 ekleme ve 0 silme
  1. 83
    0
      searx/engines/ina.py
  2. 6
    0
      searx/settings.yml
  3. 64
    0
      tests/unit/engines/test_ina.py

+ 83
- 0
searx/engines/ina.py Dosyayı Görüntüle

@@ -0,0 +1,83 @@
1
+#  INA (Videos)
2
+#
3
+# @website     https://www.ina.fr/
4
+# @provide-api no
5
+#
6
+# @using-api   no
7
+# @results     HTML (using search portal)
8
+# @stable      no (HTML can change)
9
+# @parse       url, title, content, publishedDate, thumbnail
10
+#
11
+# @todo        set content-parameter with correct data
12
+# @todo        embedded (needs some md5 from video page)
13
+
14
+from json import loads
15
+from urllib import urlencode
16
+from lxml import html
17
+from HTMLParser import HTMLParser
18
+from searx.engines.xpath import extract_text
19
+from dateutil import parser
20
+
21
+# engine dependent config
22
+categories = ['videos']
23
+paging = True
24
+page_size = 48
25
+
26
+# search-url
27
+base_url = 'https://www.ina.fr'
28
+search_url = base_url + '/layout/set/ajax/recherche/result?autopromote=&hf={ps}&b={start}&type=Video&r=&{query}'
29
+
30
+# specific xpath variables
31
+results_xpath = '//div[contains(@class,"search-results--list")]/div[@class="media"]'
32
+url_xpath = './/a/@href'
33
+title_xpath = './/h3[@class="h3--title media-heading"]'
34
+thumbnail_xpath = './/img/@src'
35
+publishedDate_xpath = './/span[@class="broadcast"]'
36
+content_xpath = './/p[@class="media-body__summary"]'
37
+
38
+
39
+# do search-request
40
+def request(query, params):
41
+    params['url'] = search_url.format(ps=page_size,
42
+                                      start=params['pageno'] * page_size,
43
+                                      query=urlencode({'q': query}))
44
+
45
+    return params
46
+
47
+
48
+# get response from search-request
49
+def response(resp):
50
+    results = []
51
+
52
+    # we get html in a JSON container...
53
+    response = loads(resp.text)
54
+    if "content" not in response:
55
+        return []
56
+    dom = html.fromstring(response["content"])
57
+    p = HTMLParser()
58
+
59
+    # parse results
60
+    for result in dom.xpath(results_xpath):
61
+        videoid = result.xpath(url_xpath)[0]
62
+        url = base_url + videoid
63
+        title = p.unescape(extract_text(result.xpath(title_xpath)))
64
+        thumbnail = extract_text(result.xpath(thumbnail_xpath)[0])
65
+        if thumbnail[0] == '/':
66
+            thumbnail = base_url + thumbnail
67
+        d = extract_text(result.xpath(publishedDate_xpath)[0])
68
+        d = d.split('/')
69
+        # force ISO date to avoid wrong parsing
70
+        d = "%s-%s-%s" % (d[2], d[1], d[0])
71
+        publishedDate = parser.parse(d)
72
+        content = extract_text(result.xpath(content_xpath))
73
+
74
+        # append result
75
+        results.append({'url': url,
76
+                        'title': title,
77
+                        'content': content,
78
+                        'template': 'videos.html',
79
+                        'publishedDate': publishedDate,
80
+                        'thumbnail': thumbnail})
81
+
82
+    # return results
83
+    return results

+ 6
- 0
searx/settings.yml Dosyayı Görüntüle

@@ -254,6 +254,12 @@ engines:
254 254
     disabled : True
255 255
     shortcut : habr
256 256
 
257
+  - name : ina
258
+    engine : ina
259
+    shortcut : in
260
+    timeout : 6.0
261
+    disabled : True
262
+
257 263
   - name : mixcloud
258 264
     engine : mixcloud
259 265
     shortcut : mc

+ 64
- 0
tests/unit/engines/test_ina.py Dosyayı Görüntüle

@@ -0,0 +1,64 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import ina
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestInaEngine(SearxTestCase):
8
+
9
+    def test_request(self):
10
+        query = 'test_query'
11
+        dicto = defaultdict(dict)
12
+        dicto['pageno'] = 0
13
+        params = ina.request(query, dicto)
14
+        self.assertTrue('url' in params)
15
+        self.assertTrue(query in params['url'])
16
+        self.assertTrue('ina.fr' in params['url'])
17
+
18
+    def test_response(self):
19
+        self.assertRaises(AttributeError, ina.response, None)
20
+        self.assertRaises(AttributeError, ina.response, [])
21
+        self.assertRaises(AttributeError, ina.response, '')
22
+        self.assertRaises(AttributeError, ina.response, '[]')
23
+
24
+        response = mock.Mock(text='{}')
25
+        self.assertEqual(ina.response(response), [])
26
+
27
+        response = mock.Mock(text='{"data": []}')
28
+        self.assertEqual(ina.response(response), [])
29
+
30
+        json = """
31
+        {"content":"\\t<div class=\\"container\\">\\n\\t\\n\
32
+        <!-- DEBUT CONTENU PRINCIPAL -->\\n<div class=\\"row\\">\\n\
33
+        <div class=\\"search-results--list\\"><div class=\\"media\\">\\n\
34
+        \\t\\t\\t\\t<a class=\\"media-left  media-video  premium    xiti_click_action\\" \
35
+        data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
36
+        href=\\"\/video\/CAF89035682\/conference-de-presse-du-general-de-gaulle-video.html\\">\\n\
37
+        <img src=\\"https:\/\/www.ina.fr\/images_v2\/140x105\/CAF89035682.jpeg\\" \
38
+        alt=\\"Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle \\">\\n\
39
+        \\t\\t\\t\\t\\t<\/a>\\n\
40
+        \\t\\t\\t\\t\\t<div class=\\"media-body\\">\\n\\t\\t\\t\\t\\t\\t<h3 class=\\"h3--title media-heading\\">\\n\
41
+        \\t\\t\\t\\t\\t\\t\\t<a class=\\"xiti_click_action\\" \
42
+        data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
43
+        href=\\"\/video\/CAF89035682\/conference-de-presse-du-general-de-gaulle-video.html\\">\
44
+        Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle <\/a>\\n\
45
+        <\/h3>\\n\
46
+        <div class=\\"media-body__info\\">\\n<span class=\\"broadcast\\">27\/11\/1967<\/span>\\n\
47
+        <span class=\\"views\\">29321 vues<\/span>\\n\
48
+        <span class=\\"duration\\">01h 33m 07s<\/span>\\n\
49
+        <\/div>\\n\
50
+        <p class=\\"media-body__summary\\">VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE . \
51
+              - PA le Pr\\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...<\/p>\\n\
52
+        <\/div>\\n<\/div><!-- \/.media -->\\n"
53
+        }
54
+        """
55
+        response = mock.Mock(text=json)
56
+        results = ina.response(response)
57
+        self.assertEqual(type(results), list)
58
+        self.assertEqual(len(results), 1)
59
+        self.assertEqual(results[0]['title'], u'Conf\xe9rence de presse du G\xe9n\xe9ral de Gaulle')
60
+        self.assertEqual(results[0]['url'],
61
+                         'https://www.ina.fr/video/CAF89035682/conference-de-presse-du-general-de-gaulle-video.html')
62
+        self.assertEqual(results[0]['content'],
63
+                         u"VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE ."
64
+                         u" - PA le Pr\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...")