Merge pull request #178 from nyaadevs/infohash-search

Info hash search, hash on torrent page, template adjustments
This commit is contained in:
Anna-Maria Meriniemi 2017-05-25 01:02:04 +03:00 committed by GitHub
commit be0f07d6b8
6 changed files with 44 additions and 17 deletions

View File

@ -205,6 +205,11 @@ class Torrent(db.Model):
def by_info_hash(cls, info_hash):
return cls.query.filter_by(info_hash=info_hash).first()
@classmethod
def by_info_hash_hex(cls, info_hash_hex):
info_hash_bytes = bytearray.fromhex(info_hash_hex)
return cls.by_info_hash(info_hash_bytes)
class TorrentNameSearch(FullText, Torrent):
__fulltext_columns__ = ('display_name',)

View File

@ -182,16 +182,25 @@ def home(rss):
flask.abort(404)
user_id = user.id
user_query = {
special_results = {
'first_word_user': None,
'query_sans_user': None
'query_sans_user': None,
'infohash_torrent': None
}
if search_term:
# Add advanced features to searches (but not RSS or user searches)
if search_term and not render_as_rss and not user_id:
# Check if the first word of the search is an existing user
user_word_match = re.match(r'^([a-zA-Z0-9_-]+) *(.*|$)', search_term)
if user_word_match:
first_word_username = user_word_match.group(1)
user_query['first_word_user'] = models.User.by_username(first_word_username)
user_query['query_sans_user'] = user_word_match.group(2)
special_results['first_word_user'] = models.User.by_username(user_word_match.group(1))
special_results['query_sans_user'] = user_word_match.group(2)
# Check if search is a 40-char torrent hash
infohash_match = re.match(r'(?i)^([a-f0-9]{40})$', search_term)
if infohash_match:
# Check for info hash in database
matched_torrent = models.Torrent.by_info_hash_hex(infohash_match.group(1))
special_results['infohash_torrent'] = matched_torrent
query_args = {
'user': user_id,
@ -210,6 +219,14 @@ def home(rss):
if flask.g.user.is_moderator: # God mode
query_args['admin'] = True
infohash_torrent = special_results.get('infohash_torrent')
if infohash_torrent:
# infohash_torrent is only set if this is not RSS or userpage search
flask.flash(flask.Markup('You were redirected here because '
'the given hash matched this torrent.'), 'info')
# Redirect user from search to the torrent if we found one with the specific info_hash
return flask.redirect(flask.url_for('view_torrent', torrent_id=infohash_torrent.id))
# If searching, we get results from elastic search
use_elastic = app.config.get('USE_ELASTIC_SEARCH')
if use_elastic and search_term:
@ -243,7 +260,7 @@ def home(rss):
torrent_query=query_results,
search=query_args,
rss_filter=rss_query_string,
user_query=user_query)
special_results=special_results)
else:
# If ES is enabled, default to db search for browsing
if use_elastic:
@ -265,7 +282,7 @@ def home(rss):
torrent_query=query,
search=query_args,
rss_filter=rss_query_string,
user_query=user_query)
special_results=special_results)
@app.route('/user/<user_name>', methods=['GET', 'POST'])

View File

@ -57,6 +57,8 @@
</button>
<a class="navbar-brand" href="/">{{ config.SITE_NAME }}</a>
</div>
{% set search_username = (user.username + ("'" if user.username[-1] == 's' else "'s")) if user_page else None %}
{% set search_placeholder = 'Search {} torrents...'.format(search_username) if user_page else 'Search...' %}
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li {% if request.path == "/upload" %} class="active"{% endif %}><a href="/upload">Upload</a></li>
@ -193,11 +195,10 @@
{% else %}
<form class="navbar-form navbar-right form" action="/" method="get">
{% endif %}
<input type="text" class="form-control" name="q" placeholder="Search..." value="{{ search["term"] if search is defined else '' }}">
<br>
<input type="text" class="form-control" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}">
<br>
<select class="form-control" title="Filter" data-width="120px" name="f">
<option value="0" title="No filter" {% if search is defined and search["quality_filter"] == "0" %}selected{% else %}selected{% endif %}>No filter</option>
<option value="1" title="No remakes" {% if search is defined and search["quality_filter"] == "1" %}selected{% endif %}>No remakes</option>
@ -231,7 +232,7 @@
<form class="navbar-form navbar-right form" action="/" method="get">
{% endif %}
<div class="input-group search-container hidden-xs hidden-sm">
<input type="text" class="form-control search-bar" name="q" placeholder="Search..." value="{{ search["term"] if search is defined else '' }}">
<input type="text" class="form-control search-bar" name="q" placeholder="{{ search_placeholder }}" value="{{ search["term"] if search is defined else '' }}">
<div class="input-group-btn nav-filter" id="navFilter-criteria">
<select class="selectpicker show-tick" title="Filter" data-width="120px" name="f">

View File

@ -9,10 +9,10 @@
</th>
{% endmacro %}
{% if user_query is defined %}
{% if user_query.first_word_user and not search.user %}
{% if special_results is defined and not search.user %}
{% if special_results.first_word_user %}
<div class="alert alert-info">
<a href="/user/{{ user_query.first_word_user.username }}{{ modify_query(q=user_query.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ user_query.first_word_user.username }}</a>
<a href="/user/{{ special_results.first_word_user.username }}{{ modify_query(q=special_results.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ special_results.first_word_user.username }}</a>
</div>
{% endif %}
{% endif %}

View File

@ -41,7 +41,7 @@
{% endif %}
<h3>
Browsing <span class="text-{{ user.userlevel_color }}">{{ user.username }}</span>'s torrents
Browsing <span class="text-{{ user.userlevel_color }}">{{ user.username }}</span>'{{ '' if user.username[-1] == 's' else 's' }} torrents
</h3>
{% include "search_results.html" %}

View File

@ -62,6 +62,10 @@
<div class="col-md-1">Completed:</div>
<div class="col-md-5">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.download_count }}{% else %}Coming soon{% endif %}</div>
</div>
<div class="row">
<div class="col-md-offset-6 col-md-1">SHA-1 hash:</div>
<div class="col-md-5"><kbd>{{ torrent.info_hash_as_hex }}</kbd></div>
</div>
</div>
<div class="panel-footer">
{% if torrent.has_torrent %}<a href="/view/{{ torrent.id }}/torrent"><i class="fa fa-download fa-fw"></i>Download Torrent</a> or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i class="fa fa-magnet fa-fw"></i>Magnet</a>