-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
110 lines (98 loc) · 3.55 KB
/
web.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
#internet stuff search within terminal
# version 2.0
#--- only seach headings as default
#--- option to search all fields
import os
import sys
import argparse
def filterResults(key,line):
# search for the key at the beginning of the line, return true if it's there
searchSize = len(key)
if line[0:searchSize] == key:
return True
else:
return False
def findTop(lineNumbers,fileLocation):
# when field != "#", find how many lines above the field are needed for each found item
newNumbers = []
tempNum = 0
with open(fileLocation, "r") as linefile:
for n in lineNumbers:
linefile.seek(0)
for i,line in enumerate(linefile):
if i < n:
if len(line) < 2:
tempNum = i+2
else:
newNumbers.append(tempNum)
break
return newNumbers
"""
try:
word = sys.argv[1]
except:
print "Usage: 'web <search term> [e]'"
print "Default is to search only titles, 'e' option searches all fields, including titles.\n"
sys.exit()
"""
parser = argparse.ArgumentParser()
parser.add_argument("query", help="string to seach for in internet.txt")
parser.add_argument("-f","--full",help="search full file instead of just titles",action="store_true")
ifile = '/home/matt/Documents/NOTES/internet.txt'
args = parser.parse_args()
word = args.query
#check for a search field (default is title)
############################################################ NOTE
##### THIS DOESN"T WORK AS INTENDED. you either check just the title, or everywhere, no matter what you put as the field
# I "find the top" before checking where the text match was made. may fix this, may not. perhaps title or everywhere is good enough
KEY = "#" #used to denote the titles of entries in the file
try:
field = sys.argv[2]
except:
field = "#"
# search file for the keyword and store results in temp
os.system("grep -in '"+word+"' "+ifile+" >> web_temp")
# get line numbers from temp
lineNumbers = []
tc = ""
with open('web_temp','r') as tfile:
for line in tfile:
for c in line:
if c == ":":
break
else:
tc += c
lineNumbers.append(int(tc))
tc = ""
# adjust line numbers if field is not "#"
if field != "#":
temps = findTop(lineNumbers,ifile)
lineNumbers = temps
#open internet.txt to get appropriate lines
with open(ifile) as internet:
start = False
# look in internet.txt at given line numbers and get that line plus the following until a blank line is reached
print " " #make a space from the terminal line for readability
for n in lineNumbers:
internet.seek(0)
first = True # color and underline the titles
#go to line number n and find how many lines there are before a blank line
for i,line in enumerate(internet):
if i == n-1:
if filterResults(KEY,line):
start = True
else:
break
if start == True:
if len(line) > 1:
if first == True:
print "\033["+str(4)+";"+str(33)+"m"+str(line[1:])+"\033[m",
first = False
else:
print "\b"+line, # the \b is a backspace because the color thing makes an extra space that is ugly and this fixes it
else:
start = False
print "\n"
break
# remove the temp file
os.system("rm web_temp")