<commit_before>package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"path"
	"strconv"
	"time"

	"github.com/cnt0/cfapi"
	"github.com/cnt0/cfapi/user"
	"github.com/cnt0/cfsubmit"
)

const settingsFileName = "cfsubmit_settings.json"

const (
	TimeLimitSeconds = 60
)

var (
	contestId int
	problemId string
	langId    string
)

var (
	errNoSubmission = errors.New("Submission file not specified")
	errUnkownExt    = errors.New("Unknown extension. Ext must be in lowercase in your settings file")
)

var CFAuthData *cfsubmit.CFSettings

func init() {
	//load settings from json
	var err error
	CFAuthData, err = cfsubmit.ReadSettings()
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	if len(os.Args) < 2 {
		fmt.Println(errNoSubmission)
		os.Exit(0)
	}

	submission, err := cfapi.NewSubmission(path.Base(os.Args[1]))
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	contestId = submission.ContestID
	problemId = submission.Problem.Index
	if l, ok := CFAuthData.ExtId[submission.ProgrammingLanguage]; !ok {
		fmt.Println(errUnkownExt)
		os.Exit(0)
	} else {
		langId = l
	}
}

func createMultipartForm() (io.Reader, string, error) {
	solutionText, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		return nil, "", err
	}

	//multipart form field: name - value
	formFields := [][]string{
		[]string{"csrf_token", CFAuthData.CSRF},
		[]string{"action", "submitSolutionFormSubmitted"},
		[]string{"submittedProblemIndex", problemId},
		[]string{"programTypeId", langId},
		[]string{"sourceFile", ""},
		[]string{"_tta", "222"},
		[]string{"source", string(solutionText)},
	}

	//cause bytes.Buffer implements both io.Reader and io.Writer
	var b bytes.Buffer
	formWriter := multipart.NewWriter(&b)

	for _, field := range formFields {
		if err := formWriter.WriteField(field[0], field[1]); err != nil {
			return nil, "", err
		}
	}

	if err := formWriter.Close(); err != nil {
		return nil, "", err
	}

	return &b, formWriter.Boundary(), nil
}

func main() {
	//request url
	reqUrl := "http://codeforces." + CFAuthData.CFDomain +
		"/contest/" + strconv.Itoa(contestId) +
		"/problem/" + problemId +
		"?csrf_token=" + CFAuthData.CSRF

	//get request body data; boundary for header
	form, boundary, err := createMultipartForm()
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	//ok, construct request
	req, err := http.NewRequest("POST", reqUrl, form)
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	//add required headers and cookies
	req.Header.Set("Content-Type", "multipart/form-data; boundary="+boundary)
	req.AddCookie(&http.Cookie{Name: "X-User", Value: CFAuthData.XUser})
<commit_msg>fixed args parsing on windows<commit_after>package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"path"
	"runtime"
	"strconv"
	"strings"
	"time"

	"github.com/cnt0/cfapi"
	"github.com/cnt0/cfapi/user"
	"github.com/cnt0/cfsubmit"
)

const settingsFileName = "cfsubmit_settings.json"

const (
	TimeLimitSeconds = 60
)

var (
	contestId int
	problemId string
	langId    string
)

var (
	errNoSubmission = errors.New("Submission file not specified")
	errUnkownExt    = errors.New("Unknown extension. Ext must be in lowercase in your settings file")
)

var CFAuthData *cfsubmit.CFSettings

func init() {

	//load settings from json
	var err error
	CFAuthData, err = cfsubmit.ReadSettings()
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	if len(os.Args) < 2 {
		fmt.Println(errNoSubmission)
		os.Exit(0)
	}

	var submission *cfapi.Submission
	if runtime.GOOS == "windows" {
		submission, err = cfapi.NewSubmission(path.Base(strings.Replace(os.Args[1], "\\", "/", -1)))
	} else {
		submission, err = cfapi.NewSubmission(path.Base(os.Args[1]))
	}

	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	contestId = submission.ContestID
	problemId = submission.Problem.Index
	if l, ok := CFAuthData.ExtId[submission.ProgrammingLanguage]; !ok {
		fmt.Println(errUnkownExt)
		os.Exit(0)
	} else {
		langId = l
	}
}

func createMultipartForm() (io.Reader, string, error) {
	solutionText, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		return nil, "", err
	}

	//multipart form field: name - value
	formFields := [][]string{
		[]string{"csrf_token", CFAuthData.CSRF},
		[]string{"action", "submitSolutionFormSubmitted"},
		[]string{"submittedProblemIndex", problemId},
		[]string{"programTypeId", langId},
		[]string{"sourceFile", ""},
		[]string{"_tta", "222"},
		[]string{"source", string(solutionText)},
	}

	//cause bytes.Buffer implements both io.Reader and io.Writer
	var b bytes.Buffer
	formWriter := multipart.NewWriter(&b)

	for _, field := range formFields {
		if err := formWriter.WriteField(field[0], field[1]); err != nil {
			return nil, "", err
		}
	}

	if err := formWriter.Close(); err != nil {
		return nil, "", err
	}

	return &b, formWriter.Boundary(), nil
}

func main() {

	//request url
	reqUrl := "http://codeforces." + CFAuthData.CFDomain +
		"/contest/" + strconv.Itoa(contestId) +
		"/problem/" + problemId +
		"?csrf_token=" + CFAuthData.CSRF

	//get request body data; boundary for header
	form, boundary, err := createMultipartForm()
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	//ok, construct request
	req, err := http.NewRequest("POST", reqUrl, form)
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	//add required headers and cookies
	req.Header.Set("Content-Type", "multipart/form-data; boundary="+boundary)
	req.AddCookie(&http.Cookie{Name: "X-User", Value: CFAuthData.XUser})

{'fraction_non_alphanumeric': 0.15899657101606207, 'fraction_numerical': 0.007218913553510196, 'mean_word_length': 4.010849909584087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4584533', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2069, 'n_words': 457}