Procházet zdrojové kódy

[enh] twitter engine added

asciimoo před 11 roky
rodič
revize
c7b5cddc48
1 změnil soubory, kde provedl 26 přidání a 0 odebrání
  1. 26
    0
      searx/engines/twitter.py

+ 26
- 0
searx/engines/twitter.py Zobrazit soubor

@@ -0,0 +1,26 @@
1
+from urlparse import urljoin
2
+from urllib import urlencode
3
+from lxml import html
4
+
5
+categories = ['social media']
6
+
7
+base_url = 'https://twitter.com/'
8
+search_url = base_url+'search?'
9
+
10
+def request(query, params):
11
+    global search_url
12
+    params['url'] = search_url + urlencode({'q': query})
13
+    return params
14
+
15
+
16
+def response(resp):
17
+    global base_url
18
+    results = []
19
+    dom = html.fromstring(resp.text)
20
+    for tweet in dom.xpath('//li[@data-item-type="tweet"]'):
21
+        link = tweet.xpath('.//small[@class="time"]//a')[0]
22
+        url = urljoin(base_url, link.attrib.get('href'))
23
+        title = ''.join(tweet.xpath('.//span[@class="username js-action-profile-name"]//text()'))
24
+        content = ''.join(map(html.tostring, tweet.xpath('.//p[@class="js-tweet-text tweet-text"]//*')))
25
+        results.append({'url': url, 'title': title, 'content': content})
26
+    return results