Skip to content

Commit

Permalink
Merge pull request #21 from schrodincat/master
Browse files Browse the repository at this point in the history
Add support for Kinopoisk.ru
  • Loading branch information
deanishe authored Oct 31, 2017
2 parents 9a55de1 + c02afbf commit 0def480
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Binary file added src/icons/kinopoisk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions src/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from hashlib import md5
import urllib
import subprocess
import collections

from workflow import Workflow, web, ICON_ERROR, ICON_SETTINGS

Expand Down Expand Up @@ -589,6 +590,65 @@ def _suggest(self):
return results


class Kinopoisk(Suggest):
"""Get search suggestions from Kinopoisk.ru."""

id_ = 'kinopoisk'
name = 'Kinopoisk.ru'
_suggest_url = 'https://www.kinopoisk.ru/search/suggest'
_search_url = 'https://www.kinopoisk.ru/index.php?kp_query={query}'
_base_url = 'https://www.kinopoisk.ru'
_results_urls = collections.OrderedDict()

def _suggest(self):
response = web.get(self.suggest_url, {'topsuggest': 'true',
'q': self.options['query']})
response.raise_for_status()
raw_results = response.json()
results = collections.OrderedDict()
for d in raw_results:
if (d['year']!="" and d['year']!="0"):
pretty_query = d['rus'].replace(" ", "\u00a0") +" (" + d['year'].replace("–", "\u2013") + ")"
else:
pretty_query = d['rus'].replace(" ", "\u00a0")
results[pretty_query] = d['link']

return results

def url_for(self, query):
if query in self._results_urls:
return self._base_url + self._results_urls[query]

url = self.search_url.encode('utf-8')
options = self.options.copy()
options['query'] = query
for key in options:
if self._quote_plus:
options[key] = urllib.quote_plus(options[key].encode('utf-8'))
else:
options[key] = urllib.quote(options[key].encode('utf-8'))
return url.format(**options)

def search(self):
components = [self.name]
for t in self.options.items():
components.extend(t)
m = md5()
log.debug('key components : {0}'.format(components))
m.update(':'.join(components).encode('utf-8'))
key = m.hexdigest()

cached_results = self.wf.cached_data(key, self._suggest, max_age=600)

self._results_urls = cached_results
results = cached_results.keys()

if self.show_query_in_results:
results = [self.options['query']] + results

return results


def main(wf):
from docopt import docopt

Expand Down

0 comments on commit 0def480

Please sign in to comment.