forked from flepied/xgoogle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautosearch.py
198 lines (172 loc) · 5.46 KB
/
autosearch.py
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
#!/bin/python
## coding=utf-8 ##
#-*- encoding: utf8 -*-
### handle options
import sys, getopt, re, os
KEYWORD=""
ENGINE=[]
INTERNAL=0
NUM=10
#PREFERENCE=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autosearch.conf')
PREFERENCE='autosearch.conf'
FORMAT=['title', 'url', 'desc']
WRITE="autosearch.output"
FILTER=""
CHART=""
SORT=""
VERBOSE=False
ENGINES=['google', 'baidu', 'qihoo', 'maopu', 'tianya', 'weibo', 'tq']
FORMATS=['title', 'url', 'desc']
DATA=[]
ENCODING = sys.getfilesystemencoding()
def opt():
global KEYWORD, ENGINE, INTERNAL, NUM, PREFERENCE, FORMAT, WRITE, FILTER, CHART, SORT, VERBOSE
global ENGINES, FORMATS
blank = re.compile('\s')
opts, args = getopt.getopt(sys.argv[1:], "k:e:i:n:p:o:w:f:c:s:vh",
["keyword=", "engine=", "internal=", "num=", "preference=", "format=", "write=", "filter=", "chart=", "sort=", "verbose", "help"])
for op, value in opts:
if op == "-k" or op == "--keyword":
KEYWORD = value.strip()
elif op == "-e" or op == "--engine":
ENGINE = blank.sub('', value).split(',')
for e in ENGINE:
if not e in ENGINES:
error( "warning: %s is not supported, ignored" % e )
ENGINE.remove(e)
else:
DATA.append([e, [[0,0]]])
elif op == "-i" or op == "--internal":
INTERNAL = float(value)
elif op == "-n" or op == "--num":
NUM = int(value)
elif op == "-p" or op == "--preference":
PREFERENCE = value
elif op == "-o" or op == "--format":
FORMAT = blank.sub('', value).split(',')
for f in FORMAT:
if not f in FORMATS:
error( "warning: format %s is not supported, ignored" % f )
FORMAT.remove(f)
elif op == "-w" or op == "--write":
WRITE = value
elif op == "-f" or op == "--filter":
FILTER = value
elif op == "-c" or op == "--chart":
CHART = value
elif op == "-s" or op == "--sort":
SORT = value
elif op == "-v" or op == "--verbose":
VERBOSE = True
elif op == "-h" or op == "--help":
usage()
sys.exit()
else:
error( "warning: option %s not recognized, ignored" % op )
def usage():
print """autosearch is an automatically tools used in command line, the usage:"""
print """autosearch
-k,--keyword=keyword ---if have blank,put the keyword in "",like this "cars tree"
-e,--engine=google,baidu,qihoo,maopu,tianya,weibo,tq ---at least one of these search engines
-i,--internal=NUMBER ---seconds, default is 0
-n,--num=NUMBER ---topmost search results, default is 10
-p,--preference=FILE ---set preference file name, default is autosearch.conf
-o,--format=title,url,desc ---set output formate, default is title,url,desc
-w,--write=FILE ---set output file, default is autosearch.output
-f,--filter=STRING ---set filter string
-c,--chart ---got output chart
-s,--sort ---set sort type
-v,--verbose ---if set, print the output to screen also
-h,--help ---this help information"""
def error(msg):
print 50*'*'
print "*** %s" % msg
print 50*'*'
usage()
sys.exit()
def try_output(content):
#s = str.format( content )
if VERBOSE:
print content
if WRITE:
with open(WRITE, 'a+') as f:
f.write(content.encode('utf-8'))
f.write("\n")
import cairo
import pycha
import pycha.bar
def try_chart():
global DATA
#print DATA
if CHART=='':
return
width, height = (500,400)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
chart = pycha.bar.VerticalBarChart(surface)
chart.addDataset( DATA )
chart.render()
surface.write_to_png('output.png')
try:
opt()
except Exception, e:
error( "error: %s" % e )
usage()
sys.exit()
else:
pass
finally:
pass
if KEYWORD=='':
error( "error: no keyword to query, exit!!!" )
if len(ENGINE)==0:
error( "error: must assign at least 1 search engine!!!" )
if not os.path.exists(PREFERENCE):
error( "error: preference file %s not exists!!!" % PREFERENCE )
if WRITE:
with open(WRITE, 'w', ) as f:
pass
### main logic
from xgoogle.GeneralSearch import GeneralSearch
import time
import string
import time
start_time = 1
while True:
for e in ENGINE:
gs=GeneralSearch(KEYWORD.decode(ENCODING), e, PREFERENCE)
results = gs.get_results()
print gs.num_results
#print gs._last_search_url
#s1 = str.format( "%s: %d results of \"" % ( e.upper(), gs.num_results ) )
#s2 = str.format( "\" --- %s" % ( time.strftime("%Y-%m-%d %X", time.localtime())) )
#s = s1 + KEYWORD.decode(ENCODING) + s2
#try_output( s )
try_output( "%s: %d results of \"%s\" --- %s" % ( e.upper(), gs.num_results, KEYWORD.decode(ENCODING), time.strftime("%Y-%m-%d %X", time.localtime())) )
try_output( 80*'-' )
index = ENGINE.index(e)
if DATA[index][0]==e:
#DATA[index][1].append([time.strftime("%Y-%m-%d %X", time.localtime()), gs.num_results])
DATA[index][1].append([start_time, gs.num_results])
count=1
while True:
for r in results:
if count>NUM:
break
try_output( "results[%d]: " % count )
for k in FORMAT:
s = "r.%s" % k
c = eval( s )
try_output( "%s" % c )
try_output( 80*'+' )
count=count+1
if count>NUM:
break
results = gs.get_results()
del gs
try_chart()
if INTERNAL:
print "info: loop search every %d seconds, press CTRL+C to exit." % INTERNAL
time.sleep(string.atof(INTERNAL))
start_time += INTERNAL
else:
sys.exit()