swisscows.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """
  2. Swisscows (Web, Images)
  3. @website https://swisscows.ch
  4. @provide-api no
  5. @using-api no
  6. @results HTML (using search portal)
  7. @stable no (HTML can change)
  8. @parse url, title, content
  9. """
  10. from cgi import escape
  11. from json import loads
  12. from urllib import urlencode, unquote
  13. import re
  14. # engine dependent config
  15. categories = ['general', 'images']
  16. paging = True
  17. language_support = True
  18. # search-url
  19. base_url = 'https://swisscows.ch/'
  20. search_string = '?{query}&page={page}'
  21. # regex
  22. regex_json = re.compile('initialData: {"Request":(.|\n)*},\s*environment')
  23. regex_json_remove_start = re.compile('^initialData:\s*')
  24. regex_json_remove_end = re.compile(',\s*environment$')
  25. regex_img_url_remove_start = re.compile('^https?://i\.swisscows\.ch/\?link=')
  26. # do search-request
  27. def request(query, params):
  28. if params['language'] == 'all':
  29. ui_language = 'browser'
  30. region = 'browser'
  31. else:
  32. region = params['language'].replace('_', '-')
  33. ui_language = params['language'].split('_')[0]
  34. search_path = search_string.format(
  35. query=urlencode({'query': query,
  36. 'uiLanguage': ui_language,
  37. 'region': region}),
  38. page=params['pageno'])
  39. # image search query is something like 'image?{query}&page={page}'
  40. if params['category'] == 'images':
  41. search_path = 'image' + search_path
  42. params['url'] = base_url + search_path
  43. return params
  44. # get response from search-request
  45. def response(resp):
  46. results = []
  47. json_regex = regex_json.search(resp.content)
  48. # check if results are returned
  49. if not json_regex:
  50. return []
  51. json_raw = regex_json_remove_end.sub('', regex_json_remove_start.sub('', json_regex.group()))
  52. json = loads(json_raw)
  53. # parse results
  54. for result in json['Results'].get('items', []):
  55. result_title = result['Title'].replace(u'\uE000', '').replace(u'\uE001', '')
  56. # parse image results
  57. if result.get('ContentType', '').startswith('image'):
  58. img_url = unquote(regex_img_url_remove_start.sub('', result['Url']))
  59. # append result
  60. results.append({'url': result['SourceUrl'],
  61. 'title': escape(result['Title']),
  62. 'content': '',
  63. 'img_src': img_url,
  64. 'template': 'images.html'})
  65. # parse general results
  66. else:
  67. result_url = result['Url'].replace(u'\uE000', '').replace(u'\uE001', '')
  68. result_content = result['Description'].replace(u'\uE000', '').replace(u'\uE001', '')
  69. # append result
  70. results.append({'url': result_url,
  71. 'title': escape(result_title),
  72. 'content': escape(result_content)})
  73. # parse images
  74. for result in json.get('Images', []):
  75. # decode image url
  76. img_url = unquote(regex_img_url_remove_start.sub('', result['Url']))
  77. # append result
  78. results.append({'url': result['SourceUrl'],
  79. 'title': escape(result['Title']),
  80. 'content': '',
  81. 'img_src': img_url,
  82. 'template': 'images.html'})
  83. # return results
  84. return results