<commit_before>from datetime import datetime

import scrapy

from fossil_gov.application_details import extract_application_detail
from fossil_gov.items import FossilGovItem


# TODO: I/E/B
# TODO: Term
# TODO: Status


def extract_cell(column_number):
    def f(row):
        return row.xpath('normalize-space(td[{}])'.format(
            column_number))[0].extract().strip()
    return f


def extract_date_from_cell(column_number):
    def f(row):
        text = extract_cell(column_number)(row)
        if text:
            return datetime.strptime(text, '%d-%b-%Y').date()
        return None
    return f


def extract_order_number(row):
    number = int(extract_cell(3)(row)) or None
    status = extract_cell(5)(row)
    return {'number': number, 'status': status}


PARSERS = {
    'applicant': extract_cell(6),
    'docket_number': extract_cell(2),
    'exp_date': extract_date_from_cell(13),
    'filed_date': extract_date_from_cell(10),
    'init_date': extract_date_from_cell(12),
    'issue_date': extract_date_from_cell(11),
    'order_number': extract_order_number,
    'application_detail': extract_application_detail,
}


class FossilGovScraper(scrapy.Spider):
    name = "fossil_gov"
    allowed_domains = ["app.fossil.energy.gov"]

    def _get_request(self, year):
        return scrapy.FormRequest(
            "https://app.fossil.energy.gov/app/fergas/DocketOrderList.go",
            formdata={
                'appFiledYear': unicode(year),
                'orderBy': 'docket',
                'ascOrder': 'false',
                'fromNum': unicode(self.from_num),
            },
            callback=self.parse,
        )

    def start_requests(self):
        self.from_num = 0
        return [self._get_request(2014)]

    def parse(self, response):
        if response.xpath('//a[contains(., ">>")]'):<commit_msg>Alphabetise keys.<commit_after>from datetime import datetime

import scrapy

from fossil_gov.application_details import extract_application_detail
from fossil_gov.items import FossilGovItem


# TODO: I/E/B
# TODO: Term
# TODO: Status


def extract_cell(column_number):
    def f(row):
        return row.xpath('normalize-space(td[{}])'.format(
            column_number))[0].extract().strip()
    return f


def extract_date_from_cell(column_number):
    def f(row):
        text = extract_cell(column_number)(row)
        if text:
            return datetime.strptime(text, '%d-%b-%Y').date()
        return None
    return f


def extract_order_number(row):
    number = int(extract_cell(3)(row)) or None
    status = extract_cell(5)(row)
    return {'number': number, 'status': status}


PARSERS = {
    'applicant': extract_cell(6),
    'application_detail': extract_application_detail,
    'docket_number': extract_cell(2),
    'exp_date': extract_date_from_cell(13),
    'filed_date': extract_date_from_cell(10),
    'init_date': extract_date_from_cell(12),
    'issue_date': extract_date_from_cell(11),
    'order_number': extract_order_number,
}


class FossilGovScraper(scrapy.Spider):
    name = "fossil_gov"
    allowed_domains = ["app.fossil.energy.gov"]

    def _get_request(self, year):
        return scrapy.FormRequest(
            "https://app.fossil.energy.gov/app/fergas/DocketOrderList.go",
            formdata={
                'appFiledYear': unicode(year),
                'orderBy': 'docket',
                'ascOrder': 'false',
                'fromNum': unicode(self.from_num),
            },
            callback=self.parse,
        )

    def start_requests(self):
        self.from_num = 0
        return [self._get_request(2014)]

    def parse(self, response):
        if response.xpath('//a[contains(., ">>")]'):
{'fraction_non_alphanumeric': 0.12926829268292683, 'fraction_numerical': 0.00975609756097561, 'mean_word_length': 2.9902702702702704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711203', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1175, 'n_words': 215}