소스 검색

Merge pull request #149 from Cqoicebordel/Flickr-engine

Rework Flickr Engine
Adam Tauber 10 년 전
부모
커밋
af41607410
3개의 변경된 파일163개의 추가작업 그리고 31개의 파일을 삭제
  1. 102
    0
      searx/engines/flickr-noapi.py
  2. 55
    29
      searx/engines/flickr.py
  3. 6
    2
      searx/settings.yml

+ 102
- 0
searx/engines/flickr-noapi.py 파일 보기

@@ -0,0 +1,102 @@
1
+#!/usr/bin/env python
2
+
3
+## Flickr (Images)
4
+# 
5
+# @website     https://www.flickr.com
6
+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html) 
7
+# 
8
+# @using-api   no
9
+# @results     HTML
10
+# @stable      no
11
+# @parse       url, title, thumbnail, img_src
12
+
13
+from urllib import urlencode
14
+from json import loads
15
+from urlparse import urljoin
16
+from lxml import html
17
+import re
18
+
19
+categories = ['images']
20
+
21
+url = 'https://secure.flickr.com/'
22
+search_url = url+'search/?{query}&page={page}'
23
+photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
24
+regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
25
+
26
+paging = True
27
+
28
+def build_flickr_url(user_id, photo_id):
29
+    return photo_url.format(userid=user_id,photoid=photo_id)
30
+
31
+
32
+def request(query, params):
33
+    params['url'] = search_url.format(query=urlencode({'text': query}),
34
+                                      page=params['pageno'])
35
+    return params
36
+
37
+
38
+def response(resp):
39
+    results = []
40
+    
41
+    matches = regex.search(resp.text)
42
+    
43
+    if matches == None:
44
+        return results
45
+
46
+    match = matches.group(1)
47
+    search_results = loads(match)
48
+    
49
+    if not '_data' in search_results:
50
+        return []
51
+    
52
+    photos = search_results['_data']
53
+    
54
+    for photo in photos:
55
+        
56
+        # In paged configuration, the first pages' photos are represented by a None object
57
+        if photo == None:
58
+            continue
59
+        
60
+        # From the biggest to the lowest format
61
+        if 'o' in photo['sizes']:
62
+            img_src = photo['sizes']['o']['displayUrl']
63
+        elif 'k' in photo['sizes']:
64
+            img_src = photo['sizes']['k']['displayUrl']
65
+        elif 'h' in photo['sizes']:
66
+            img_src = photo['sizes']['h']['displayUrl']
67
+        elif 'b' in photo['sizes']:
68
+            img_src = photo['sizes']['b']['displayUrl']
69
+        elif 'c' in photo['sizes']:
70
+            img_src = photo['sizes']['c']['displayUrl']
71
+        elif 'z' in photo['sizes']:
72
+            img_src = photo['sizes']['z']['displayUrl']
73
+        elif 'n' in photo['sizes']:
74
+            img_src = photo['sizes']['n']['displayUrl']
75
+        elif 'm' in photo['sizes']:
76
+            img_src = photo['sizes']['m']['displayUrl']
77
+        elif 't' in photo['sizes']:
78
+            img_src = photo['sizes']['to']['displayUrl']
79
+        elif 'q' in photo['sizes']:
80
+            img_src = photo['sizes']['q']['displayUrl']
81
+        elif 's' in photo['sizes']:
82
+            img_src = photo['sizes']['s']['displayUrl']
83
+        else:
84
+            continue
85
+        
86
+        url = build_flickr_url(photo['owner']['id'], photo['id'])
87
+
88
+        title = photo['title']
89
+        
90
+        content = '<span class="photo-author">'+ photo['owner']['username'] +'</span><br />'
91
+        
92
+        if 'description' in photo:
93
+            content = content + '<span class="description">' + photo['description'] + '</span>'
94
+
95
+        # append result
96
+        results.append({'url': url,
97
+                        'title': title,
98
+                        'img_src': img_src,
99
+                        'content': content,
100
+                        'template': 'images.html'})
101
+        
102
+    return results

+ 55
- 29
searx/engines/flickr.py 파일 보기

@@ -1,54 +1,80 @@
1 1
 #!/usr/bin/env python
2 2
 
3
+## Flickr (Images)
4
+# 
5
+# @website     https://www.flickr.com
6
+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html) 
7
+# 
8
+# @using-api   yes
9
+# @results     JSON
10
+# @stable      yes
11
+# @parse       url, title, thumbnail, img_src
12
+#More info on api-key : https://www.flickr.com/services/apps/create/
13
+
3 14
 from urllib import urlencode
4
-#from json import loads
5
-from urlparse import urljoin
6
-from lxml import html
7
-from time import time
15
+from json import loads
8 16
 
9 17
 categories = ['images']
10 18
 
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'
19
+nb_per_page = 15
20
+paging = True
21
+api_key= None
22
+
23
+
24
+url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={api_key}&{text}&sort=relevance&extras=description%2C+owner_name%2C+url_o%2C+url_z&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
25
+photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
14 26
 
15 27
 paging = True
16 28
 
29
+def build_flickr_url(user_id, photo_id):
30
+    return photo_url.format(userid=user_id,photoid=photo_id)
31
+
17 32
 
18 33
 def request(query, params):
19
-    params['url'] = search_url.format(query=urlencode({'text': query}),
20
-                                      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'
34
+    params['url'] = url.format(text=urlencode({'text': query}),
35
+                               api_key=api_key,
36
+                               nb_per_page=nb_per_page,
37
+                               page=params['pageno'])
30 38
     return params
31 39
 
32 40
 
33 41
 def response(resp):
34 42
     results = []
35
-    dom = html.fromstring(resp.text)
36
-    for result in dom.xpath(results_xpath):
37
-        img = result.xpath('.//img')
43
+    
44
+    search_results = loads(resp.text)
38 45
 
39
-        if not img:
40
-            continue
46
+    # return empty array if there are no results
47
+    if not 'photos' in search_results:
48
+        return []
49
+
50
+    if not 'photo' in search_results['photos']:
51
+        return []
41 52
 
42
-        img = img[0]
43
-        img_src = 'https:'+img.attrib.get('src')
53
+    photos = search_results['photos']['photo']
44 54
 
45
-        if not img_src:
55
+    # parse results
56
+    for photo in photos:
57
+        if 'url_o' in photo:
58
+            img_src = photo['url_o']
59
+        elif 'url_z' in photo:
60
+            img_src = photo['url_z']
61
+        else:
46 62
             continue
47 63
 
48
-        href = urljoin(url, result.xpath('.//a')[0].attrib.get('href'))
49
-        title = img.attrib.get('alt', '')
50
-        results.append({'url': href,
64
+        url = build_flickr_url(photo['owner'], photo['id'])
65
+
66
+        title = photo['title']
67
+        
68
+        content = '<span class="photo-author">'+ photo['ownername'] +'</span><br />'
69
+        
70
+        content = content + '<span class="description">' + photo['description']['_content'] + '</span>'
71
+        
72
+        # append result
73
+        results.append({'url': url,
51 74
                         'title': title,
52 75
                         'img_src': img_src,
76
+                        'content': content,
53 77
                         'template': 'images.html'})
78
+
79
+    # return results
54 80
     return results

+ 6
- 2
searx/settings.yml 파일 보기

@@ -70,10 +70,14 @@ engines:
70 70
     shortcut : px
71 71
 
72 72
   - name : flickr
73
-    engine : flickr
74 73
     categories : images
75 74
     shortcut : fl
76
-    timeout: 3.0
75
+# You can use the engine using the official stable API, but you need an API key
76
+# See : https://www.flickr.com/services/apps/create/
77
+#    engine : flickr
78
+#    api_key: 'apikey' # required!
79
+# Or you can use the html non-stable engine, activated by default
80
+    engine : flickr-noapi
77 81
 
78 82
   - name : general-file
79 83
     engine : generalfile