-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
68 lines (55 loc) · 1.89 KB
/
search.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
import requests
from sys import argv
import re
import json
class github:
def __init__(self, repo):
self.api = "https://api.github.com"
self.repo = repo
pass
def search(self,keyword):
path = "/search/code?q={}+repo:{}".format(keyword,self.repo)
return requests.get(self.api+path).json()
def existing_repository(self):
path = "/repos/{}".format(self.repo)
r = requests.get(self.api+path)
if r.status_code != 200:
return False
return True
class bcolors:
OKGREEN = '\033[92m'
WARNING = '\033[91m'
ENDC = '\033[0m'
def load_keywords():
with open('keywords.txt') as words_file:
keywords = words_file.read().splitlines()
return keywords
def grab_file(files):
for item in files:
for f in files[item]:
r = requests.get(f)
if r.status_code == 200:
pattern = re.compile(r".*"+item+".*")
print("-----")
print("File: {}".format(bcolors.WARNING+f+bcolors.ENDC))
for findings in re.findall(pattern, r.text):
print("{}".format(findings.replace(item,bcolors.OKGREEN+item+bcolors.ENDC)))
if __name__ == '__main__':
g = github(argv[1])
if g.existing_repository():
keywords = load_keywords()
print("Found existing Repository")
print("Looking for keywords: ")
findings = dict()
for i in keywords:
search = g.search(i)
if search["total_count"] != 0:
urls = []
for items in search["items"]:
urls.append(items['html_url'].replace("github.com","raw.githubusercontent.com").replace("/blob",""))
findings.update({
i : urls
})
grab_file(findings)
else:
print("Repository {} does not exist".format(argv[1]))