.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
flickr.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 Flickr (Images)
4
5 More info on api-key : https://www.flickr.com/services/apps/create/
6"""
7
8from json import loads
9from urllib.parse import urlencode
10
11# about
12about = {
13 "website": 'https://www.flickr.com',
14 "wikidata_id": 'Q103204',
15 "official_api_documentation": 'https://secure.flickr.com/services/api/flickr.photos.search.html',
16 "use_official_api": True,
17 "require_api_key": True,
18 "results": 'JSON',
19}
20
21categories = ['images']
22
23nb_per_page = 15
24paging = True
25api_key = None
26
27
28url = (
29 'https://api.flickr.com/services/rest/?method=flickr.photos.search'
30 + '&api_key={api_key}&{text}&sort=relevance'
31 + '&extras=description%2C+owner_name%2C+url_o%2C+url_n%2C+url_z'
32 + '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
33)
34photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
35
36paging = True
37
38
39def build_flickr_url(user_id, photo_id):
40 return photo_url.format(userid=user_id, photoid=photo_id)
41
42
43def request(query, params):
44 params['url'] = url.format(
45 text=urlencode({'text': query}), api_key=api_key, nb_per_page=nb_per_page, page=params['pageno']
46 )
47 return params
48
49
50def response(resp):
51 results = []
52
53 search_results = loads(resp.text)
54
55 # return empty array if there are no results
56 if 'photos' not in search_results:
57 return []
58
59 if 'photo' not in search_results['photos']:
60 return []
61
62 photos = search_results['photos']['photo']
63
64 # parse results
65 for photo in photos:
66 if 'url_o' in photo:
67 img_src = photo['url_o']
68 elif 'url_z' in photo:
69 img_src = photo['url_z']
70 else:
71 continue
72
73 # For a bigger thumbnail, keep only the url_z, not the url_n
74 if 'url_n' in photo:
75 thumbnail_src = photo['url_n']
76 elif 'url_z' in photo:
77 thumbnail_src = photo['url_z']
78 else:
79 thumbnail_src = img_src
80
81 # append result
82 results.append(
83 {
84 'url': build_flickr_url(photo['owner'], photo['id']),
85 'title': photo['title'],
86 'img_src': img_src,
87 'thumbnail_src': thumbnail_src,
88 'content': photo['description']['_content'],
89 'author': photo['ownername'],
90 'template': 'images.html',
91 }
92 )
93
94 # return results
95 return results
request(query, params)
Definition flickr.py:43
build_flickr_url(user_id, photo_id)
Definition flickr.py:39