-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathczsearch.cgi
More file actions
executable file
·277 lines (241 loc) · 6.4 KB
/
czsearch.cgi
File metadata and controls
executable file
·277 lines (241 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'yaml'
require 'cgi'
class Logger
QUIET = 0
WARN = 1
INFO = 2
DEBUG = 3
@@level = Logger::DEBUG
@@messages = []
def Logger.level
return @@level
end
def Logger.level=(new_level)
@@level = new_level
end
def Logger.log(level, message)
if level == 0
raise 'Sending zero level log messages is prohibited! Zero is meant for quiet behavior'
end
if level <= @@level
@@messages << message
end
end
def Logger.messages
return @@messages
end
end
class CZShareParser
def initialize()
@line_regexps = {
'name'=>Regexp.new('<a[^>]+>(.*)</a>'),
'url'=>Regexp.new('<a href="([^"]+)"'),
'size'=>Regexp.new('<td class="col4">(.*)</td>'),
'desc'=>Regexp.new("</a>[ \t]*(.*?)[ \t]*</td>")
}
@next_regexp = Regexp.new('<a href="([^"]+)" class="btn-next">')
end
def parse_line(line)
parsed = {}
@line_regexps.each_key { |k|
m = @line_regexps[k].match(line)
if m
parsed[k] = m[1].strip
else
parsed[k] = ''
end
}
if parsed['name'].empty?
Logger.log(Logger::WARN, line)
end
#parsed['name'] = 'ERROR - ' + parsed['url'] if parsed['name'].empty?
return parsed
end
def find_next(line)
url = @next_regexp.match(line)
if url:
if url[1].include? 't=txt'
Logger.log(Logger::INFO, 'found next url: '+url.to_s)
if not url[1][0] == '/'
return '/'+url[1]
end
return url[1]
end
end
return nil
end
end
class CZShare
def initialize()
@server = 'czshare.com'
@parser = CZShareParser.new
@con = Net::HTTP.new(@server, 80)
end
def parse_page(lines)
Logger.log(Logger::INFO, 'parsing page with '+lines.size.to_s+' lines')
Logger.log(Logger::INFO, lines.to_s.length.to_s)
waiting_for_table = false
started = false
downloads = []
next_url = nil
lines.each do |line|
#line.strip!
if not next_url
next_url = @parser.find_next(line)
end
if line.include? 'id="tab-table-all"'
waiting_for_table = true
end
if waiting_for_table and line.include? '<table>'
waiting_for_table = false
started = true
end
if started and line.include? 'col2'
downloads << @parser.parse_line(line)
end
if started and line.include? '</table>'
started = false
break
end
end
return {'downloads'=>downloads, 'next'=>next_url }
end
def load_search(page_url)
Logger.log(Logger::INFO, 'loading page ' + page_url)
page_source = ''
@con.get(page_url) { |body|
page_source += body
}
return parse_page(page_source.split("\n"))
end
def search(term)
Logger.log(Logger::INFO, 'searching for '+term)
downloads = []
next_url = '/search.php?q='+URI.escape(term)
while next_url
res = load_search(next_url)
downloads = downloads + res['downloads']
next_url = res['next']
end
#downloads.sort(key=lambda d: d['name'])
downloads.sort! { |a,b| a['name'] <=> b['name'] }
return downloads
end
end
class Printer
def initialize
@use_html = true
end
def use_html
return @use_html
end
def use_html=(should_use_html)
@use_html = should_use_html
end
def out(txt)
puts(txt)
end
def print_downloads(downloads)
out('<div class="downloads">')
out("<div class=\"count\">Found #{downloads.size} entries (displayed <span id=\"displayedCount\">#{downloads.size}</span>):</div>")
downloads.each_with_index do |down, idx|
out('<div class="download">' + idx.to_s + ') ' +
"<a target=\"_blank\" href=\"#{down['url']}\">#{down['name']}</a>" +
' - '+down['size']+' - '+down['desc'] +
'</div>')
$stdout.flush
end
out('</div>')
end
def print_header(term)
return if not @use_html
out('Content-Type: text/html')
out('')
out('<! DOCTYPE html >')
out('<html>')
out('<head>')
out('<meta charset="utf-8"><title>CZShare Search</title>')
out('<style>')
out('.error { color:red; }')
out('.download { font-size:11px; }')
out('.log { color: #ccc; background-color:#333; font-size:x-small; margin-top: 2em; border-top: 1px solid gray; }')
out('.log { height: 1.1em; overflow:hidden;}')
out('.log:hover { height: auto; }')
out('</style>')
out('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>')
out('<script type="text/javascript">')
out('$(document).ready(function() {')
out(' var filterInput = $("#filter");')
out(' var filterTimeOut = 0;')
out(' var lastFilter = "";')
out(' var allDown = $(".download");')
out(' var displayedCount = $("#displayedCount");')
out(' var doFilter = function () { ')
out(' var fv = $.trim(filterInput.val()).toLowerCase();')
out(' if( fv == lastFilter ) { return; } else { lastFilter = fv; }')
out(' var displ = 0;')
out(' allDown.each(function(){')
out(' var d = $(this);')
out(' if(!d.attr("textLower")) { d.attr("textLower", d.text().toLowerCase()); }')
out(' if(fv == "" || d.attr("textLower").indexOf(fv) != -1) {')
out(' d.show();')
out(' displ++;')
out(' } else { ')
out(' d.hide();')
out(' }')
out(' });')
out(' displayedCount.text(displ);')
out(' };')
out(' var requestFilter = function () { ')
out(' clearTimeout(filterTimeOut);')
out(' filterTimeOut = setTimeout(doFilter, 250);')
out(' };')
out(' filterInput.bind("change", requestFilter);')
out(' filterInput.bind("input", requestFilter);')
out(' filterInput.bind("keyup", requestFilter);')
out('});')
out('</script>')
out('</head>')
out('<body><h1>CZShare Search</h1>')
out('<form action="" method="post"><div>')
out('<label for="term">What to look for?</label>')
out('<input type="text" name="term" id="term" value="'+term+'" />')
out('<input type="submit" value="Search" />')
out('</div></form>')
out('<div>Filter results: <input type="text" id="filter" /></div>');
$stdout.flush
end
def print_footer
print_logs
return if not @use_html
out('</body>')
out('</html>')
end
def print_logs
out('<div class="log">')
out('Progress log:<br />')
Logger.messages.each do |msg|
out('<div class="message">')
out(CGI.escapeHTML(msg))
out('</div>')
end
out('</div>')
end
end
cgi = CGI.new
czs = CZShare.new
printer = Printer.new
if cgi.key? 'plaintext'
printer.use_html = false
end
printer.print_header cgi['term']
if cgi.key?('inputfile')
p czs.parse_page(open(cgi['inputfile']))
elsif cgi.key?('term')
downloads = czs.search(cgi['term'])
printer.print_downloads(downloads)
end
printer.print_footer