forked from pratraut/scrapyFi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapyfi.py
318 lines (278 loc) · 14.5 KB
/
scrapyfi.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from bs4 import BeautifulSoup
import requests, os, json, argparse
from lib.helper import *
from lib.github_downloader import *
import terminal_banner, termcolor, platform, time
from rich.console import Console
from rich.table import Table
COLOR = '\033[32m'
banner_text = f"""{COLOR}
'||''''| ||
.... .... ... .. .... ... ... .... ... || . ...
||. ' .| '' ||' '' '' .|| ||' || '|. | ||''| ||
. '|.. || || .|' || || | '|.| || ||
|'..|' '|...' .||. '|..'|' ||...' '| .||. .||.
|| .. |
'''' ''
Author: savi0ur
"""
desc = "Helps in Searching and Downloading contracts of a program from Immunefi"
dev_info = """
v1.0
"""
if(platform.system() == 'Windows'):
os.system('cls')
if(platform.system().lower() in ['linux', 'darwin']):
os.system('clear')
banner = terminal_banner.Banner(banner_text)
print(termcolor.colored(banner.text,'cyan'), end="")
print(termcolor.colored(desc,'white', attrs=['bold']), end = "")
print(termcolor.colored(dev_info,'yellow'))
argp = argparse.ArgumentParser()
argp.add_argument("-t", "--timeout", type=int, help="timeout for each request in seconds (default: 10 sec)", default=10)
sub_argp = argp.add_subparsers(help="Commands")
# List
list_argp = sub_argp.add_parser("list", help="List programs")
list_argp.add_argument("list_programs", help="List all the programs with basic info", action="store_true")
list_argp.add_argument("-lcl", "--least-contract-link", help="list projects by least contract link", action="store_true")
list_argp.add_argument("-lgl", "--least-github-link", help="list projects by least github link", action="store_true")
list_argp.add_argument("-lol", "--least-other-link", help="list projects by least other link", action="store_true")
list_argp.add_argument("-ltl", "--least-total-link", help="list projects by least total link", action="store_true")
list_argp.add_argument("-ltc", "--least-total-contracts", help="list projects by least total contracts", action="store_true")
# list_argp.add_argument("-t", "--test", help="test", action="store_true")
# Search
search_argp = sub_argp.add_parser("search", help="Search programs")
search_argp.add_argument("-q", "--query", help="Query particular program by its name. Ex. MakerDAO", required=True)
search_argp.add_argument("-d", "--download", help="Download all contracts code from queried program", action="store_true")
search_argp.add_argument("-f", "--filter", help="Filter results of a queried program", required=False)
# Download
download_argp = sub_argp.add_parser("download", help="Download code from link")
download_argp.add_argument("links", help="Download all contracts code from provided links (space separated)", nargs='+')
download_argp.add_argument("-fn", "--folder-name", help="Folder name to store contracts", default="Custom Downloads")
parser = vars(argp.parse_args())
# Sets the environment variable for timeout
os.environ['TIMEOUT'] = str(parser['timeout'])
# print(parser)
def get_dollar_repr(num):
return "${:,}".format(num)
def get(obj, id):
if id in obj and obj[id]:
return obj[id]
return None
def get_assets(assets, filter=None):
new_assets = {}
new_assets['github'] = []
new_assets['contract'] = []
new_assets['other'] = []
if not assets:
return new_assets
for asset in assets:
if filter and filter not in asset['url']:
continue
if 'github' in asset['url']:
# new_assets['github'] = new_assets.get('github', [])
new_assets['github'].append(asset['url'])
elif '/0x' in asset['url']:
# new_assets['contract'] = new_assets.get('contract', [])
new_assets['contract'].append(asset['url'])
else:
# new_assets['other'] = new_assets.get('other', [])
new_assets['other'].append(asset['url'])
return new_assets
def get_project_data(url):
try:
page = requests.get(url, timeout=int(os.environ['TIMEOUT']))
except requests.ConnectionError:
print(f"[-] Unable to fetch URL : {url}. Make sure you are connected to the internet")
exit()
soup = BeautifulSoup(page.content, 'html.parser')
soup = soup.find("script", id="__NEXT_DATA__")
json_data = json.loads(soup.string)
bounty = json_data['props']['pageProps']['bounty']
return bounty
def get_data(query=None, filter=None):
url = 'https://immunefi.com/explore/'
main_url = 'https://immunefi.com'
try:
page = requests.get(url, timeout=int(os.environ['TIMEOUT']))
except requests.ConnectionError:
print(f"[-] Unable to fetch URL : {url}. Make sure you are connected to the internet")
exit()
soup = BeautifulSoup(page.content, 'html.parser')
soup = soup.find("script", id="__NEXT_DATA__")
json_data = json.loads(soup.string)
bounties = json_data['props']['pageProps']['bounties']
# print("Total number of projects :", len(bounties))
projects = []
if query:
# Search for query string in projects
for project in bounties:
if query.lower() in project['project'].lower():
try:
project_detail = {}
project_detail['id'] = get(project, 'id')
project_detail['project'] = get(project, 'project')
project_detail['date'] = get(project, 'date')
project_detail['maximum_reward'] = get(project, 'maximum_reward')
project_detail['technologies'] = get(project, 'technologies')
bounty_url = main_url + '/bounty/' + project_detail['id']
project_detail['url'] = bounty_url
project_ext_data = get_project_data(bounty_url)
project_detail['kyc'] = get(project_ext_data, 'kyc')
assets = project_ext_data['assets']
project_detail['assets_in_scope'] = get_assets(assets, filter=filter)
project_detail['num_contracts'] = 0
if parser.get('least_total_contracts', None):
# print("Project = ", project_detail['project'])
project_detail['num_contracts'] = get_number_of_contracts(contract_list=project_detail['assets_in_scope']['contract'])
# count_github_files(project_detail['assets_in_scope']['github'])
projects.append(Project(**project_detail))
except Exception as err:
print("Exception =", err)
else:
# List all projects
for project in bounties:
if project['is_external']:
continue
try:
project_detail = {}
project_detail['id'] = get(project, 'id')
project_detail['project'] = get(project, 'project')
project_detail['date'] = get(project, 'date')
project_detail['maximum_reward'] = get(project, 'maximum_reward')
project_detail['technologies'] = get(project, 'technologies')
bounty_url = main_url + '/bounty/' + project_detail['id']
project_detail['url'] = bounty_url
project_ext_data = get_project_data(bounty_url)
project_detail['kyc'] = get(project_ext_data, 'kyc')
assets = project_ext_data['assets']
project_detail['assets_in_scope'] = get_assets(assets)
project_detail['num_contracts'] = 0
if parser.get('least_total_contracts', None):
# print("Project = ", project_detail['project'])
project_detail['num_contracts'] = get_number_of_contracts(contract_list=project_detail['assets_in_scope']['contract'])
# count_github_files(project_detail['assets_in_scope']['github'])
projects.append(Project(**project_detail))
except Exception as err:
print("Exception =", err)
return projects
def display_projects(projects, type=None, option=None):
print(f"NOTE: Ignoring programs with zero contract links")
console = Console()
table = Table(show_header=True, header_style="bold magenta")
# FORMAT = "%5s %-40s %-20s %-30s %20s"
# print('-'*120)
if option == 'ltc':
# print(FORMAT % ("SN", "Project", "Reward($)", "Tech", "#Contracts"))
for col in ["SN", "Project", "Reward", "Technologies", "#Contracts"]:
if col == "Reward":
table.add_column(col, justify="center", header_style="cyan", style="green")
else:
table.add_column(col, justify="center", header_style="cyan")
else:
# print(FORMAT % ("SN", "Project", "Reward($)", "Tech", "#Links"))
for col in ["SN", "Project", "Reward", "Technologies", "#Links"]:
if col == "Reward":
table.add_column(col, justify="center", header_style="cyan", style="green")
else:
table.add_column(col, justify="center", header_style="cyan")
# print('-'*120)
# print(projects[0].__dict__)
sn = 1
for project in projects:
tech = '|'.join(project.technologies) if project.technologies else None
if type:
if project.assets_in_scope[type]:
# print(FORMAT % (sn, project.project, project.maximum_reward, tech, len(project.assets_in_scope[type])))
table.add_row(str(sn), project.project, get_dollar_repr(project.maximum_reward), tech, str(len(project.assets_in_scope[type])))
sn += 1
elif option == 'ltc':
# number_contracts = sum((project.num_contracts[k]) for k in project.num_contracts)
number_contracts = project.num_contracts
if number_contracts:
# print(FORMAT % (sn, project.project, project.maximum_reward, tech, number_contracts))
table.add_row(str(sn), project.project, get_dollar_repr(project.maximum_reward), tech, str(number_contracts))
sn += 1
else:
number_contract_links = sum(len(project.assets_in_scope[k]) for k in project.assets_in_scope)
if number_contract_links:
# print(FORMAT % (sn, project.project, project.maximum_reward, tech, number_contract_links))
table.add_row(str(sn), project.project, get_dollar_repr(project.maximum_reward), tech, str(number_contract_links))
sn += 1
# print('-'*120)
console.print(table)
def search_contract(projects, query):
results = []
for project in projects:
if query.lower() in project.project.lower():
results.append(project)
return results
projects = []
if parser.get('list_programs', None):
t0 = time.time()
projects = get_data()
t1 = time.time()
print(f"{t1-t0} seconds to process {len(projects)} projects.")
if parser.get('list_programs', None):
if parser.get('least_contract_link', None):
print(f"Filtering by Least number of contract links...")
filtered_projects = sorted(projects, key=lambda x: len(x.assets_in_scope['contract']), reverse=False)
display_projects(filtered_projects, type='contract')
elif parser.get('least_github_link', None):
print(f"Filtering by Least number of github links...")
filtered_projects = sorted(projects, key=lambda x: len(x.assets_in_scope['github']), reverse=False)
display_projects(filtered_projects, type='github')
elif parser.get('least_other_link', None):
print(f"Filtering by Least number of other links...")
filtered_projects = sorted(projects, key=lambda x: len(x.assets_in_scope['other']), reverse=False)
display_projects(filtered_projects, type='other')
elif parser.get('least_total_link', None):
print(f"Filtering by Least total number of links...")
filtered_projects = sorted(projects, key=lambda x: sum(len(x.assets_in_scope[k]) for k in x.assets_in_scope), reverse=False)
display_projects(filtered_projects)
elif parser.get('least_total_contracts', None):
print(f"Filtering by Least total number of contracts...")
# filtered_projects = sorted(projects, key=lambda x: sum((x.num_contracts[k]) for k in x.num_contracts), reverse=False)
# import pdb; pdb.set_trace()
filtered_projects = sorted(projects, key=lambda x: x.num_contracts, reverse=False)
display_projects(filtered_projects, option='ltc')
# elif parser.get('test', None):
# print('Testing')
# LINK = ['https://etherscan.io/address/0xeecee260a402fe3c20e5b8301382005124bef121a', \
# 'https://etherscan.io/address/0xde229e52bdb72c449db7912968e51d9d5e793005', \
# 'https://etherscan.io/address/0xca1bf9e6add6155e92dc1dc7c0bf210c159a2f43']
# print(get_number_of_contracts(contract_list=LINK))
else:
display_projects(projects)
if parser.get('query', None):
print(f"Searching for {parser.get('query')}...")
t0 = time.time()
# print("Filter =", parser.get("filter"))
res = get_data(parser.get('query'), filter=parser.get("filter"))
t1 = time.time()
print(f"{t1-t0} seconds to process {len(res)} projects.")
# res = search_contract(projects, parser.get('query'))
if res:
display_projects(res)
for item in res:
print(f"Links for {item.project}:")
for rec in item.assets_in_scope:
print(f"{rec.upper()}:")
i = 1
if not item.assets_in_scope[rec]:
print("\tNO DATA FOUND\n")
for link in item.assets_in_scope[rec]:
print(f"\t{i}. {link}")
i += 1
print()
for item in res:
if parser.get('download', None):
download_contracts(item.assets_in_scope['contract'], project_name=item.project.strip())
download_github(item.assets_in_scope['github'], project_name=item.project.strip())
else:
print(f"Not able to find project : {parser.get('query')}")
if parser.get('links', None):
prepare_links = [{'url': link} for link in parser.get('links')]
links = get_assets(prepare_links)
download_contracts(links['contract'], project_name=parser.get('folder_name'))
download_github(links['github'], project_name=parser.get('folder_name'))