Skip to content

Commit

Permalink
add filters for search
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoxia committed May 16, 2015
1 parent 7740742 commit cfdb545
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 22 deletions.
3 changes: 2 additions & 1 deletion ssbc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
url(r'^hash/(.{40})$', web.views.hash),
url(r'^info/(.{40})$', web.views.hash_old),
url(r'^search/(.+?)/(\d*)$', web.views.search_old),
url(r'^list/(.+?)/(\d*)$', web.views.search, name='list'),
url(r'^search/(.+?)$', web.views.search),
url(r'^list/(.+?)/(\d*)$', web.views.search_list, name='list'),
]
2 changes: 1 addition & 1 deletion web/static/js/ssbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $('.x-sform').submit(function(e){
$('.x-kw').focus();
return false;
}
var url = '/list/' + encodeURIComponent(kw) + '/1';
var url = '/list/' + encodeURIComponent(kw) + '/';
window.location = url;
return false;
});
Expand Down
2 changes: 1 addition & 1 deletion web/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<div class="good">
<p>
{% for kw in reclist%}
<a href="/list/{{kw|urlencode}}/1">{{kw}}</a> &nbsp;&nbsp;&nbsp;&nbsp;
<a href="/list/{{kw|urlencode}}/">{{kw}}</a> &nbsp;&nbsp;&nbsp;&nbsp;
{% endfor %}
</p>
<p class="tips2">
Expand Down
2 changes: 1 addition & 1 deletion web/templates/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h4>{{info.name}}</h4>
<th class="col-lg-2">关键词</th>
<td>
{% for kw in keywords %}
<a alt="{{kw}} 磁力搜索" href="/list/{{kw|urlencode}}/1">{{kw}}</a>
<a alt="{{kw}} 磁力搜索" href="/list/{{kw|urlencode}}/">{{kw}}</a>
{% endfor %}
</td>
</tr>
Expand Down
14 changes: 13 additions & 1 deletion web/templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
</div>

{% load filters %}
<h4>共找到 {{result.meta.total}} 条关于 {{keyword}} 的结果,耗时 {{result.meta.time}} 秒.</h4>
<h4>共找到 {{result.meta.total_found}} 条关于 {{keyword}} 的结果,耗时 {{result.meta.time}} 秒.</h4>
<p>
<div class="btn-group" role="group">
{% for x in cats_navs %}
<a class="btn btn-default {% ifequal x.value category%}active{%endifequal%}" href="./?c={{x.value}}&s={{sort}}">{{x.name}} ({{x.num}})</a>
{% endfor %}
</div>
<div class="pull-right btn-group" role="group">
{% for x in sort_navs %}
<a class="btn btn-default {% ifequal x.value sort %}active{%endifequal%}" href="./?c={{category}}&s={{x.value}}">{{x.name}}</a>
{% endfor %}
</div>
</p>
<table class="table">
{% for x in result.items %}
<tr><td class="x-item">
Expand Down
10 changes: 5 additions & 5 deletions web/templates/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
{% if p == 1 %}
<li class="disabled"><a href="#">&larr; Previous</a></li>
{% else %}
<li><a href="{{p|add:-1}}">&larr; Previous </a></li>
<li><a href="./?c={{category}}&s={{sort}}&p={{p|add:-1}}">&larr; Previous </a></li>
{% endif %}

{% for i in prev_pages %}
<li class=""><a href="{{i}}"> {{i}} </a></li>
<li class=""><a href="./?c={{category}}&s={{sort}}&p={{i}}"> {{i}} </a></li>
{% endfor %}

<li class="active"><a href="{{g.p}}"> {{p}} <span class="sr-only">(current)</span></a></li>
<li class="active"><a href="./?c={{category}}&s={{sort}}&p={{g.p}}"> {{p}} <span class="sr-only">(current)</span></a></li>

{% for i in next_pages %}
<li class=""><a href="{{i}}"> {{i}} </a></li>
<li class=""><a href="./?c={{category}}&s={{sort}}&p={{i}}"> {{i}} </a></li>
{% endfor %}

{% if p >= page_max %}
<li class="disabled"><a href="#"> Next &rarr;</a></li>
{% else %}
<li><a href="{{p|add:1}}"> Next &rarr; </a></li>
<li><a href="./?c={{category}}&s={{sort}}&p={{p|add:1}}"> Next &rarr; </a></li>
{% endif %}
</ul>
26 changes: 22 additions & 4 deletions web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import sys
import urllib
import requests
import workers.metautils
from django.http import Http404
from django.shortcuts import render, redirect

API_URL = 'http://127.0.0.1:8001/api/'
API_URL = 'http://70.39.87.34:8001/api/'
API_HOST = 'www.shousibaocai.com'
re_punctuations = re.compile(
u"。|,|,|!|…|!|《|》|<|>|\"|'|:|:|?|\?|、|\||“|”|‘|’|;|—|(|)|·|\(|\)| |\.|【|】|『|』|@|&|%|\^|\*|\+|\||<|>|~|`|\[|\]")
Expand Down Expand Up @@ -39,20 +40,24 @@ def hash(request, h):
return render(request, 'info.html', d)


def search(request, keyword, p):
def search(request, keyword=None, p=None):
d = {'keyword': keyword}
d['words'] = list(set(re_punctuations.sub(u' ', d['keyword']).split()))
try:
d['p'] = int(p)
d['p'] = int(p or request.GET.get('p'))
except:
d['p'] = 1
d['category'] = request.GET.get('c', '')
d['sort'] = request.GET.get('s', 'create_time')
d['ps'] = 10
d['offset'] = d['ps']*(d['p']-1)
# Fetch list
qs = {
'keyword': keyword.encode('utf8'),
'count': d['ps'],
'start': d['offset'],
'category': d['category'],
'sort': d['sort'],
}
url = API_URL + 'json_search?' + urllib.urlencode(qs)
r = req_session.get(url, headers={'Host':API_HOST})
Expand All @@ -77,10 +82,20 @@ def search(request, keyword, p):
x['files'] = [{'path': x['name'], 'length': x['length']}]
# pagination
w = 10
total = int(d['result']['meta']['total'])
total = int(d['result']['meta']['total_found'])
d['page_max'] = total / d['ps'] if total % d['ps'] == 0 else total/d['ps'] + 1
d['prev_pages'] = range( max(d['p']-w+min(int(w/2), d['page_max']-d['p']),1), d['p'])
d['next_pages'] = range( d['p']+1, int(min(d['page_max']+1, max(d['p']-w/2,1) + w )) )
d['sort_navs'] = [
{'name': '按收录时间', 'value': 'create_time'},
{'name': '按文件大小', 'value': 'length'},
{'name': '按相关性', 'value': 'relavance'},
]
d['cats_navs'] = [{'name': '全部', 'num': total, 'value': ''}]
for x in d['cats']['items']:
v = workers.metautils.get_label_by_crc32(x['category'])
d['cats_navs'].append({'value': v, 'name': workers.metautils.get_label(v), 'num': x['num']})

return render(request, 'list.html', d)

def hash_old(request, h):
Expand All @@ -89,3 +104,6 @@ def hash_old(request, h):
def search_old(request, kw, p):
return redirect('list', kw, p)

def search_list(request, kw, p):
return search(request, kw, p)

Empty file added workers/__init__.py
Empty file.
24 changes: 16 additions & 8 deletions workers/metautils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#coding: utf8
import os
import binascii

cats = {
u'video': u'视频',
u'image': u'图片',
u'document': u'书籍',
u'music': u'音乐',
u'package': u'压缩',
u'software': u'软件',
}

def get_label(name):
cats = {
u'video': u'视频',
u'image': u'图片',
u'document': u'书籍',
u'music': u'音乐',
u'package': u'压缩',
u'software': u'软件',
}
if name in cats:
return cats[name]
return u'其它'

def get_label_by_crc32(n):
for k in cats:
if binascii.crc32(k)&0xFFFFFFFFL == n:
return k
return u'other'

def get_extension(name):
return os.path.splitext(name)[1]

Expand Down

0 comments on commit cfdb545

Please sign in to comment.