Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

add --zip option to zip the submission file prior to uploading it #52

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
*/__pycache__/
*/*.pyc
.kaggle-cli
.idea/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ To submit an entry.
$ kg submit <submission-file> -u <username> -p <password> -c <competition> -m "<message>"
```

Optionally, add `-z` to zip the submission file before uploading:

```
$ kg submit <submission-file> -u <username> -p <password> -c <competition> -z -m "<message>"
```

### Download
To download the data files (resumable).

Expand Down Expand Up @@ -53,6 +59,8 @@ $ kg dataset -u <username> -p <password> -o <owner> -d <dataset>
### Config
To set global config.

> Optional: add `-z` to zip submission files before uploading.

```
$ kg config -g -u <username> -p <password> -c <competition>
```
Expand Down
48 changes: 45 additions & 3 deletions kaggle_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@

CONFIG_DIR_NAME = '.kaggle-cli'
CONFIG_FILE_NAME = 'config'
DATA_OPTIONS = set(['username', 'password', 'competition'])
FIELD_OPTIONS = {
'username': {
'type': str
},
'password': {
'type': str
},
'competition': {
'type': str
},
'zip': {
'type': bool
}
}


def get_config(config_path):
Expand Down Expand Up @@ -49,11 +62,30 @@ def merge_dicts(x, y={}):
return z


def config_section_to_dict(config, section, field_options):
result_dict = {}
for name, spec in field_options.items():
if spec['type'] == bool:
value = config.getboolean(section, name, fallback=None)
elif spec['type'] == int:
value = config.getint(section, name, fallback=None)
elif spec['type'] == float:
value = config.getfloat(section, name, fallback=None)
else:
value = config.get(section, name, fallback=None)
if value:
result_dict[name] = value
return result_dict


def get_working_config(configs):
return reduce(
lambda working_config, config:
merge_dicts(config, working_config),
map(lambda config: dict(config['user']), configs),
map(
lambda config:
config_section_to_dict(config, 'user', FIELD_OPTIONS),
configs),
{}
)

Expand Down Expand Up @@ -85,6 +117,11 @@ def get_parser(self, prog_name):
parser.add_argument('-u', '--username', help='username')
parser.add_argument('-p', '--password', help='password')
parser.add_argument('-c', '--competition', help='competition')
parser.add_argument(
'-z',
'--zip',
help='zip the submission file before uploading?',
action='store_true')
parser.add_argument(
'-g',
'--global',
Expand All @@ -97,7 +134,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
parsed_arg_dict = vars(parsed_args)

if DATA_OPTIONS & set(
if set(FIELD_OPTIONS.keys()) & set(
filter(lambda x: parsed_arg_dict[x], parsed_arg_dict)
):
if parsed_arg_dict['global']:
Expand Down Expand Up @@ -135,6 +172,11 @@ def take_action(self, parsed_args):
parsed_arg_dict['competition']
)

if parsed_arg_dict['zip']:
config.set(
'user','zip','yes'
)

with open(config_path, 'w') as config_file:
config.write(config_file)
else:
Expand Down
2 changes: 1 addition & 1 deletion kaggle_cli/meta.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.12.8'
VERSION = '0.12.10'
62 changes: 52 additions & 10 deletions kaggle_cli/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import time
import re
import json
import sys
import uuid
from argparse import ArgumentTypeError
import zipfile

from cliff.command import Command

Expand All @@ -21,6 +25,7 @@ def get_parser(self, prog_name):
parser.add_argument('-c', '--competition', help='competition')
parser.add_argument('-u', '--username', help='username')
parser.add_argument('-p', '--password', help='password')
parser.add_argument('-z', '--zip', help='zip the submission file before uploading?', action='store_true')

return parser

Expand All @@ -30,16 +35,23 @@ def take_action(self, parsed_args):
username = config.get('username', '')
password = config.get('password', '')
competition = config.get('competition', '')
zip = config.get('zip', False)

browser = common.login(username, password)
base = 'https://www.kaggle.com'
competition_url = '/'.join([base, 'c', competition])
file_form_submit_url = '/'.join([base, 'blobs/inbox/submissions'])
entry_form_submit_url = '/'.join([competition_url, 'submission.json'])
file_form_url = '/'.join([base, 'blobs/inbox/submissions'])
entry_form_url = '/'.join([competition_url, 'submission.json'])

entry = parsed_args.entry
message = parsed_args.message

archive_name = make_archive_name(entry)

if zip:
with zipfile.ZipFile(archive_name, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(entry)

competition_page = browser.get(competition_url)

if competition_page.status_code == 404:
Expand All @@ -51,35 +63,44 @@ def take_action(self, parsed_args):
str(competition_page.soup)
).group(1)

if zip:
target_name = archive_name
else:
target_name = entry

form_submission = browser.post(
file_form_submit_url,
file_form_url,
data={
'fileName': entry,
'contentLength': os.path.getsize(entry),
'lastModifiedDateUtc': int(os.path.getmtime(entry) * 1000)
'fileName': target_name,
'contentLength': os.path.getsize(target_name),
'lastModifiedDateUtc': int(os.path.getmtime(target_name) * 1000)
}
).json()

file_submit_url = base + form_submission['createUrl']

with open(entry, 'rb') as submission_file:
with open(target_name, 'rb') as submission_file:
token = browser.post(
file_submit_url,
files={
'file': submission_file
}
).json()['token']

browser.post(
entry_form_submit_url,
entry_form_resp_message = browser.post(
entry_form_url,
data=json.dumps({
'blobFileTokens': [token],
'submissionDescription': message if message else ''
}),
headers={
'Content-Type': 'application/json'
}
)
).json()['pageMessages']

if entry_form_resp_message and entry_form_resp_message[0]['type'] == 'error':
print(entry_form_resp_message[0]['dangerousHtmlMessage'])
return

status_url = (
'https://www.kaggle.com/'
Expand All @@ -98,3 +119,24 @@ def take_action(self, parsed_args):
else:
print('something went wrong')
break

if zip:
os.remove(target_name)


def make_archive_name(original_file_path):

# if original name already has a suffix (csv,txt,etc), remove it
extension_pattern = r'(^.+)\.(.+)$'

# file may be in another directory
original_basename = os.path.basename(original_file_path)

if re.match(extension_pattern,original_basename):
archive_name = re.sub(extension_pattern,r'\1.zip',original_basename)
else:
archive_name = original_basename+".zip"

original_directory_path = os.path.dirname(original_file_path)

return os.path.join(original_directory_path,archive_name)