import json
from scraperAPI.task import Task
from scraperAPI.error import APIError
from scraperAPI.log import Log
from scraperAPI.request import Request

from scraperAPI.base import Config, APIDBConnection

class ScraperAPI:
	def __init__(self, config_json=None, config_filename=None):
		self.config = Config(config_json=config_json, config_filename=config_filename)
		self.task = Task()
		self.error = APIError()
		self.log = Log()
		self.request = Request()
	
	def get_db_connection(self):
		return APIDBConnection()

	def handle_response(self, result, task_id, name):
		did_error = False
		if "output" in result:
			output = result["output"]

			# This is where all results of scraping should be saved using their respective endpoints

			if "logs" in output:
				for log in result["logs"]:
					if log is None:
						continue
					if log["type"] == 'error':
						self.error.insert({ "error": "Failure during scraping script: " + log["data"] })
						did_error = True
					if log["type"] == 'navigation':
						url = log["data"]
						self.request.insert({'url':url.replace("'","''").replace('"','\"'), 'task_id':task_id, 'sent_by': name })
						if 'captcha' in url:
							self.error.insert({ 'error': "scraperAPI discovered a captcha in a request when processing the logs of task " + task_id })
							did_error = True
				self.log.insert({ 'log_json': json.dumps(result["logs"]), 'task_id':task_id })

		# This allows the autoscraper to know immediately to stop scraping
		return did_error
