coding utf-8 from subprocess import Popen PIPE import sys import re fr

 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
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
import sys
import re
from processor import Processor
from decorators import simple_named_processor
from message import Message
class UrlgrepProcessor(Processor):
limit = 3
@simple_named_processor('urlgrep')
def process(self, frm, text):
if not text:
return ''
else:
splitted = text.split(" ")
url = splitted[0]
pattern = " ".join(splitted[1:])
re_pattern = re.compile(re.escape(pattern), re.I)
data = Popen(["lynx", "-dump", url], stdout=PIPE).communicate()[0]
lines = []
for line in data.splitlines():
if re_pattern.search(line):
lines.append(line)
if len(lines) == self.limit:
break
return res