# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from dotenv import load_dotenv, find_dotenv

# find dotenv file walking up directories until it is found
dotenv_path=find_dotenv()
load_dotenv(dotenv_path)

import os
KAGGLE_USERNAME=os.environ.get("KAGGLE_USERNAME")
print(KAGGLE_USERNAME)

import requests
from requests import session
import os
from dotenv import load_dotenv, find_dotenv

# +
# NOTE: This is redundant as Kaggle API need to be used to download data and post doesn't work anymore
#payload for post
payload = {
          'action': 'login',
          'username': os.environ.get("KAGGLE_USERNAME"),
          'password': <PASSWORD>("KAGGLE_PASSWORD")
          }
url='https://www.kaggle.com/c/titanic/download/train.csv'

# setup session
with session() as c:
    c.post('https://www.kaggle.com/account/login', data=payload)
    response=c.get(url)
    
# -

# API keys can be created from the Kaggle account and stored in C://Users/<windows-name>/.kaggle/kaggle.json
def extract_data(file_name, file_path):
    # !kaggle competitions download titanic -f $file_name -p $file_path  --force


# +
# file names
train_file_name="train.csv"
test_file_name="test.csv"

# file paths
raw_data_path = os.path.join(os.path.pardir,'data', 'raw')
train_data_path = os.path.join(raw_data_path, 'train.csv')
test_data_path = os.path.join(raw_data_path, 'test.csv')
extract_data(train_file_name, raw_data_path)
extract_data(test_file_name, raw_data_path)

# -

# Building the reproducible python file script
get_raw_data_script_file=os.path.join(os.path.pardir, 'src', 'data','get_raw_data.py')

# +
# %%writefile $get_raw_data_script_file
# Coding #
import os
import logging


def extract_data(file_name, file_path):
    # Inside python script write os.system to emulate command line and put the command
     os.system('kaggle competitions download titanic -f {} -p {}  --force'.format(file_name, file_path))
    
def main(project_dir):
    '''
    main method
    '''
    # get logger
    logger = logging.getLogger(__name__)
    logger.info('getting raw data')
    # file names
    train_file_name="train.csv"
    test_file_name="test.csv" 
    # file paths 
    raw_data_path = os.path.join(os.path.pardir,'data', 'raw')
    train_data_path = os.path.join(raw_data_path, 'train.csv')
    test_data_path = os.path.join(raw_data_path, 'test.csv')
    extract_data(train_file_name, raw_data_path)
    extract_data(test_file_name, raw_data_path)
    logger.info('downloaded raw training and test data from kaggle')

if __name__ == '__main__':
    # getting root directory
    project_dir=os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
    
    # setup logger
    log_fmt= '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level = logging.INFO, format=log_fmt)
    
    # call the main method
    main(project_dir)

else:
    raise Exception('main method not found')
    
# -

# !python $get_raw_data_script_file



StarcoderdataJupyterScriptsDedupFiltered