diff options
Diffstat (limited to 'searx/engines')
| -rw-r--r-- | searx/engines/9gag.py | 77 | ||||
| -rw-r--r-- | searx/engines/apple_maps.py | 113 |
2 files changed, 190 insertions, 0 deletions
diff --git a/searx/engines/9gag.py b/searx/engines/9gag.py new file mode 100644 index 000000000..d1846725c --- /dev/null +++ b/searx/engines/9gag.py @@ -0,0 +1,77 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +# pylint: disable=invalid-name +"""9GAG (social media)""" + +from json import loads +from datetime import datetime +from urllib.parse import urlencode + +about = { + "website": 'https://9gag.com/', + "wikidata_id": 'Q277421', + "official_api_documentation": None, + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +categories = ['social media'] +paging = True + +search_url = "https://9gag.com/v1/search-posts?{query}" +page_size = 10 + + +def request(query, params): + query = urlencode({'query': query, 'c': (params['pageno'] - 1) * page_size}) + + params['url'] = search_url.format(query=query) + + return params + + +def response(resp): + results = [] + + json_results = loads(resp.text)['data'] + + for result in json_results['posts']: + result_type = result['type'] + + # Get the not cropped version of the thumbnail when the image height is not too important + if result['images']['image700']['height'] > 400: + thumbnail = result['images']['imageFbThumbnail']['url'] + else: + thumbnail = result['images']['image700']['url'] + + if result_type == 'Photo': + results.append( + { + 'template': 'images.html', + 'url': result['url'], + 'title': result['title'], + 'content': result['description'], + 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), + 'img_src': result['images']['image700']['url'], + 'thumbnail_src': thumbnail, + } + ) + elif result_type == 'Animated': + results.append( + { + 'template': 'videos.html', + 'url': result['url'], + 'title': result['title'], + 'content': result['description'], + 'publishedDate': datetime.utcfromtimestamp(result['creationTs']), + 'thumbnail': thumbnail, + 'iframe_src': result['images'].get('image460sv', {}).get('url'), + } + ) + + if 'tags' in json_results: + for suggestion in json_results['tags']: + results.append({'suggestion': suggestion['key']}) + + return results diff --git a/searx/engines/apple_maps.py b/searx/engines/apple_maps.py new file mode 100644 index 000000000..eb4af422e --- /dev/null +++ b/searx/engines/apple_maps.py @@ -0,0 +1,113 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Apple Maps""" + +from json import loads +from time import time +from urllib.parse import urlencode + +from searx.network import get as http_get +from searx.engines.openstreetmap import get_key_label + +about = { + "website": 'https://www.apple.com/maps/', + "wikidata_id": 'Q276101', + "official_api_documentation": None, + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +token = {'value': '', 'last_updated': None} + +categories = ['map'] +paging = False + +search_url = "https://api.apple-mapkit.com/v1/search?{query}&mkjsVersion=5.72.53" + + +def obtain_token(): + update_time = time() - (time() % 1800) + try: + # use duckduckgo's mapkit token + token_response = http_get('https://duckduckgo.com/local.js?get_mk_token=1', timeout=2.0) + actual_token = http_get( + 'https://cdn.apple-mapkit.com/ma/bootstrap?apiVersion=2&mkjsVersion=5.72.53&poi=1', + timeout=2.0, + headers={'Authorization': 'Bearer ' + token_response.text}, + ) + token['value'] = loads(actual_token.text)['authInfo']['access_token'] + token['last_updated'] = update_time + # pylint: disable=bare-except + except: + pass + return token + + +def request(query, params): + if time() - (token['last_updated'] or 0) > 1800: + obtain_token() + + params['url'] = search_url.format(query=urlencode({'q': query, 'lang': params['language']})) + + params['headers'] = {'Authorization': 'Bearer ' + token['value']} + + return params + + +def response(resp): + results = [] + + resp_json = loads(resp.text) + + user_language = resp.search_params['language'] + + for result in resp_json['results']: + boundingbox = None + if 'displayMapRegion' in result: + box = result['displayMapRegion'] + boundingbox = [box['southLat'], box['northLat'], box['westLng'], box['eastLng']] + + links = [] + if 'telephone' in result: + telephone = result['telephone'] + links.append( + { + 'label': get_key_label('phone', user_language), + 'url': 'tel:' + telephone, + 'url_label': telephone, + } + ) + if result.get('urls'): + url = result['urls'][0] + links.append( + { + 'label': get_key_label('website', user_language), + 'url': url, + 'url_label': url, + } + ) + + results.append( + { + 'template': 'map.html', + 'type': result.get('poiCategory'), + 'title': result['name'], + 'links': links, + 'latitude': result['center']['lat'], + 'longitude': result['center']['lng'], + 'url': result['placecardUrl'], + 'boundingbox': boundingbox, + 'geojson': {'type': 'Point', 'coordinates': [result['center']['lng'], result['center']['lat']]}, + 'address': { + 'name': result['name'], + 'house_number': result.get('subThoroughfare'), + 'road': result.get('thoroughfare'), + 'locality': result.get('locality'), + 'postcode': result.get('postCode'), + 'country': result.get('country'), + }, + } + ) + + return results |