<commit_before>var Dropbox = require('dropbox');
var request = require('request');
var xml2js = require('xml2js');
var md5 = require('md5');
var fs = require('fs');

var db_key = '<KEY>';
var db_secret = '<KEY>';
var db_accesstoken = '<KEY>'; //TODO: get this with OAUTH
var ss_key = '<KEY>';

var dbx = new Dropbox({ accessToken: db_accesstoken });


// gets all the files from dropbox recursively
var listFiles = function(dbx, cursor) {
    if (!cursor) {
        dbx.filesListFolder({ path: '', recursive: true })
            .then(function(response) {
                response.entries.forEach(function(item) {
                    console.log(item);                    
                });
                if (response.has_more) {
                    listFiles(dbx, response.cursor);
            }})
            .catch(function(error) {
                console.log(error);
            });
    } else {
        dbx.filesListFolderContinue({ cursor: cursor })
            .then(function(response) {
                response.entries.forEach(function(item) {
                    console.log(item);                    
                });
                if (response.has_more) {
                    listFiles(dbx, response.cursor);
            }})
            .catch(function(error) {
                console.log(error);
            });
    }
}

// gets sendspace sessionkey for current session
var getSendspaceSessionkey = function(handler) {
    var token;
    request('http://api.sendspace.com/rest/?method=auth.createtoken&api_key=LN5IKXSIF8&api_version=1.2', function(error, response, body) {
        var parser = new xml2js.Parser({explicitArray: false});
        parser.parseString(body, function(err, result) {
            token = result.result.token;
        });
        
        if (token) {
            var email = '<EMAIL>';
            var pw = '<PASSWORD>';
            var tokened_pw = md5(token + md5(pw).toLowerCase()).toLowerCase();
            
            request('http://api.sendspace.com/rest/?method=auth.login&token='+token+'&user_name='+email+'&tokened_password='+tokened_pw, function(error, response, body) {
                parser.parseString(body, function(err, result) {
                    handler(result.result.session_key);
                });
            });
        }
    });
}

var getUploadUrl = function(session_key) {
    request('http://api.sendspace.com/rest/?method=upload.getinfo&session_key='+session_key, function(error, response, body) {
        var parser = new xml2js.Parser({explicitArray: false});
        parser.parseString(body, function(err, result) {
            var uploadObj = result.result.upload.$;
            console.log(uploadObj);
            
            var formData = {
                MAX_FILE_SIZE: uploadObj.max_file_size,
                UPLOAD_IDENTIFIER: uploadObj.upload_identifier,
                extra_info: uploadObj.extra_info,
                userfile: fs.createReadStream('testi.txt')
            };

            request.post({url:uploadObj.url, formData: formData}, function(error, response, body) {
                console.log(body);
            });
        });
    });
}

module.exports = function() {
    //listFiles(dbx);
    getSendspaceSessionkey(getUploadUrl);
    
    return "Terve mualima!";
}<commit_msg>sync file from dropbox to sendspace, hardcoded values, needs a lot of refactoring<commit_after>var Dropbox = require('dropbox');
var request = require('request');
var xml2js = require('xml2js');
var md5 = require('md5');
var fs = require('fs');

var db_key = '3a30dgfp45ufpmh';
var db_secret = '<KEY>';
var db_accesstoken = '<KEY>'; //TODO: get this with OAUTH
var ss_key = '<KEY>';

var dbx = new Dropbox({ accessToken: db_accesstoken });


// gets all the files from dropbox recursively
var listDropBoxFiles = function(dbx, cursor) {
    if (!cursor) {
        dbx.filesListFolder({ path: '', recursive: true })
            .then(function(response) {
                response.entries.forEach(function(item) {
                    console.log(item);                    
                });
                if (response.has_more) {
                    listFiles(dbx, response.cursor);
            }})
            .catch(function(error) {
                console.log(error);
            });
    } else {
        dbx.filesListFolderContinue({ cursor: cursor })
            .then(function(response) {
                response.entries.forEach(function(item) {
                    console.log(item);                    
                });
                if (response.has_more) {
                    listFiles(dbx, response.cursor);
            }})
            .catch(function(error) {
                console.log(error);
            });
    }
}

var listSendSpaceFiles = function(session_key) {
    request('http://api.sendspace.com/rest/?method=folders.getcontents&session_key='+session_key+'&folder_id=0', function(error, response, body) {
        console.log(body);
    });
}

var getDropBoxDownloadParams = function(filepath, authToken) {
    return {
        url: 'https://content.dropboxapi.com/2/files/download',
        headers: {
            'Dropbox-API-Arg': '{"path": "'+filepath+'"}',
            'Authorization': 'Bearer '+authToken
        }
    }
}

var sendSpaceUploadResponseToJson = function(response) {
    var responseObj = {};
    var responseLines = response.split('\n');
    responseLines.forEach(function(line) {
        if(line) {
            var splittedLine = line.split('=');
            responseObj[splittedLine[0]] = splittedLine[1];
        }
    });
    return responseObj;
}

// gets sendspace sessionkey for current session
var getSendspaceSessionkey = function(handler) {
    var token;
    request('http://api.sendspace.com/rest/?method=auth.createtoken&api_key=LN5IKXSIF8&api_version=1.2', function(error, response, body) {
        var parser = new xml2js.Parser({explicitArray: false});
        parser.parseString(body, function(err, result) {
            token = result.result.token;

            if (token) {
                var email = '<EMAIL>';
                var pw = '<PASSWORD>';
                var tokened_pw = md5(token + md5(pw).toLowerCase()).toLowerCase();
                
                request('http://api.sendspace.com/rest/?method=auth.login&token='+token+'&user_name='+email+'&tokened_password='+tokened_pw, function(error, response, body) {
                    parser.parseString(body, function(err, result) {
                        handler(result.result.session_key);
                    });
                });
            }
        });
    });
}

var uploadFileToSendSpace = function(session_key) {
    request('http://api.sendspace.com/rest/?method=upload.getinfo&session_key='+session_key, function(error, response, body) {
        var parser = new xml2js.Parser({explicitArray: false});
        parser.parseString(body, function(err, result) {
            var uploadObj = result.result.upload.$;
            
            var options = {
                url: 'https://content.dropboxapi.com/2/files/download',
                headers: {
                    'Dropbox-API-Arg': '{"path": "/testi1.txt"}',
                    'Authorization': 'Bearer <KEY>'
                }
            };
            
            
            var formData = {
                MAX_FILE_SIZE: uploadObj.max_file_size,
                UPLOAD_IDENTIFIER: uploadObj.upload_identifier,
                extra_info: uploadObj.extra_info,
                userfile: request.get(getDropBoxDownloadParams("/testi1.txt", db_accesstoken))
            };

            
            request.post({url:uploadObj.url, formData: formData}, function(error, response, body) {
                if(error) {
                    console.log(error);
                }

                var respJson = sendSpaceUploadResponseToJson(body);
                if(respJson.upload_status === 'ok') {
                    // rename the file since downloadStream gives 'donwload' to file name
                    request('http://api.sendspace.com/rest/?method=files.setinfo&session_key='+session_key+'&file_id='+respJson.file_id+'&name=kissakala.txt', function(error, response, body) {
                        console.log(body);
                    });
                }
            });
            
        });
    });
}

module.exports = function() {
    //listDropBoxFiles(dbx);
    //getSendspaceSessionkey(uploadFileToSendSpace);
    
    return "Terve mualima!";
}
{'fraction_non_alphanumeric': 0.13175914994096813, 'fraction_numerical': 0.004250295159386069, 'mean_word_length': 1.9587844917918267, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7171945', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2359, 'n_words': 413}