-
Notifications
You must be signed in to change notification settings - Fork 37
/
search_aj
executable file
·78 lines (62 loc) · 2.16 KB
/
search_aj
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
#!/usr/bin/env python
#coding=UTF-8
## 搜索Java资源,https://github.com/superhj1987/awesome-tech-collections/blob/master/awesome-java.md
import sys
import getopt
import urllib2
reload(sys)
sys.setdefaultencoding( "utf-8" )
TIPS_TPL = "\n" + '=' * 8 + "%s" + '=' * 8 + "\n"
def usage():
print "usage: search_aj [catagory_name | project_name]"
def progress_report(bytes_so_far, chunk_size, total_size):
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
sys.stdout.write("searching.... (%0.2f%%)\r" %
(percent))
sys.stdout.flush()
if bytes_so_far >= total_size:
sys.stdout.write('\n')
def main():
opts,args = getopt.getopt(sys.argv[1:],'',[])
argc = len(args)
if argc < 1:
usage()
sys.exit(1)
search_name = args[0]
req = urllib2.Request("https://raw.githubusercontent.com/superhj1987/awesome-tech-collections/master/awesome-java.md")
res = urllib2.urlopen(req)
total_size = res.info().getheader('Content-Length').strip()
total_size = int(total_size)
chunk_size = 1024
bytes_so_far = 0
data = []
while 1:
chunk = res.read(chunk_size)
bytes_so_far += len(chunk)
if not chunk:
break
data += chunk
progress_report(bytes_so_far, chunk_size, total_size)
content = "".join(data)
category = ""
target_cat = None
search_result = ""
lines = content.split("\n")
search_name = search_name.lower()
for line in lines:
if line.startswith("##") :
target_cat = None
category = line[line.index(" "):].strip().lower()
if category.find(search_name) > 0:
search_result += TIPS_TPL % ('finded in Categoty[' + category + ']')
target_cat = category
elif line.startswith("* ["):
project = line[line.index("["):].strip().lower()
if target_cat:
search_result += project + "\n"
elif project.find(search_name) > 0:
search_result += TIPS_TPL % ('find matched projects in Category[' + category + ']') + project + "\n"
print search_result
if __name__ == "__main__":
main()