This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
scraper.py
287 lines (251 loc) · 11.7 KB
/
scraper.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
# -*- coding: utf-8 -*-
# This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.
import json
import os
import base64
from datetime import datetime
from glob import glob
from shutil import copy2
from viper.common.abstracts import Module
from viper.core.config import __config__
from viper.core.project import __project__
import logging
logger = logging.getLogger(__name__)
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
try:
from scrapysplashwrapper import crawl
HAVE_SCRAPY = True
except ImportError:
HAVE_SCRAPY = False
try:
from har2tree import CrawledTree
HAVE_ETE = True
except ImportError:
HAVE_ETE = False
cfg = __config__
class Scraper(Module):
cmd = 'scraper'
description = 'Scrape a website using scrapy and splash. Requires a running splash instance, provided as a docker thingie.'
authors = ['Raphaël Vinot']
def __init__(self):
super(Scraper, self).__init__()
try:
self.user_agents = cfg.useragents.ua.split('\n')
except Exception:
# Use a generic user agent in case the viper user didn't update their config file
self.user_agents = ['Mozilla/5.0 (Windows NT 6.3; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0']
self.scraper_store = os.path.join(__project__.get_path(), 'scraper')
if not os.path.exists(self.scraper_store):
os.makedirs(self.scraper_store)
self.quiet = False
self.very_quiet = False
self.verbose = False
self.debug = False
# Scraping paramaters
self.parser.add_argument("-u", "--url", help='URL to scrap')
self.parser.add_argument("--depth", type=int, default=1, help='Depth to crawl on the website')
# Actions on already scraped data
self.parser.add_argument("-l", "--list", action='store_true', help='List already scraped URLs')
group1 = self.parser.add_argument_group('ID details', 'Actions on scraped data (by ID).')
group1.add_argument("-i", "--id", type=int, help='Dump ID (get it from -l/--list).')
group1.add_argument("-d", "--delete", action='store_true', help='Delete a report (ID, or all).')
group1.add_argument("-v", "--view", action='store_true', help='View a dump.')
group1.add_argument("-t", "--tree", action='store_true', help='Tree view.')
group1.add_argument("-ch", "--copy_har", help='Copy harfiles somewhere else.')
# General parameters
self.parser.add_argument("-vq", "--very_quiet", action='store_true', help='Very quiet view (Only display hostnames)')
self.parser.add_argument("-q", "--quiet", action='store_true', help='Quiet view (Only display external URLs)')
self.parser.add_argument("--verbose", action='store_true', help='Verbose view')
self.parser.add_argument("--debug", action='store_true', help='Enable debug on the crawler.')
def scrape(self, ua, url, depth):
if not HAVE_SCRAPY:
self.log('error', 'Missing dependencies: scrapy and scrapy-splash')
return
if self.debug:
params = {'log_enabled': True, 'log_level': 'INFO'}
else:
params = {}
items = crawl(cfg.scraper.splash_url, url, depth, ua, **params)
width = len(str(len(items)))
if not items:
self.log('error', 'Unable to crawl. Probably a network problem (try --debug).')
return None
i = 1
now = datetime.now().isoformat()
dirpath = os.path.join(self.scraper_store, now)
os.makedirs(dirpath)
for item in items:
with open(os.path.join(dirpath, '{0:0{width}}.json'.format(i, width=width)), 'w') as f:
json.dump(item, f)
png = item['png']
with open(os.path.join(dirpath, '{0:0{width}}.png'.format(i, width=width)), 'wb') as f:
f.write(base64.b64decode(png))
harfile = item['har']
with open(os.path.join(dirpath, '{0:0{width}}.har'.format(i, width=width)), 'w') as f:
json.dump(harfile, f)
htmlfile = item['html']
with open(os.path.join(dirpath, '{0:0{width}}.html'.format(i, width=width)), 'w') as f:
json.dump(htmlfile, f)
i += 1
return now
def tree(self):
if not HAVE_ETE:
self.log('error', 'Missing dependency: git+https://github.com/viper-framework/har2tree.git')
return
har_files = self.all_reports[self.reportid]['har']
ct = CrawledTree(har_files)
ct.find_parents()
ct.join_trees()
tree_file = os.path.join(self.scraper_store, self.all_reports[self.reportid]['isots'], "tree.pdf")
ct.dump_test(tree_file)
self.log('success', 'Tree dump created: {}'.format(tree_file))
def view(self):
all_hostnames = set()
for json_f, har, png in zip(self.all_reports[self.reportid]['json'],
self.all_reports[self.reportid]['har'],
self.all_reports[self.reportid]['png']):
with open(json_f, 'r') as f:
loaded_json = json.load(f)
if not self.very_quiet:
self.log('info', 'Requested URL: {}'.format(loaded_json['requestedUrl']))
if loaded_json['url'] != loaded_json['requestedUrl']:
self.log('item', 'Redirected to: {}'.format(loaded_json['url']))
if loaded_json.get('title'):
self.log('item', loaded_json['title'])
self.log('success', 'PNG view ({}): {}'.format(loaded_json['geometry'], png))
with open(har, 'r') as f:
harfile = json.load(f)
requested_domain = '.'.join(urlparse(loaded_json['url']).hostname.split('.')[-2:])
for entry in harfile['log']['entries']:
# Inspired by: https://github.com/fboender/harview/blob/master/src/harview.py
if self.quiet and not entry['response']['redirectURL']:
url_parsed = urlparse(entry['response']['url'])
if url_parsed.hostname and url_parsed.hostname.endswith(requested_domain):
continue
hostname = urlparse(entry['request']['url']).hostname
if hostname:
all_hostnames.add(urlparse(entry['request']['url']).hostname)
if self.very_quiet:
continue
status = entry['response']['status']
if status >= 400:
log_type = 'error'
elif status >= 300:
log_type = 'warning'
else:
log_type = 'success'
self.log(log_type, '{} {} {}'.format(status, entry['request']['method'], entry['request']['url']))
if 300 <= status <= 399:
# Redirect
self.log(log_type, 'Redirect to: {}'.format(entry['response']['redirectURL']))
if not self.verbose:
continue
self.log('info', 'Request headers')
for header in entry['request']['headers']:
self.log('item', '{}: {}'.format(header['name'], header['value']))
if entry['request']['method'] == 'POST' and entry['request'].get('postData'):
self.log('info', 'POST data ({})'.format(entry['request']['postData']['mimeType']))
self.log('item', entry['request']['postData']['text'])
self.log('info', 'Response headers (status = {})'.format(entry['response']['status']))
for header in entry['response']['headers']:
self.log('item', '{}: {}'.format(header['name'], header['value']))
if 'text' in entry['response']['content']:
self.log('info', 'Response data ({})'.format(entry['response']['content']['mimeType']))
self.log('item', entry['response']['content']['text'])
if self.very_quiet:
self.log('info', 'All unique hostnames appearing in this trace:')
for d in sorted(all_hostnames):
self.log('item', d)
def load_reports(self):
to_return = []
for report in self.sorted_reports():
r = {}
r['isots'] = report
r['json'] = sorted(glob(os.path.join(self.scraper_store, report, '*.json')))
r['har'] = sorted(glob(os.path.join(self.scraper_store, report, '*.har')))
r['png'] = sorted(glob(os.path.join(self.scraper_store, report, '*.png')))
to_return.append(r)
return to_return
def sorted_reports(self):
return sorted(os.listdir(self.scraper_store))
def list(self):
header = ['ID', 'Time', 'Requested URL']
rows = []
if self.reportid is not None:
json_files = self.all_reports[self.reportid]['json']
for jf in json_files:
with open(jf, 'r') as f:
loaded_json = json.load(f)
row = [self.reportid + 1, self.all_reports[self.reportid]['isots'], loaded_json['requestedUrl']]
rows.append(row)
else:
i = 1
for report in self.all_reports:
with open(report['json'][0], 'r') as f:
loaded_json = json.load(f)
# print(loaded_json['requestedUrl'])
row = [i, report['isots'], loaded_json['requestedUrl']]
i += 1
rows.append(row)
self.log('table', dict(header=header, rows=rows))
def copy_har(self, destination):
report = self.all_reports[self.reportid]
if os.path.exists(destination):
if not os.path.isdir(destination):
self.log('error', 'If it exists, destination has to be a directory.')
else:
destination = os.path.join(destination, report['isots'])
os.makedirs(destination)
for harfile in report['har']:
copy2(harfile, destination)
self.log('success', 'Har files copied to {}.'.format(destination))
def delete(self):
if self.reportid == 'all':
for path in glob(os.path.join(self.scraper_store, '*')):
os.rmdir(path)
self.log('success', '{} deleted.'.format(path))
return
path = os.path.join(self.scraper_store, self.all_reports[self.reportid]['isots'])
os.rmdir(path)
self.log('success', '{} deleted.'.format(path))
def run(self):
super(Scraper, self).run()
self.all_reports = self.load_reports()
if self.args is None:
return
if self.args.quiet:
self.quiet = True
if self.args.verbose:
self.verbose = True
if self.args.debug:
self.debug = True
if self.args.very_quiet:
self.very_quiet = True
if self.args.id is not None:
self.reportid = self.args.id - 1
else:
self.reportid = None
if self.args.url:
self.reportid = self.scrape(self.user_agents[0], self.args.url, self.args.depth)
if self.reportid is None:
return
self.all_reports = self.load_reports()
self.reportid = len(self.all_reports) - 1
self.view()
elif self.args.list:
self.list()
elif self.reportid:
if self.args.view:
self.view()
if self.args.delete:
self.delete()
elif self.args.tree:
self.tree()
elif self.args.copy_har:
self.copy_har(self.args.copy_har)
else:
return