forked from james-see/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern-example-google.py
72 lines (65 loc) · 2.8 KB
/
pattern-example-google.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
# pattern library example querying Google using the context and terms option to get weights and comparisons
# author: James Campbell
# Date Created: 2015 05 18
# to install pattern, it is simple via pip: pip install pattern
import sys # need this to pass arguments at the command line
from termcolor import colored # awesome color library for printing colored text in the terminal
import argparse
import random
# terminal arguments parser globals - do not change
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store', dest='simple_value',
help='Search term')
parser.add_argument('-c', action='store', dest='context',
help='Set the context term to search on in Google')
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
parser.add_argument('-f', action='store_false', default=False,
dest='boolean_switch',
help='Set a switch to false')
parser.add_argument('-a', action='append', dest='collection',
default=[],
help='Add repeated values to a list',
)
parser.add_argument('-A', action='append_const', dest='const_collection',
const='value-1-to-append',
default=[],
help='Add different values to list')
parser.add_argument('-B', action='append_const', dest='const_collection',
const='value-2-to-append',
help='Add different values to list')
parser.add_argument('--version', action='version', version='%(prog)s 1.1')
results = parser.parse_args()
# check to see if the context term is set at the command line, otherwise set it to dangerous as default
if results.context != None:
contexter = results.context
else: contexter = 'dangerous'
# global dictionary list of terms - do not change
diction = []
subset = []
lengthmin = 6
numterms = 10
fname = 'assets/dictionary-list.html'
with open(fname) as f:
diction = f.readlines()
for term in diction:
if len(term) > lengthmin:
subset.append(term.strip('\n'))
# function to get a random term from the minlength dictionary in subset list
def rando(listofterms,num):
i = 0
while i < num:
randomed = random.choice(listofterms)
#print randomed
searchlist.append(randomed)
i = i + 1
return
searchlist = [] # the list of terms that will be generated in the rando function
# setup the default search terms
rando(subset,numterms) # get total list of terms based on numterms set in the globals section above
from pattern.web import sort
results = sort(terms=searchlist,context=contexter,prefix=True)
for weight, term in results:
print "%.2f" % (weight * 100) + '%', term
exit()