Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search suggestions #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN pip install requests_unixsocket
RUN pip install aiohttp
RUN pip install lockfile
RUN pip install diskcache
RUN pip install fuzzywuzzy

RUN wget https://github.com/kaegi/alass/releases/download/v2.0.0/alass-linux64 -O /usr/bin/alass
RUN chmod +x /usr/bin/alass
Expand Down
9 changes: 9 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ def search(searchterm):
return jsonify(results=filtered_results + rest)


@app.route("/api/suggestions/", defaults=dict(searchterm=""))
@app.route("/api/suggestions/<string:searchterm>")
@authorize
def suggestions(searchterm):
import tmdb
results = tmdb.search(searchterm)
return jsonify(results=results)


def _torrent_url_to_magnet(torrent_url):
filepath = "/tmp/" + ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) + ".torrent"
magnet_link = None
Expand Down
10 changes: 9 additions & 1 deletion app/frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@
Vue.component("search-screen", {
template: "#search-screen-template",
data: function () {
return { searchterm: "" };
return { searchterm: "", suggestions: [] };
},
methods: {
onSubmit: function (e) {
Expand Down Expand Up @@ -514,7 +514,15 @@
}
};

var self = this
this.inputListener = function (e) {
$.get("/api/suggestions/" + e.target.value).then(function(result) {
self.suggestions = result.results;
})
};

document.addEventListener("keydown", this.keylistener);
$("#searchinput").on("input", this.inputListener);
},
destroyed: function () {
document.removeEventListener("keydown", this.keylistener);
Expand Down
6 changes: 5 additions & 1 deletion app/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
<script type="text/x-template" id="search-screen-template">
<form @submit="onSubmit">
<div class="input-group mb-3">
<input type="text" autofocus class="form-control" v-model="searchterm" autocorrect="off" autocapitalize="none">
<input id="searchinput" type="search" list="searchterm" autofocus class="form-control" v-model="searchterm" autocorrect="off" autocapitalize="none" autocomplete="on">
<datalist id="searchterm">
<option v-for="suggestion in suggestions" :value="suggestion">{{suggestion}}</option>
<option :value="searchterm">{{searchterm}}</option>
</datalist>
<div class="input-group-append">
<button class="btn btn-light" type="submit"><div class="icon search_icon"/></button>
</div>
Expand Down
2 changes: 2 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
TORRENT_UPLOAD_LIMIT = -1
EXCLUDE_TRACKERS_FROM_TRENDING=[]

TVDB_API_KEY = None

MOVE_RESULTS_TO_BOTTOM_CONTAINING_STRINGS=["XviD"]

# SUBTITLES
Expand Down
37 changes: 37 additions & 0 deletions app/tmdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from fuzzywuzzy import fuzz
from fuzzywuzzy import process

import re
from collections import OrderedDict

import urllib
import requests


API_KEY = "0d77f2a9250ff38a3a22ecb9e20d7a74"
ROOT_URL = "https://api.themoviedb.org/3/search"


def fuzzy_search(search_term, search_list, score_cutoff = 70):
results = process.extract(search_term, search_list, limit=8)
return [r for r in results if r[1] >= score_cutoff]


def remove_duplicates(input_list):
unique_dict = OrderedDict.fromkeys(input_list)
return list(unique_dict.keys())


def search(query):
query = re.sub(r'[^\w\s]', '',query)
tvresponse = requests.get(f"{ROOT_URL}/tv?api_key={API_KEY}&query=" + urllib.parse.quote(query) ).json()["results"]
movieresponse = requests.get(f"{ROOT_URL}/movie?api_key={API_KEY}&query=" + urllib.parse.quote(query) ).json()["results"]
relevant = []
clean_title = lambda r: re.sub(r'[^\w\s\'-]', '', ((r.get("name") or r.get("title"))).lower())
scores = {clean_title(r): r["popularity"] for r in tvresponse + movieresponse}
results = remove_duplicates([clean_title(r) for r in tvresponse + movieresponse])

def sortfun(el):
return -scores[el[0]]*scores[el[0]]*el[1]

return [r[0] for r in sorted(fuzzy_search(query, results), key=sortfun)]