Skip to content

Commit

Permalink
chg: [crawler] auto tag crawled domains
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrtia committed Jun 20, 2023
1 parent c02b9f2 commit 501d10b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 17 deletions.
5 changes: 4 additions & 1 deletion bin/crawlers/Crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,13 @@ def compute(self, capture):
self.save_capture_response(parent_id, entries)

self.domain.update_daterange(self.date.replace('/', ''))
# Origin + History
# Origin + History + tags
if self.root_item:
self.domain.set_last_origin(parent_id)
self.domain.add_history(epoch, root_item=self.root_item)
# Tags
for tag in task.get_tags():
self.domain.add_tag(tag)
elif self.domain.was_up():
self.domain.add_history(epoch, root_item=epoch)

Expand Down
58 changes: 47 additions & 11 deletions bin/lib/crawlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ def process_queue(self):
task_uuid = create_task(meta['url'], depth=meta['depth'], har=meta['har'], screenshot=meta['screenshot'],
header=meta['header'],
cookiejar=meta['cookiejar'], proxy=meta['proxy'],
tags=meta['tags'],
user_agent=meta['user_agent'], parent='scheduler', priority=40)
if task_uuid:
schedule.set_task(task_uuid)
Expand Down Expand Up @@ -1069,6 +1070,14 @@ def get_user_agent(self):
def _set_field(self, field, value):
return r_crawler.hset(f'schedule:{self.uuid}', field, value)

def get_tags(self):
return r_crawler.smembers(f'schedule:tags:{self.uuid}')

def set_tags(self, tags=[]):
for tag in tags:
r_crawler.sadd(f'schedule:tags:{self.uuid}', tag)
# Tag.create_custom_tag(tag)

def get_meta(self, ui=False):
meta = {
'uuid': self.uuid,
Expand All @@ -1083,6 +1092,7 @@ def get_meta(self, ui=False):
'cookiejar': self.get_cookiejar(),
'header': self.get_header(),
'proxy': self.get_proxy(),
'tags': self.get_tags(),
}
status = self.get_status()
if ui:
Expand All @@ -1098,6 +1108,7 @@ def get_meta_status(self): # TODO: Description ? Frequency ???
meta = {'uuid': self.uuid,
'url': self.get_url(),
'user': self.get_user(),
'tags': self.get_tags(),
'next_run': self.get_next_run(r_str=True)}
status = self.get_status()
if isinstance(status, ScheduleStatus):
Expand All @@ -1106,7 +1117,7 @@ def get_meta_status(self): # TODO: Description ? Frequency ???
return meta

def create(self, frequency, user, url,
depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None, user_agent=None):
depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None, user_agent=None, tags=[]):

if self.exists():
raise Exception('Error: Monitor already exists')
Expand Down Expand Up @@ -1135,6 +1146,9 @@ def create(self, frequency, user, url,
if user_agent:
self._set_field('user_agent', user_agent)

if tags:
self.set_tags(tags)

r_crawler.sadd('scheduler:schedules', self.uuid)

def delete(self):
Expand All @@ -1148,12 +1162,13 @@ def delete(self):

# delete meta
r_crawler.delete(f'schedule:{self.uuid}')
r_crawler.delete(f'schedule:tags:{self.uuid}')
r_crawler.srem('scheduler:schedules', self.uuid)

def create_schedule(frequency, user, url, depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None, user_agent=None):
def create_schedule(frequency, user, url, depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None, user_agent=None, tags=[]):
schedule_uuid = gen_uuid()
schedule = CrawlerSchedule(schedule_uuid)
schedule.create(frequency, user, url, depth=depth, har=har, screenshot=screenshot, header=header, cookiejar=cookiejar, proxy=proxy, user_agent=user_agent)
schedule.create(frequency, user, url, depth=depth, har=har, screenshot=screenshot, header=header, cookiejar=cookiejar, proxy=proxy, user_agent=user_agent, tags=tags)
return schedule_uuid

# TODO sanityze UUID
Expand Down Expand Up @@ -1289,6 +1304,11 @@ def get_captures_status():
status.append(meta)
return status

def delete_captures():
for capture_uuid in get_crawler_captures():
capture = CrawlerCapture(capture_uuid)
capture.delete()

##-- CRAWLER STATE --##


Expand Down Expand Up @@ -1371,6 +1391,14 @@ def is_ongoing(self):
def _set_field(self, field, value):
return r_crawler.hset(f'crawler:task:{self.uuid}', field, value)

def get_tags(self):
return r_crawler.smembers(f'crawler:task:tags:{self.uuid}')

def set_tags(self, tags):
for tag in tags:
r_crawler.sadd(f'crawler:task:tags:{self.uuid}', tag)
# Tag.create_custom_tag(tag)

def get_meta(self):
meta = {
'uuid': self.uuid,
Expand All @@ -1385,14 +1413,15 @@ def get_meta(self):
'header': self.get_header(),
'proxy': self.get_proxy(),
'parent': self.get_parent(),
'tags': self.get_tags(),
}
return meta

# TODO STATUS UPDATE
# TODO SANITIZE PRIORITY
# PRIORITY: discovery = 0/10, feeder = 10, manual = 50, auto = 40, test = 100
def create(self, url, depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None,
user_agent=None, parent='manual', priority=0):
user_agent=None, tags=[], parent='manual', priority=0):
if self.exists():
raise Exception('Error: Task already exists')

Expand Down Expand Up @@ -1423,7 +1452,7 @@ def create(self, url, depth=1, har=True, screenshot=True, header=None, cookiejar
# TODO SANITIZE COOKIEJAR -> UUID

# Check if already in queue
hash_query = get_task_hash(url, domain, depth, har, screenshot, priority, proxy, cookiejar, user_agent, header)
hash_query = get_task_hash(url, domain, depth, har, screenshot, priority, proxy, cookiejar, user_agent, header, tags)
if r_crawler.hexists(f'crawler:queue:hash', hash_query):
self.uuid = r_crawler.hget(f'crawler:queue:hash', hash_query)
return self.uuid
Expand All @@ -1444,6 +1473,9 @@ def create(self, url, depth=1, har=True, screenshot=True, header=None, cookiejar
if user_agent:
self._set_field('user_agent', user_agent)

if tags:
self.set_tags(tags)

r_crawler.hset('crawler:queue:hash', hash_query, self.uuid)
self._set_field('hash', hash_query)
r_crawler.zadd('crawler:queue', {self.uuid: priority})
Expand Down Expand Up @@ -1483,10 +1515,10 @@ def delete(self):


# TODO move to class ???
def get_task_hash(url, domain, depth, har, screenshot, priority, proxy, cookiejar, user_agent, header):
def get_task_hash(url, domain, depth, har, screenshot, priority, proxy, cookiejar, user_agent, header, tags):
to_enqueue = {'domain': domain, 'depth': depth, 'har': har, 'screenshot': screenshot,
'priority': priority, 'proxy': proxy, 'cookiejar': cookiejar, 'user_agent': user_agent,
'header': header}
'header': header, 'tags': tags}
if priority != 0:
to_enqueue['url'] = url
return hashlib.sha512(pickle.dumps(to_enqueue)).hexdigest()
Expand All @@ -1502,15 +1534,15 @@ def add_task_to_lacus_queue():

# PRIORITY: discovery = 0/10, feeder = 10, manual = 50, auto = 40, test = 100
def create_task(url, depth=1, har=True, screenshot=True, header=None, cookiejar=None, proxy=None,
user_agent=None, parent='manual', priority=0, task_uuid=None):
user_agent=None, tags=[], parent='manual', priority=0, task_uuid=None):
if task_uuid:
if CrawlerTask(task_uuid).exists():
task_uuid = gen_uuid()
else:
task_uuid = gen_uuid()
task = CrawlerTask(task_uuid)
task_uuid = task.create(url, depth=depth, har=har, screenshot=screenshot, header=header, cookiejar=cookiejar,
proxy=proxy, user_agent=user_agent, parent=parent, priority=priority)
proxy=proxy, user_agent=user_agent, tags=tags, parent=parent, priority=priority)
return task_uuid


Expand Down Expand Up @@ -1586,15 +1618,17 @@ def api_add_crawler_task(data, user_id=None):
if verify[1] != 200:
return verify

tags = data.get('tags', [])

if frequency:
# TODO verify user
return create_schedule(frequency, user_id, url, depth=depth_limit, har=har, screenshot=screenshot, header=None,
cookiejar=cookiejar_uuid, proxy=proxy, user_agent=None), 200
cookiejar=cookiejar_uuid, proxy=proxy, user_agent=None, tags=tags), 200
else:
# TODO HEADERS
# TODO USER AGENT
return create_task(url, depth=depth_limit, har=har, screenshot=screenshot, header=None,
cookiejar=cookiejar_uuid, proxy=proxy, user_agent=None,
cookiejar=cookiejar_uuid, proxy=proxy, user_agent=None, tags=tags,
parent='manual', priority=90), 200


Expand Down Expand Up @@ -1870,6 +1904,8 @@ def test_ail_crawlers():
load_blacklist()

# if __name__ == '__main__':
# delete_captures()

# item_id = 'crawled/2023/02/20/data.gz'
# item = Item(item_id)
# content = item.get_content()
Expand Down
35 changes: 34 additions & 1 deletion var/www/blueprints/crawler_splash.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def manual():
is_manager_connected=crawlers.get_lacus_connection_metadata(),
crawlers_types=crawlers_types,
proxies=proxies,
l_cookiejar=l_cookiejar)
l_cookiejar=l_cookiejar,
tags_selector_data=Tag.get_tags_selector_data())


@crawler_splash.route("/crawlers/send_to_spider", methods=['POST'])
Expand All @@ -113,6 +114,34 @@ def send_to_spider():
depth_limit = request.form.get('depth_limit')
cookiejar_uuid = request.form.get('cookiejar')

# TAGS
tags = request.form.get("tags", [])
taxonomies_tags = request.form.get('taxonomies_tags')
if taxonomies_tags:
try:
taxonomies_tags = json.loads(taxonomies_tags)
except:
taxonomies_tags = []
else:
taxonomies_tags = []
galaxies_tags = request.form.get('galaxies_tags')
if galaxies_tags:
try:
galaxies_tags = json.loads(galaxies_tags)
except:
galaxies_tags = []
else:
galaxies_tags = []
# custom tags
if tags:
tags = tags.split()
else:
tags = []
escaped = []
for tag in tags:
escaped.append(tag)
tags = escaped + taxonomies_tags + galaxies_tags

# Frequency
if request.form.get('crawler_scheduler'):
frequency = request.form.get('frequency')
Expand Down Expand Up @@ -147,6 +176,8 @@ def send_to_spider():
data['proxy'] = proxy
if cookiejar_uuid:
data['cookiejar'] = cookiejar_uuid
if tags:
data['tags'] = tags
# print(data)
res = crawlers.api_add_crawler_task(data, user_id=user_id)

Expand All @@ -163,6 +194,7 @@ def scheduler_dashboard():
# print(schedulers)
# TODO list currently queued ?
return render_template("crawler_scheduler_dashboard.html",
bootstrap_label=bootstrap_label,
schedulers=schedulers,
is_manager_connected=crawlers.get_lacus_connection_metadata())

Expand All @@ -176,6 +208,7 @@ def schedule_show():
abort(404)
meta = schedule.get_meta(ui=True)
return render_template("crawler_schedule_uuid.html",
bootstrap_label=bootstrap_label,
meta=meta)

@crawler_splash.route("/crawlers/schedule/delete", methods=['GET'])
Expand Down
23 changes: 20 additions & 3 deletions var/www/templates/crawler/crawler_splash/crawler_manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
<link href="{{ url_for('static', filename='css/bootstrap4.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/font-awesome.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/daterangepicker.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/tags.css') }}" rel="stylesheet">

<!-- JS -->
<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
<script src="{{ url_for('static', filename='js/popper.min.js')}}"></script>
<script src="{{ url_for('static', filename='js/bootstrap4.min.js')}}"></script>
<script language="javascript" src="{{ url_for('static', filename='js/moment.min.js') }}"></script>
<script language="javascript" src="{{ url_for('static', filename='js/jquery.daterangepicker.min.js') }}"></script>
<script language="javascript" src="{{ url_for('static', filename='js/d3.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/moment.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery.daterangepicker.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/d3.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/tags.js') }}"></script>

</head>

Expand Down Expand Up @@ -119,6 +121,21 @@ <h5><i class="fas fa-clock"></i>&nbsp;Adjust crawling interval as needed</h5>

</div>

<div class="card bg-light my-4">
<div class="card-header bg-dark text-white">
<b>Tags</b>
</div>
<div class="card-body">
<div class="input-group mb-2 mr-sm-2">
<div class="input-group-prepend">
<div class="input-group-text bg-danger text-white"><i class="fas fa-tag"></i></div>
</div>
<input id="tags" name="tags" class="form-control" placeholder="Custom Tags (optional, space separated)" type="text" {%if dict_tracker%}{%if dict_tracker['tags']%}value="{%for tag in dict_tracker['tags']%}{{tag}} {%endfor%}"{%endif%}{%endif%}>
</div>
{% include 'tags/block_tags_selector.html' %}
</div>
</div>

</div>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ <h4 class="card-title"><b>{{ meta['url'] }}</b></h4>
{% endif %}
</td>
</tr>
<tr>
<th>Tags</th>
<td>
{%for tag in meta['tags']%}
<span class="badge badge-{{ bootstrap_label[loop.index0 % 5] }}">{{ tag }}</span>
{%endfor%}
</td>
</tr>
</tbody>
</table>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ <h1>Schedulers:</h1>
<tbody id="tbody_last_crawled">
{% for meta in schedulers %}
<tr>
<td><a href="{{ url_for('crawler_splash.schedule_show') }}?uuid={{ meta['uuid'] }}">{{ meta['url'] }}</a></td>
<td>
<a href="{{ url_for('crawler_splash.schedule_show') }}?uuid={{ meta['uuid'] }}">{{ meta['url'] }}</a>
<div>
{% for tag in meta['tags'] %}
<span class="badge badge-{{ bootstrap_label[loop.index0 % 5] }}">{{ tag }}</span>
{%endfor%}
</div>
</td>
<td>{{ meta['status'] }}</td>
<td>
{% if not meta['next_run'] %}
Expand Down

0 comments on commit 501d10b

Please sign in to comment.