Procházet zdrojové kódy

[fix] flickr.com mods

Adam Tauber před 11 roky
rodič
revize
3386e21cdf
1 změnil soubory, kde provedl 34 přidání a 28 odebrání
  1. 34
    28
      searx/engines/flickr.py

+ 34
- 28
searx/engines/flickr.py Zobrazit soubor

@@ -1,48 +1,54 @@
1 1
 #!/usr/bin/env python
2 2
 
3 3
 from urllib import urlencode
4
-from json import loads
5
-#from urlparse import urljoin
4
+#from json import loads
5
+from urlparse import urljoin
6
+from lxml import html
7
+from time import time
6 8
 
7 9
 categories = ['images']
8 10
 
9
-# url = 'https://secure.flickr.com/'
10
-# search_url = url+'search/?{query}&page={page}'
11
-# results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]'  # noqa
11
+url = 'https://secure.flickr.com/'
12
+search_url = url+'search/?{query}&page={page}'
13
+results_xpath = '//div[@class="view display-item-tile"]/figure/div'
12 14
 
13 15
 paging = True
14 16
 
15
-# text=[query]
16
-# TODO clean "extras"
17
-search_url = 'https://api.flickr.com/services/rest?extras=can_addmeta%2Ccan_comment%2Ccan_download%2Ccan_share%2Ccontact%2Ccount_comments%2Ccount_faves%2Ccount_notes%2Cdate_taken%2Cdate_upload%2Cdescription%2Cicon_urls_deep%2Cisfavorite%2Cispro%2Clicense%2Cmedia%2Cneeds_interstitial%2Cowner_name%2Cowner_datecreate%2Cpath_alias%2Crealname%2Csafety_level%2Csecret_k%2Csecret_h%2Curl_c%2Curl_h%2Curl_k%2Curl_l%2Curl_m%2Curl_n%2Curl_o%2Curl_q%2Curl_s%2Curl_sq%2Curl_t%2Curl_z%2Cvisibility&per_page=50&page={page}&{query}&sort=relevance&method=flickr.photos.search&api_key=ad11b34c341305471e3c410a02e671d0&format=json'  # noqa
18
-
19 17
 
20 18
 def request(query, params):
21 19
     params['url'] = search_url.format(query=urlencode({'text': query}),
22 20
                                       page=params['pageno'])
23
-    #params['url'] = search_url.format(query=urlencode({'q': query}),
24
-    #                                  page=params['pageno'])
21
+    time_string = str(int(time())-3)
22
+    params['cookies']['BX'] = '3oqjr6d9nmpgl&b=3&s=dh'
23
+    params['cookies']['xb'] = '421409'
24
+    params['cookies']['localization'] = 'en-us'
25
+    params['cookies']['flrbp'] = time_string +\
26
+        '-3a8cdb85a427a33efda421fbda347b2eaf765a54'
27
+    params['cookies']['flrbs'] = time_string +\
28
+        '-ed142ae8765ee62c9ec92a9513665e0ee1ba6776'
29
+    params['cookies']['flrb'] = '9'
25 30
     return params
26 31
 
27 32
 
28 33
 def response(resp):
29 34
     results = []
30
-    images = loads(resp.text[14:-1])["photos"]["photo"]
31
-    for i in images:
32
-        results.append({'url': i['url_s'],
33
-                        'title': i['title'],
34
-                        'img_src': i['url_s'],
35
+    dom = html.fromstring(resp.text)
36
+    for result in dom.xpath(results_xpath):
37
+        img = result.xpath('.//img')
38
+
39
+        if not img:
40
+            continue
41
+
42
+        img = img[0]
43
+        img_src = 'https:'+img.attrib.get('src')
44
+
45
+        if not img_src:
46
+            continue
47
+
48
+        href = urljoin(url, result.xpath('.//a')[0].attrib.get('href'))
49
+        title = img.attrib.get('alt', '')
50
+        results.append({'url': href,
51
+                        'title': title,
52
+                        'img_src': img_src,
35 53
                         'template': 'images.html'})
36
-    #dom = html.fromstring(resp.text)
37
-    #for result in dom.xpath(results_xpath):
38
-    #    href = urljoin(url, result.attrib.get('href'))
39
-    #    img = result.xpath('.//img')[0]
40
-    #    title = img.attrib.get('alt', '')
41
-    #    img_src = img.attrib.get('data-defer-src')
42
-    #    if not img_src:
43
-    #        continue
44
-    #    results.append({'url': href,
45
-    #                    'title': title,
46
-    #                    'img_src': img_src,
47
-    #                    'template': 'images.html'})
48 54
     return results