#!/usr/bin/env python
# coding=utf-8

from billiard import Process
from base64 import b64encode

from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

settings = get_project_settings()


class Crawler():

    def __init__(self):
        self.crawler = CrawlerProcess(settings)


class OJInitCrawler(Crawler):

    def _crawl(self, oj):
        self.crawler.crawl(
            oj + '_init',
        )
        self.crawler.start()
        self.crawler.stop()

    def crawl(self, oj):
        p = Process(
            target=self._crawl,
            args=[oj]
        )
        p.start()
        p.join()


class ProblemCrawler(Crawler):

    def _crawl(self, oj, problem_id):
        self.crawler.crawl(
                oj + '_problem',
                problem_id=problem_id
            )
        self.crawler.start()
        self.crawler.stop()

    def crawl(self, oj, problem_id):
        p = Process(
            target=self._crawl,
            args=[
                oj,
                problem_id
            ]
        )
        p.start()
        p.join()
    

class StatusCrawler(Crawler):

    def _crawl(self, oj, run_id):
        self.crawler.crawl(
            oj + '_status',
            run_id=run_id
        )
        self.crawler.start()
        self.crawler.stop()

    def crawl(self, oj, run_id):
        p = Process(
            target=self._crawl,
            args=[
                oj,
                run_id
            ]
        )
        p.start()
        p.join()


class CodeSubmitCrawler(Crawler):

    def _crawl(
            self,
            oj,
            problem_id,
            language,
            code,
            username,
            nickname,
            password):
        self.crawler.crawl(
            oj + '_submit',
            problem_id=problem_id,
            language=language,
            source=b64encode(code),
            username=username,
            nickname=nickname,
            password=password
        )
        self.crawler.start()
        self.crawler.stop()

    def crawl(
            self,
            oj,
            solution_id,
            problem_id,
            language,
            code,
            username,
            nickname,
            password):
        p = Process(
            target=self._crawl,
            args=[
                oj,
                solution_id,
                problem_id,
                language,
                code,
                username,
                nickname,
                password
            ]
        )
        p.start()
        p.join()


class AccountCrawler(Crawler):

    def _crawl(self, oj, username, password):
        self.crawler.crawl(
            oj + '_user',
            username=username,
            password=password
        )
        self.crawler.start()
        self.crawler.stop()

    def crawl(self, oj, username, password):
        p = Process(
            target=self._crawl,
            args=[
                oj,
                username,
                password
            ]
        )
        p.start()
        p.join()

@@ -63,37 +63,8 @@
 cess
-%0Afrom base64 import b64encode
 %0A%0Afr
@@ -1669,38 +1669,16 @@
 ername,%0A
-            nickname,%0A
         
@@ -1837,23 +1837,12 @@
 rce=
-b64encode(code)
+code
 ,%0A  
@@ -1874,39 +1874,8 @@
 me,%0A
-            nickname=nickname,%0A
     
@@ -2021,33 +2021,8 @@
 oj,%0A
-            solution_id,%0A
     
@@ -2103,38 +2103,16 @@
 ername,%0A
-            nickname,%0A
         
@@ -2222,37 +2222,8 @@
 oj,%0A
-                solution_id,%0A
     
@@ -2324,34 +2324,8 @@
 me,%0A
-                nickname,%0A
     

update crawl