.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
flickr_noapi.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Flickr (Images)
3
4"""
5
6import json
7from time import time
8import re
9from urllib.parse import urlencode
10from searx.utils import ecma_unescape, html_to_text
11
12# about
13about = {
14 "website": 'https://www.flickr.com',
15 "wikidata_id": 'Q103204',
16 "official_api_documentation": 'https://secure.flickr.com/services/api/flickr.photos.search.html',
17 "use_official_api": False,
18 "require_api_key": False,
19 "results": 'HTML',
20}
21
22# engine dependent config
23categories = ['images']
24paging = True
25time_range_support = True
26safesearch = False
27
28time_range_dict = {
29 'day': 60 * 60 * 24,
30 'week': 60 * 60 * 24 * 7,
31 'month': 60 * 60 * 24 * 7 * 4,
32 'year': 60 * 60 * 24 * 7 * 52,
33}
34image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'm', 'n', 't', 'q', 's')
35
36search_url = 'https://www.flickr.com/search?{query}&page={page}'
37time_range_url = '&min_upload_date={start}&max_upload_date={end}'
38photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
39modelexport_re = re.compile(r"^\s*modelExport:\s*({.*}),$", re.M)
40
41
42def build_flickr_url(user_id, photo_id):
43 return photo_url.format(userid=user_id, photoid=photo_id)
44
45
46def _get_time_range_url(time_range):
47 if time_range in time_range_dict:
48 return time_range_url.format(start=time(), end=str(int(time()) - time_range_dict[time_range]))
49 return ''
50
51
52def request(query, params):
53 params['url'] = search_url.format(query=urlencode({'text': query}), page=params['pageno']) + _get_time_range_url(
54 params['time_range']
55 )
56 return params
57
58
59def response(resp): # pylint: disable=too-many-branches
60 results = []
61
62 matches = modelexport_re.search(resp.text)
63 if matches is None:
64 return results
65
66 match = matches.group(1)
67 model_export = json.loads(match)
68
69 if 'legend' not in model_export:
70 return results
71 legend = model_export['legend']
72
73 # handle empty page
74 if not legend or not legend[0]:
75 return results
76
77 for x, index in enumerate(legend):
78 if len(index) != 8:
79 logger.debug("skip legend enty %s : %s", x, index)
80 continue
81
82 photo = model_export['main'][index[0]][int(index[1])][index[2]][index[3]][index[4]][index[5]][int(index[6])][
83 index[7]
84 ]
85 author = ecma_unescape(photo.get('realname', ''))
86 source = ecma_unescape(photo.get('username', ''))
87 if source:
88 source += ' @ Flickr'
89 title = ecma_unescape(photo.get('title', ''))
90 content = html_to_text(ecma_unescape(photo.get('description', '')))
91 img_src = None
92
93 # From the biggest to the lowest format
94 size_data = None
95 for image_size in image_sizes:
96 if image_size in photo['sizes']['data']:
97 size_data = photo['sizes']['data'][image_size]['data']
98 break
99
100 if not size_data:
101 logger.debug('cannot find valid image size: {0}'.format(repr(photo['sizes']['data'])))
102 continue
103
104 img_src = size_data['url']
105 resolution = f"{size_data['width']} x {size_data['height']}"
106
107 # For a bigger thumbnail, keep only the url_z, not the url_n
108 if 'n' in photo['sizes']['data']:
109 thumbnail_src = photo['sizes']['data']['n']['data']['url']
110 elif 'z' in photo['sizes']['data']:
111 thumbnail_src = photo['sizes']['data']['z']['data']['url']
112 else:
113 thumbnail_src = img_src
114
115 if 'ownerNsid' not in photo:
116 # should not happen, disowned photo? Show it anyway
117 url = img_src
118 else:
119 url = build_flickr_url(photo['ownerNsid'], photo['id'])
120
121 result = {
122 'url': url,
123 'img_src': img_src,
124 'thumbnail_src': thumbnail_src,
125 'source': source,
126 'resolution': resolution,
127 'template': 'images.html',
128 }
129 result['author'] = author.encode(errors='ignore').decode()
130 result['source'] = source.encode(errors='ignore').decode()
131 result['title'] = title.encode(errors='ignore').decode()
132 result['content'] = content.encode(errors='ignore').decode()
133 results.append(result)
134
135 return results
build_flickr_url(user_id, photo_id)