瀏覽代碼

update piratebay engine and add comments

Thomas Pointhuber 10 年之前
父節點
當前提交
dae88d862b
共有 2 個檔案被更改,包括 34 行新增8 行删除
  1. 34
    7
      searx/engines/piratebay.py
  2. 0
    1
      searx/settings.yml

+ 34
- 7
searx/engines/piratebay.py 查看文件

1
+## Piratebay (Videos, Music, Files)
2
+# 
3
+# @website     https://thepiratebay.se
4
+# @provide-api no (nothing found)
5
+# 
6
+# @using-api   no
7
+# @results     HTML (using search portal)
8
+# @stable      yes (HTML can change)
9
+# @parse       url, title, content, seed, leech, magnetlink
10
+
1
 from urlparse import urljoin
11
 from urlparse import urljoin
2
 from cgi import escape
12
 from cgi import escape
3
 from urllib import quote
13
 from urllib import quote
4
 from lxml import html
14
 from lxml import html
5
 from operator import itemgetter
15
 from operator import itemgetter
6
 
16
 
7
-categories = ['videos', 'music']
17
+# engine dependent config
18
+categories = ['videos', 'music', 'files']
19
+paging = True
8
 
20
 
21
+# search-url
9
 url = 'https://thepiratebay.se/'
22
 url = 'https://thepiratebay.se/'
10
 search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
23
 search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
11
-search_types = {'videos': '200',
24
+
25
+# piratebay specific type-definitions
26
+search_types = {'files': '0',                
12
                 'music': '100',
27
                 'music': '100',
13
-                'files': '0'}
28
+                'videos': '200'}
14
 
29
 
30
+# specific xpath variables
15
 magnet_xpath = './/a[@title="Download this torrent using magnet"]'
31
 magnet_xpath = './/a[@title="Download this torrent using magnet"]'
16
 content_xpath = './/font[@class="detDesc"]//text()'
32
 content_xpath = './/font[@class="detDesc"]//text()'
17
 
33
 
18
-paging = True
19
-
20
 
34
 
35
+# do search-request
21
 def request(query, params):
36
 def request(query, params):
22
-    search_type = search_types.get(params['category'], '200')
37
+    search_type = search_types.get(params['category'], '0')
38
+
23
     params['url'] = search_url.format(search_term=quote(query),
39
     params['url'] = search_url.format(search_term=quote(query),
24
                                       search_type=search_type,
40
                                       search_type=search_type,
25
                                       pageno=params['pageno'] - 1)
41
                                       pageno=params['pageno'] - 1)
42
+
26
     return params
43
     return params
27
 
44
 
28
 
45
 
46
+# get response from search-request
29
 def response(resp):
47
 def response(resp):
30
     results = []
48
     results = []
49
+
31
     dom = html.fromstring(resp.text)
50
     dom = html.fromstring(resp.text)
51
+
32
     search_res = dom.xpath('//table[@id="searchResult"]//tr')
52
     search_res = dom.xpath('//table[@id="searchResult"]//tr')
33
 
53
 
54
+    # return empty array if nothing is found
34
     if not search_res:
55
     if not search_res:
35
-        return results
56
+        return []
36
 
57
 
58
+    # parse results
37
     for result in search_res[1:]:
59
     for result in search_res[1:]:
38
         link = result.xpath('.//div[@class="detName"]//a')[0]
60
         link = result.xpath('.//div[@class="detName"]//a')[0]
39
         href = urljoin(url, link.attrib.get('href'))
61
         href = urljoin(url, link.attrib.get('href'))
41
         content = escape(' '.join(result.xpath(content_xpath)))
63
         content = escape(' '.join(result.xpath(content_xpath)))
42
         seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
64
         seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
43
 
65
 
66
+        # convert seed to int if possible
44
         if seed.isdigit():
67
         if seed.isdigit():
45
             seed = int(seed)
68
             seed = int(seed)
46
         else:
69
         else:
47
             seed = 0
70
             seed = 0
48
 
71
 
72
+        # convert leech to int if possible
49
         if leech.isdigit():
73
         if leech.isdigit():
50
             leech = int(leech)
74
             leech = int(leech)
51
         else:
75
         else:
52
             leech = 0
76
             leech = 0
53
 
77
 
54
         magnetlink = result.xpath(magnet_xpath)[0]
78
         magnetlink = result.xpath(magnet_xpath)[0]
79
+
80
+        # append result
55
         results.append({'url': href,
81
         results.append({'url': href,
56
                         'title': title,
82
                         'title': title,
57
                         'content': content,
83
                         'content': content,
60
                         'magnetlink': magnetlink.attrib['href'],
86
                         'magnetlink': magnetlink.attrib['href'],
61
                         'template': 'torrent.html'})
87
                         'template': 'torrent.html'})
62
 
88
 
89
+    # return results sorted by seeder
63
     return sorted(results, key=itemgetter('seed'), reverse=True)
90
     return sorted(results, key=itemgetter('seed'), reverse=True)

+ 0
- 1
searx/settings.yml 查看文件

82
 
82
 
83
   - name : piratebay
83
   - name : piratebay
84
     engine : piratebay
84
     engine : piratebay
85
-    categories : videos, music, files
86
     shortcut : tpb
85
     shortcut : tpb
87
 
86
 
88
   - name : soundcloud
87
   - name : soundcloud