2018-08-09 17:42:21 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2018-08-13 09:23:14 +02:00
|
|
|
import re
|
2019-02-25 16:38:50 +01:00
|
|
|
import uuid
|
|
|
|
import json
|
2018-08-09 17:42:21 +02:00
|
|
|
import redis
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
import subprocess
|
2018-08-16 17:24:39 +02:00
|
|
|
import requests
|
2018-08-09 17:42:21 +02:00
|
|
|
|
2019-05-15 09:57:18 +02:00
|
|
|
from collections import deque
|
2019-01-29 12:00:14 +01:00
|
|
|
from pyfaup.faup import Faup
|
|
|
|
|
2018-08-09 17:42:21 +02:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
from Helper import Process
|
|
|
|
from pubsublogger import publisher
|
|
|
|
|
2020-07-24 08:54:54 +02:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib'))
|
|
|
|
import crawlers
|
|
|
|
|
2019-02-07 17:22:44 +01:00
|
|
|
# ======== FUNCTIONS ========
|
2019-02-05 17:16:44 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
def load_blacklist(service_type):
|
2019-02-05 17:16:44 +01:00
|
|
|
try:
|
2019-02-22 17:00:24 +01:00
|
|
|
with open(os.environ['AIL_BIN']+'/torcrawler/blacklist_{}.txt'.format(service_type), 'r') as f:
|
|
|
|
redis_crawler.delete('blacklist_{}'.format(service_type))
|
2019-02-05 17:16:44 +01:00
|
|
|
lines = f.read().splitlines()
|
|
|
|
for line in lines:
|
2019-02-22 17:00:24 +01:00
|
|
|
redis_crawler.sadd('blacklist_{}'.format(service_type), line)
|
2019-02-05 17:16:44 +01:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2019-02-26 14:50:48 +01:00
|
|
|
def update_auto_crawler():
|
|
|
|
current_epoch = int(time.time())
|
|
|
|
list_to_crawl = redis_crawler.zrangebyscore('crawler_auto_queue', '-inf', current_epoch)
|
|
|
|
for elem_to_crawl in list_to_crawl:
|
|
|
|
mess, type = elem_to_crawl.rsplit(';', 1)
|
|
|
|
redis_crawler.sadd('{}_crawler_priority_queue'.format(type), mess)
|
|
|
|
redis_crawler.zrem('crawler_auto_queue', elem_to_crawl)
|
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# Extract info form url (url, domain, domain url, ...)
|
|
|
|
def unpack_url(url):
|
2019-02-25 16:38:50 +01:00
|
|
|
to_crawl = {}
|
2019-02-22 17:00:24 +01:00
|
|
|
faup.decode(url)
|
|
|
|
url_unpack = faup.get()
|
2019-05-06 11:46:20 +02:00
|
|
|
# # FIXME: # TODO: remove me
|
|
|
|
try:
|
|
|
|
to_crawl['domain'] = url_unpack['domain'].decode()
|
|
|
|
except:
|
|
|
|
to_crawl['domain'] = url_unpack['domain']
|
|
|
|
to_crawl['domain'] = to_crawl['domain'].lower()
|
|
|
|
|
|
|
|
|
|
|
|
# force lower case domain/subdomain (rfc4343)
|
|
|
|
# # FIXME: # TODO: remove me
|
|
|
|
try:
|
|
|
|
url_host = url_unpack['host'].decode()
|
|
|
|
except:
|
|
|
|
url_host = url_unpack['host']
|
|
|
|
|
|
|
|
new_url_host = url_host.lower()
|
|
|
|
url_lower_case = url.replace(url_host, new_url_host, 1)
|
2019-02-25 16:38:50 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
if url_unpack['scheme'] is None:
|
2019-02-25 16:38:50 +01:00
|
|
|
to_crawl['scheme'] = 'http'
|
2019-05-06 11:46:20 +02:00
|
|
|
url= 'http://{}'.format(url_lower_case)
|
2019-02-25 16:38:50 +01:00
|
|
|
else:
|
2019-05-06 11:46:20 +02:00
|
|
|
# # FIXME: # TODO: remove me
|
|
|
|
try:
|
|
|
|
scheme = url_unpack['scheme'].decode()
|
|
|
|
except Exception as e:
|
|
|
|
scheme = url_unpack['scheme']
|
2019-02-25 16:38:50 +01:00
|
|
|
if scheme in default_proto_map:
|
|
|
|
to_crawl['scheme'] = scheme
|
2019-05-06 11:46:20 +02:00
|
|
|
url = url_lower_case
|
2019-02-25 16:38:50 +01:00
|
|
|
else:
|
2019-05-06 11:46:20 +02:00
|
|
|
redis_crawler.sadd('new_proto', '{} {}'.format(scheme, url_lower_case))
|
2019-02-25 16:38:50 +01:00
|
|
|
to_crawl['scheme'] = 'http'
|
2019-05-06 11:46:20 +02:00
|
|
|
url= 'http://{}'.format(url_lower_case.replace(scheme, '', 1))
|
2019-02-25 16:38:50 +01:00
|
|
|
|
|
|
|
if url_unpack['port'] is None:
|
|
|
|
to_crawl['port'] = default_proto_map[to_crawl['scheme']]
|
|
|
|
else:
|
2019-05-06 11:46:20 +02:00
|
|
|
# # FIXME: # TODO: remove me
|
|
|
|
try:
|
|
|
|
port = url_unpack['port'].decode()
|
|
|
|
except:
|
|
|
|
port = url_unpack['port']
|
2019-02-25 16:38:50 +01:00
|
|
|
# Verify port number #################### make function to verify/correct port number
|
|
|
|
try:
|
|
|
|
int(port)
|
|
|
|
# Invalid port Number
|
|
|
|
except Exception as e:
|
|
|
|
port = default_proto_map[to_crawl['scheme']]
|
|
|
|
to_crawl['port'] = port
|
|
|
|
|
2019-04-25 13:54:06 +02:00
|
|
|
#if url_unpack['query_string'] is None:
|
|
|
|
# if to_crawl['port'] == 80:
|
|
|
|
# to_crawl['url']= '{}://{}'.format(to_crawl['scheme'], url_unpack['host'].decode())
|
|
|
|
# else:
|
|
|
|
# to_crawl['url']= '{}://{}:{}'.format(to_crawl['scheme'], url_unpack['host'].decode(), to_crawl['port'])
|
|
|
|
#else:
|
|
|
|
# to_crawl['url']= '{}://{}:{}{}'.format(to_crawl['scheme'], url_unpack['host'].decode(), to_crawl['port'], url_unpack['query_string'].decode())
|
|
|
|
|
|
|
|
to_crawl['url'] = url
|
|
|
|
if to_crawl['port'] == 80:
|
2019-05-06 11:46:20 +02:00
|
|
|
to_crawl['domain_url'] = '{}://{}'.format(to_crawl['scheme'], new_url_host)
|
2019-02-22 17:00:24 +01:00
|
|
|
else:
|
2019-05-06 11:46:20 +02:00
|
|
|
to_crawl['domain_url'] = '{}://{}:{}'.format(to_crawl['scheme'], new_url_host, to_crawl['port'])
|
2019-04-25 13:54:06 +02:00
|
|
|
|
2019-05-06 11:46:20 +02:00
|
|
|
# # FIXME: # TODO: remove me
|
|
|
|
try:
|
|
|
|
to_crawl['tld'] = url_unpack['tld'].decode()
|
|
|
|
except:
|
|
|
|
to_crawl['tld'] = url_unpack['tld']
|
2019-02-25 16:38:50 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
return to_crawl
|
|
|
|
|
2019-04-23 11:15:34 +02:00
|
|
|
def get_crawler_config(redis_server, mode, service_type, domain, url=None):
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_options = {}
|
2019-04-23 11:15:34 +02:00
|
|
|
if mode=='auto':
|
|
|
|
config = redis_server.get('crawler_config:{}:{}:{}:{}'.format(mode, service_type, domain, url))
|
|
|
|
else:
|
|
|
|
config = redis_server.get('crawler_config:{}:{}:{}'.format(mode, service_type, domain))
|
2019-02-25 16:38:50 +01:00
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
else:
|
|
|
|
config = json.loads(config)
|
|
|
|
for option in default_crawler_config:
|
|
|
|
if option in config:
|
|
|
|
crawler_options[option] = config[option]
|
|
|
|
else:
|
|
|
|
crawler_options[option] = default_crawler_config[option]
|
2019-02-26 14:50:48 +01:00
|
|
|
if mode == 'auto':
|
|
|
|
crawler_options['time'] = int(config['time'])
|
|
|
|
elif mode == 'manual':
|
|
|
|
redis_server.delete('crawler_config:{}:{}:{}'.format(mode, service_type, domain))
|
2019-02-25 16:38:50 +01:00
|
|
|
return crawler_options
|
|
|
|
|
2020-08-24 22:31:41 +02:00
|
|
|
def load_crawler_config(queue_type, service_type, domain, paste, url, date):
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_config = {}
|
2020-07-27 15:46:09 +02:00
|
|
|
crawler_config['splash_url'] = f'http://{splash_url}'
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_config['item'] = paste
|
|
|
|
crawler_config['service_type'] = service_type
|
|
|
|
crawler_config['domain'] = domain
|
|
|
|
crawler_config['date'] = date
|
|
|
|
|
2020-08-24 22:31:41 +02:00
|
|
|
if queue_type and queue_type != 'tor':
|
|
|
|
service_type = queue_type
|
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# Auto and Manual Crawling
|
2019-02-25 16:38:50 +01:00
|
|
|
# Auto ################################################# create new entry, next crawling => here or when ended ?
|
|
|
|
if paste == 'auto':
|
2019-04-23 11:15:34 +02:00
|
|
|
crawler_config['crawler_options'] = get_crawler_config(redis_crawler, 'auto', service_type, domain, url=url)
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_config['requested'] = True
|
|
|
|
# Manual
|
|
|
|
elif paste == 'manual':
|
|
|
|
crawler_config['crawler_options'] = get_crawler_config(r_cache, 'manual', service_type, domain)
|
2019-02-22 17:00:24 +01:00
|
|
|
crawler_config['requested'] = True
|
|
|
|
# default crawler
|
|
|
|
else:
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_config['crawler_options'] = get_crawler_config(redis_crawler, 'default', service_type, domain)
|
2019-02-22 17:00:24 +01:00
|
|
|
crawler_config['requested'] = False
|
|
|
|
return crawler_config
|
|
|
|
|
|
|
|
def is_domain_up_day(domain, type_service, date_day):
|
|
|
|
if redis_crawler.sismember('{}_up:{}'.format(type_service, date_day), domain):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-02-05 17:16:44 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
def set_crawled_domain_metadata(type_service, date, domain, father_item):
|
|
|
|
# first seen
|
|
|
|
if not redis_crawler.hexists('{}_metadata:{}'.format(type_service, domain), 'first_seen'):
|
|
|
|
redis_crawler.hset('{}_metadata:{}'.format(type_service, domain), 'first_seen', date['date_day'])
|
2018-09-27 15:43:03 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
redis_crawler.hset('{}_metadata:{}'.format(type_service, domain), 'paste_parent', father_item)
|
|
|
|
# last check
|
|
|
|
redis_crawler.hset('{}_metadata:{}'.format(type_service, domain), 'last_check', date['date_day'])
|
|
|
|
|
|
|
|
# Put message back on queue
|
|
|
|
def on_error_send_message_back_in_queue(type_service, domain, message):
|
|
|
|
if not redis_crawler.sismember('{}_domain_crawler_queue'.format(type_service), domain):
|
|
|
|
redis_crawler.sadd('{}_domain_crawler_queue'.format(type_service), domain)
|
|
|
|
redis_crawler.sadd('{}_crawler_priority_queue'.format(type_service), message)
|
|
|
|
|
2019-03-22 16:48:07 +01:00
|
|
|
def crawl_onion(url, domain, port, type_service, message, crawler_config):
|
2019-02-25 16:38:50 +01:00
|
|
|
crawler_config['url'] = url
|
2019-03-22 16:48:07 +01:00
|
|
|
crawler_config['port'] = port
|
2019-02-22 17:00:24 +01:00
|
|
|
print('Launching Crawler: {}'.format(url))
|
2018-08-13 09:23:14 +02:00
|
|
|
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'crawling_domain', domain)
|
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'started_time', datetime.datetime.now().strftime("%Y/%m/%d - %H:%M.%S"))
|
2019-01-29 12:00:14 +01:00
|
|
|
|
2018-12-17 16:04:12 +01:00
|
|
|
retry = True
|
|
|
|
nb_retry = 0
|
|
|
|
while retry:
|
|
|
|
try:
|
2020-07-27 15:46:09 +02:00
|
|
|
r = requests.get(f'http://{splash_url}' , timeout=30.0)
|
2018-12-17 16:04:12 +01:00
|
|
|
retry = False
|
|
|
|
except Exception:
|
|
|
|
# TODO: relaunch docker or send error message
|
|
|
|
nb_retry += 1
|
|
|
|
|
2020-07-24 08:54:54 +02:00
|
|
|
if nb_retry == 2:
|
2021-03-04 09:26:28 +01:00
|
|
|
crawlers.restart_splash_docker(splash_url, splash_name)
|
2021-04-01 10:24:29 +02:00
|
|
|
time.sleep(20)
|
2020-07-24 08:54:54 +02:00
|
|
|
|
2019-01-29 12:09:19 +01:00
|
|
|
if nb_retry == 6:
|
2019-02-22 17:00:24 +01:00
|
|
|
on_error_send_message_back_in_queue(type_service, domain, message)
|
2018-12-17 16:04:12 +01:00
|
|
|
publisher.error('{} SPASH DOWN'.format(splash_url))
|
|
|
|
print('--------------------------------------')
|
|
|
|
print(' \033[91m DOCKER SPLASH DOWN\033[0m')
|
|
|
|
print(' {} DOWN'.format(splash_url))
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'SPLASH DOWN')
|
2019-01-29 12:00:14 +01:00
|
|
|
nb_retry == 0
|
2018-12-17 16:04:12 +01:00
|
|
|
|
|
|
|
print(' \033[91m DOCKER SPLASH NOT AVAILABLE\033[0m')
|
|
|
|
print(' Retry({}) in 10 seconds'.format(nb_retry))
|
|
|
|
time.sleep(10)
|
2018-08-21 15:54:53 +02:00
|
|
|
|
|
|
|
if r.status_code == 200:
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'Crawling')
|
2019-02-25 16:38:50 +01:00
|
|
|
# save config in cash
|
|
|
|
UUID = str(uuid.uuid4())
|
|
|
|
r_cache.set('crawler_request:{}'.format(UUID), json.dumps(crawler_config))
|
|
|
|
|
|
|
|
process = subprocess.Popen(["python", './torcrawler/tor_crawler.py', UUID],
|
2018-08-21 15:54:53 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
while process.poll() is None:
|
|
|
|
time.sleep(1)
|
2018-08-16 17:24:39 +02:00
|
|
|
|
2018-08-21 15:54:53 +02:00
|
|
|
if process.returncode == 0:
|
2018-09-27 15:43:03 +02:00
|
|
|
output = process.stdout.read().decode()
|
|
|
|
print(output)
|
|
|
|
# error: splash:Connection to proxy refused
|
|
|
|
if 'Connection to proxy refused' in output:
|
2019-02-22 17:00:24 +01:00
|
|
|
on_error_send_message_back_in_queue(type_service, domain, message)
|
2018-09-28 15:23:27 +02:00
|
|
|
publisher.error('{} SPASH, PROXY DOWN OR BAD CONFIGURATION'.format(splash_url))
|
2018-09-27 15:43:03 +02:00
|
|
|
print('------------------------------------------------------------------------')
|
|
|
|
print(' \033[91m SPLASH: Connection to proxy refused')
|
|
|
|
print('')
|
|
|
|
print(' PROXY DOWN OR BAD CONFIGURATION\033[0m'.format(splash_url))
|
|
|
|
print('------------------------------------------------------------------------')
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'Error')
|
2018-09-27 15:43:03 +02:00
|
|
|
exit(-2)
|
2020-07-27 15:46:09 +02:00
|
|
|
else:
|
|
|
|
crawlers.update_splash_manager_connection_status(True)
|
2018-08-13 09:23:14 +02:00
|
|
|
else:
|
2018-08-21 15:54:53 +02:00
|
|
|
print(process.stdout.read())
|
2018-09-27 15:43:03 +02:00
|
|
|
exit(-1)
|
2018-08-21 15:54:53 +02:00
|
|
|
else:
|
2019-02-22 17:00:24 +01:00
|
|
|
on_error_send_message_back_in_queue(type_service, domain, message)
|
2018-09-27 15:43:03 +02:00
|
|
|
print('--------------------------------------')
|
|
|
|
print(' \033[91m DOCKER SPLASH DOWN\033[0m')
|
|
|
|
print(' {} DOWN'.format(splash_url))
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'Crawling')
|
2018-09-27 15:43:03 +02:00
|
|
|
exit(1)
|
2018-08-13 09:23:14 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# check external links (full_crawl)
|
|
|
|
def search_potential_source_domain(type_service, domain):
|
|
|
|
external_domains = set()
|
|
|
|
for link in redis_crawler.smembers('domain_{}_external_links:{}'.format(type_service, domain)):
|
|
|
|
# unpack url
|
|
|
|
url_data = unpack_url(link)
|
|
|
|
if url_data['domain'] != domain:
|
|
|
|
if url_data['tld'] == 'onion' or url_data['tld'] == 'i2p':
|
|
|
|
external_domains.add(url_data['domain'])
|
|
|
|
# # TODO: add special tag ?
|
|
|
|
if len(external_domains) >= 20:
|
|
|
|
redis_crawler.sadd('{}_potential_source'.format(type_service), domain)
|
|
|
|
print('New potential source found: domain')
|
|
|
|
redis_crawler.delete('domain_{}_external_links:{}'.format(type_service, domain))
|
|
|
|
|
|
|
|
|
2018-08-09 17:42:21 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2020-07-24 08:54:54 +02:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print('usage:', 'Crawler.py', 'splash_url')
|
2018-09-12 09:55:49 +02:00
|
|
|
exit(1)
|
2019-02-22 17:00:24 +01:00
|
|
|
##################################################
|
2020-07-24 08:54:54 +02:00
|
|
|
splash_url = sys.argv[1]
|
|
|
|
|
|
|
|
splash_name = crawlers.get_splash_name_by_url(splash_url)
|
2021-03-04 09:26:28 +01:00
|
|
|
proxy_name = crawlers.get_splash_proxy(splash_name)
|
2021-03-05 18:56:31 +01:00
|
|
|
crawler_type = crawlers.get_splash_crawler_type(splash_name)
|
2020-07-24 08:54:54 +02:00
|
|
|
|
2021-03-05 18:47:38 +01:00
|
|
|
print(f'SPLASH Name: {splash_name}')
|
|
|
|
print(f'Proxy Name: {proxy_name}')
|
2021-03-05 18:56:31 +01:00
|
|
|
print(f'Crawler Type: {crawler_type}')
|
2021-03-04 09:26:28 +01:00
|
|
|
|
2021-03-05 18:47:38 +01:00
|
|
|
#time.sleep(10)
|
2021-03-05 18:03:15 +01:00
|
|
|
#sys.exit(0)
|
2020-07-24 08:54:54 +02:00
|
|
|
|
|
|
|
#rotation_mode = deque(['onion', 'regular'])
|
2020-08-24 22:31:41 +02:00
|
|
|
all_crawler_queues = crawlers.get_crawler_queue_types_by_splash_name(splash_name)
|
|
|
|
rotation_mode = deque(all_crawler_queues)
|
|
|
|
print(rotation_mode)
|
2020-07-24 08:54:54 +02:00
|
|
|
|
|
|
|
default_proto_map = {'http': 80, 'https': 443}
|
|
|
|
######################################################## add ftp ???
|
2019-02-22 17:00:24 +01:00
|
|
|
|
|
|
|
publisher.port = 6380
|
|
|
|
publisher.channel = "Script"
|
|
|
|
publisher.info("Script Crawler started")
|
|
|
|
config_section = 'Crawler'
|
|
|
|
|
|
|
|
# Setup the I/O queues
|
|
|
|
p = Process(config_section)
|
2018-08-24 10:13:56 +02:00
|
|
|
|
2018-09-24 16:28:55 +02:00
|
|
|
print('splash url: {}'.format(splash_url))
|
2018-09-12 09:55:49 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
PASTES_FOLDER = os.path.join(os.environ['AIL_HOME'], p.config.get("Directories", "pastes"))
|
|
|
|
|
|
|
|
r_serv_metadata = redis.StrictRedis(
|
|
|
|
host=p.config.get("ARDB_Metadata", "host"),
|
|
|
|
port=p.config.getint("ARDB_Metadata", "port"),
|
|
|
|
db=p.config.getint("ARDB_Metadata", "db"),
|
|
|
|
decode_responses=True)
|
2018-08-09 17:42:21 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
r_cache = redis.StrictRedis(
|
|
|
|
host=p.config.get("Redis_Cache", "host"),
|
|
|
|
port=p.config.getint("Redis_Cache", "port"),
|
|
|
|
db=p.config.getint("Redis_Cache", "db"),
|
|
|
|
decode_responses=True)
|
|
|
|
|
|
|
|
redis_crawler = redis.StrictRedis(
|
|
|
|
host=p.config.get("ARDB_Onion", "host"),
|
|
|
|
port=p.config.getint("ARDB_Onion", "port"),
|
|
|
|
db=p.config.getint("ARDB_Onion", "db"),
|
|
|
|
decode_responses=True)
|
|
|
|
|
2021-03-05 18:47:38 +01:00
|
|
|
faup = crawlers.get_faup()
|
2019-02-25 16:38:50 +01:00
|
|
|
|
2019-07-24 10:18:10 +02:00
|
|
|
# get HAR files
|
|
|
|
default_crawler_har = p.config.getboolean("Crawler", "default_crawler_har")
|
|
|
|
if default_crawler_har:
|
2020-03-23 18:00:09 +01:00
|
|
|
default_crawler_har = True
|
2019-07-24 10:18:10 +02:00
|
|
|
else:
|
2020-03-23 18:00:09 +01:00
|
|
|
default_crawler_har = False
|
2019-07-24 10:18:10 +02:00
|
|
|
|
|
|
|
# get PNG files
|
|
|
|
default_crawler_png = p.config.getboolean("Crawler", "default_crawler_png")
|
|
|
|
if default_crawler_png:
|
2020-03-23 18:00:09 +01:00
|
|
|
default_crawler_png = True
|
2019-07-24 10:18:10 +02:00
|
|
|
else:
|
2020-03-23 18:00:09 +01:00
|
|
|
default_crawler_png = False
|
2019-07-24 10:18:10 +02:00
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
# Default crawler options
|
2020-03-23 18:00:09 +01:00
|
|
|
default_crawler_config = {'html': True,
|
2019-07-24 10:18:10 +02:00
|
|
|
'har': default_crawler_har,
|
|
|
|
'png': default_crawler_png,
|
2019-02-25 16:38:50 +01:00
|
|
|
'depth_limit': p.config.getint("Crawler", "crawler_depth_limit"),
|
2019-07-24 10:18:10 +02:00
|
|
|
'closespider_pagecount': p.config.getint("Crawler", "default_crawler_closespider_pagecount"),
|
2020-03-30 18:43:50 +02:00
|
|
|
'cookiejar_uuid': None,
|
2019-07-24 10:18:10 +02:00
|
|
|
'user_agent': p.config.get("Crawler", "default_crawler_user_agent")}
|
2019-02-25 16:38:50 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# Track launched crawler
|
2020-08-17 21:52:57 +02:00
|
|
|
r_cache.sadd('all_splash_crawlers', splash_url)
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'Waiting')
|
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'started_time', datetime.datetime.now().strftime("%Y/%m/%d - %H:%M.%S"))
|
2019-01-29 12:00:14 +01:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# update hardcoded blacklist
|
|
|
|
load_blacklist('onion')
|
|
|
|
load_blacklist('regular')
|
|
|
|
|
2018-08-09 17:42:21 +02:00
|
|
|
while True:
|
|
|
|
|
2019-02-26 14:50:48 +01:00
|
|
|
update_auto_crawler()
|
|
|
|
|
2019-05-15 09:57:18 +02:00
|
|
|
rotation_mode.rotate()
|
2020-07-24 08:54:54 +02:00
|
|
|
to_crawl = crawlers.get_elem_to_crawl_by_queue_type(rotation_mode)
|
2019-02-25 16:38:50 +01:00
|
|
|
if to_crawl:
|
2019-02-22 17:00:24 +01:00
|
|
|
url_data = unpack_url(to_crawl['url'])
|
|
|
|
# remove domain from queue
|
|
|
|
redis_crawler.srem('{}_domain_crawler_queue'.format(to_crawl['type_service']), url_data['domain'])
|
2018-08-21 15:54:53 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
print()
|
|
|
|
print()
|
|
|
|
print('\033[92m------------------START CRAWLER------------------\033[0m')
|
|
|
|
print('crawler type: {}'.format(to_crawl['type_service']))
|
|
|
|
print('\033[92m-------------------------------------------------\033[0m')
|
|
|
|
print('url: {}'.format(url_data['url']))
|
|
|
|
print('domain: {}'.format(url_data['domain']))
|
|
|
|
print('domain_url: {}'.format(url_data['domain_url']))
|
2019-03-22 16:48:07 +01:00
|
|
|
print()
|
2018-08-21 15:54:53 +02:00
|
|
|
|
2019-02-22 17:00:24 +01:00
|
|
|
# Check blacklist
|
2019-02-25 16:38:50 +01:00
|
|
|
if not redis_crawler.sismember('blacklist_{}'.format(to_crawl['type_service']), url_data['domain']):
|
|
|
|
date = {'date_day': datetime.datetime.now().strftime("%Y%m%d"),
|
|
|
|
'date_month': datetime.datetime.now().strftime("%Y%m"),
|
|
|
|
'epoch': int(time.time())}
|
2019-02-22 17:00:24 +01:00
|
|
|
|
2019-02-26 14:50:48 +01:00
|
|
|
# Update crawler status type
|
2020-08-17 21:52:57 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'type', to_crawl['type_service'])
|
2019-02-22 17:00:24 +01:00
|
|
|
|
2020-08-24 22:31:41 +02:00
|
|
|
crawler_config = load_crawler_config(to_crawl['queue_type'], to_crawl['type_service'], url_data['domain'], to_crawl['paste'], to_crawl['url'], date)
|
2019-02-22 17:00:24 +01:00
|
|
|
# check if default crawler
|
2019-02-26 14:50:48 +01:00
|
|
|
if not crawler_config['requested']:
|
|
|
|
# Auto crawl only if service not up this month
|
|
|
|
if redis_crawler.sismember('month_{}_up:{}'.format(to_crawl['type_service'], date['date_month']), url_data['domain']):
|
|
|
|
continue
|
2019-02-22 17:00:24 +01:00
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
set_crawled_domain_metadata(to_crawl['type_service'], date, url_data['domain'], to_crawl['paste'])
|
2019-02-22 17:00:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
#### CRAWLER ####
|
|
|
|
# Manual and Auto Crawler
|
|
|
|
if crawler_config['requested']:
|
|
|
|
|
|
|
|
######################################################crawler strategy
|
|
|
|
# CRAWL domain
|
2019-03-22 16:48:07 +01:00
|
|
|
crawl_onion(url_data['url'], url_data['domain'], url_data['port'], to_crawl['type_service'], to_crawl['original_message'], crawler_config)
|
2019-02-22 17:00:24 +01:00
|
|
|
|
|
|
|
# Default Crawler
|
|
|
|
else:
|
|
|
|
# CRAWL domain
|
2019-03-22 16:48:07 +01:00
|
|
|
crawl_onion(url_data['domain_url'], url_data['domain'], url_data['port'], to_crawl['type_service'], to_crawl['original_message'], crawler_config)
|
2019-02-25 16:38:50 +01:00
|
|
|
#if url != domain_url and not is_domain_up_day(url_data['domain'], to_crawl['type_service'], date['date_day']):
|
|
|
|
# crawl_onion(url_data['url'], url_data['domain'], to_crawl['original_message'])
|
2019-02-22 17:00:24 +01:00
|
|
|
|
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
# Save last_status day (DOWN)
|
|
|
|
if not is_domain_up_day(url_data['domain'], to_crawl['type_service'], date['date_day']):
|
|
|
|
redis_crawler.sadd('{}_down:{}'.format(to_crawl['type_service'], date['date_day']), url_data['domain'])
|
2019-02-22 17:00:24 +01:00
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
# if domain was UP at least one time
|
2019-03-22 16:48:07 +01:00
|
|
|
if redis_crawler.exists('crawler_history_{}:{}:{}'.format(to_crawl['type_service'], url_data['domain'], url_data['port'])):
|
2019-02-25 16:38:50 +01:00
|
|
|
# add crawler history (if domain is down)
|
2019-03-22 16:48:07 +01:00
|
|
|
if not redis_crawler.zrangebyscore('crawler_history_{}:{}:{}'.format(to_crawl['type_service'], url_data['domain'], url_data['port']), date['epoch'], date['epoch']):
|
2019-02-25 16:38:50 +01:00
|
|
|
# Domain is down
|
2019-03-22 16:48:07 +01:00
|
|
|
redis_crawler.zadd('crawler_history_{}:{}:{}'.format(to_crawl['type_service'], url_data['domain'], url_data['port']), int(date['epoch']), int(date['epoch']))
|
2019-02-22 17:00:24 +01:00
|
|
|
|
|
|
|
############################
|
|
|
|
# extract page content
|
|
|
|
############################
|
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
# update list, last crawled domains
|
2019-03-22 16:48:07 +01:00
|
|
|
redis_crawler.lpush('last_{}'.format(to_crawl['type_service']), '{}:{};{}'.format(url_data['domain'], url_data['port'], date['epoch']))
|
2019-02-25 16:38:50 +01:00
|
|
|
redis_crawler.ltrim('last_{}'.format(to_crawl['type_service']), 0, 15)
|
2018-08-21 15:54:53 +02:00
|
|
|
|
2019-02-25 16:38:50 +01:00
|
|
|
#update crawler status
|
2020-06-09 18:33:41 +02:00
|
|
|
r_cache.hset('metadata_crawler:{}'.format(splash_url), 'status', 'Waiting')
|
|
|
|
r_cache.hdel('metadata_crawler:{}'.format(splash_url), 'crawling_domain')
|
2019-01-29 12:00:14 +01:00
|
|
|
|
2019-02-26 14:50:48 +01:00
|
|
|
# Update crawler status type
|
2020-08-17 21:52:57 +02:00
|
|
|
r_cache.hdel('metadata_crawler:{}'.format(splash_url), 'type', to_crawl['type_service'])
|
2019-02-26 14:50:48 +01:00
|
|
|
|
|
|
|
# add next auto Crawling in queue:
|
|
|
|
if to_crawl['paste'] == 'auto':
|
|
|
|
redis_crawler.zadd('crawler_auto_queue', int(time.time()+crawler_config['crawler_options']['time']) , '{};{}'.format(to_crawl['original_message'], to_crawl['type_service']))
|
2019-04-18 16:57:51 +02:00
|
|
|
# update list, last auto crawled domains
|
|
|
|
redis_crawler.lpush('last_auto_crawled', '{}:{};{}'.format(url_data['domain'], url_data['port'], date['epoch']))
|
|
|
|
redis_crawler.ltrim('last_auto_crawled', 0, 9)
|
2018-08-09 17:42:21 +02:00
|
|
|
else:
|
2019-02-25 16:38:50 +01:00
|
|
|
print(' Blacklisted Domain')
|
|
|
|
print()
|
|
|
|
print()
|
|
|
|
|
2018-08-09 17:42:21 +02:00
|
|
|
else:
|
|
|
|
time.sleep(1)
|