# Path: collectors/icdpcs/record.py
# class Record(base.Record):
# 
#     # Config
# 
#     table = 'icdpcs'
# 
#     # General
# 
#     code = Text(primary_key=True)
#     is_header = Boolean('0')
#     short_description = Text()
#     long_description = Text()
#     version = Text()
#     last_updated = Date('%Y-%m-%d')


import io
import logging
import zipfile
import requests
from .record import Record
def collect(conf, conn):
    """Collect ICD-XX-PCS procedures.
    """

    # For more information see:
    # https://www.cms.gov/Medicare/Coding/ICD10/2016-ICD-10-PCS-and-GEMs.html
    URL = 'https://www.cms.gov/Medicare/Coding/ICD10/Downloads/2016-PCS-Long-Abbrev-Titles.zip'
    FILE = 'icd10pcs_order_2016.txt'
    VERSION = 'ICD-10-PCS'
    LAST_UPDATED = '2015-10-01'

    # Prepare file
    zip = requests.get(URL).content
    file = zipfile.ZipFile(io.BytesIO(zip)).open(FILE)

    count = 0
    for line in file:
        # Prepare data
        # Format is described in instruction
        # stored in zip archive we download
        data = {
            'code': line[6:6+7].strip(),
            'is_header': line[14:14+1].strip(),
            'short_description': line[16:16+60].strip(),
            'long_description': line[77:].strip(),
            'version': VERSION,
            'last_updated': LAST_UPDATED,
        }

        # Create record

# Path: collectors/icdpcs/record.py
# class Record(base.Record):
# 
#     # Config
# 
#     table = 'icdpcs'
# 
#     # General
# 
#     code = Text(primary_key=True)
#     is_header = Boolean('0')
#     short_description = Text()
#     long_description = Text()
#     version = Text()
#     last_updated = Date('%Y-%m-%d')


# Path: collectors/icdpcs/collector.py
import io
import logging
import zipfile
import requests
from .record import Record

def collect(conf, conn):
    """Collect ICD-XX-PCS procedures.
    """

    # For more information see:
    # https://www.cms.gov/Medicare/Coding/ICD10/2016-ICD-10-PCS-and-GEMs.html
    URL = 'https://www.cms.gov/Medicare/Coding/ICD10/Downloads/2016-PCS-Long-Abbrev-Titles.zip'
    FILE = 'icd10pcs_order_2016.txt'
    VERSION = 'ICD-10-PCS'
    LAST_UPDATED = '2015-10-01'

    # Prepare file
    zip = requests.get(URL).content
    file = zipfile.ZipFile(io.BytesIO(zip)).open(FILE)

    count = 0
    for line in file:
        # Prepare data
        # Format is described in instruction
        # stored in zip archive we download
        data = {
            'code': line[6:6+7].strip(),
            'is_header': line[14:14+1].strip(),
            'short_description': line[16:16+60].strip(),
            'long_description': line[77:].strip(),
            'version': VERSION,
            'last_updated': LAST_UPDATED,
        }

        # Create record

        record = Record.create(URL, data)