<commit_before>#!/usr/bin/env python
# Copyright (c) 2011, <NAME> <<EMAIL>>
# All rights reserved.

import hashlib
import os
import sys
import ConfigParser as configparser
import requests
from os.path import join
from datetime import datetime
from dateutil.relativedelta import *
from optparse import OptionParser
from poster.encode import multipart_encode
from datetime import datetime, timedelta

config = configparser.RawConfigParser()
if not config.read([os.path.expanduser('~/.ppupload.conf') or 'ppupload.conf', '/etc/ppupload.conf']):
    sys.exit("Couldn't read configuration file")

usage = 'usage: %prog -h'
parser = OptionParser(usage, version="%prog 0.1")
parser.add_option("--file", "-f", help='Specify file to upload')
parser.add_option("--onetime", "-1", help='One time download(optional)', default=False, action='store_true')
parser.add_option("--lifetime", "-l", help='Lifetime(optional): 1h, 1d, 1w, 1m (hour/day/week/month). Default lifetime is unlimited')
parser.add_option("--password", "-p", help='Download password(optional)')

(options, args) = parser.parse_args()

if not options.file:
    parser.error('Specify --file')
elif not os.path.isfile(options.file):
    sys.exit('File does not exist')
else:
    filename = os.path.basename(options.file)

if options.onetime:
    one_time_download = 1
else:
    one_time_download = 0

if options.password:
    password = hashlib.new('<PASSWORD>')
    password.update(options.password)
    download_password = password.hexdigest()
else:
    download_password = ''

# lifetime (should be moved to the upload script on the server)
if options.lifetime:
    if options.lifetime == '1h':
        lifetime = datetime.now() + relativedelta(hours=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1d':
        lifetime = datetime.now() + relativedelta(days=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1w':
        lifetime = datetime.now() + relativedelta(weeks=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1m':
        lifetime = datetime.now() + relativedelta(weeks=4)
        expire = lifetime.strftime('%Y%m%d%H%M')
    else:
        parser.error('Unknown lifetime')
else:
    expire = ''

class ProgressMeter(object):
    def __init__(self, microseconds=1000000):
        self.started_at = None
        self.timeout = microseconds
        self.previous_length = 0

    def __call__(self, param, current, total):
        if self.started_at is None:
            self.started_at = datetime.now()
        else:
            if datetime.now() - self.started_at > timedelta(microseconds=self.timeout):
                line = '{filename} {percent:.1%}'.format(
                    filename = param.name,
                    percent = current / float(total)
                )

                sys.stdout.write('\b' * self.previous_length)
                sys.stdout.write(line)
                sys.stdout.flush()

                self.previous_length = len(line)

                self.started_at = datetime.now()


half_a_second = 500000

postdata = {
    'uploaded': open('/Users/jhaals/Desktop/brooks_bottom.jpg', 'rb'),
    'username': config.get('settings', 'username'),
    'password': config.get('settings', 'password'),
    'filename': filename,
    'download_password': <PASSWORD>_password,
    'one_time_download': one_time_download,
}
datagen, headers = multipart_encode(postdata,
    cb = ProgressMeter(half_a_second)
)

r = requests.post(config.get('settings', 'upload_url'), data=datagen, headers=headers)
print r.content
# parse results from server....

#    download_url = config.get('remote', 'url')+download_hash
#    if sys.platform == 'darwin':
#        os.system('echo %s | pbcopy' % download_url)
#    print download_url<commit_msg>ability to define files to upload. \o/<commit_after>#!/usr/bin/env python
# Copyright (c) 2011, <NAME> <<EMAIL>>
# All rights reserved.

import hashlib
import os
import sys
import ConfigParser as configparser
import requests
from os.path import join
from datetime import datetime
from dateutil.relativedelta import *
from optparse import OptionParser
from poster.encode import multipart_encode
from datetime import datetime, timedelta

config = configparser.RawConfigParser()
if not config.read([os.path.expanduser('~/.ppupload.conf') or 'ppupload.conf', '/etc/ppupload.conf']):
    sys.exit("Couldn't read configuration file")

usage = 'usage: %prog -h'
parser = OptionParser(usage, version="%prog 0.1")
parser.add_option("--file", "-f", help='Specify file to upload')
parser.add_option("--onetime", "-1", help='One time download(optional)', default=False, action='store_true')
parser.add_option("--lifetime", "-l", help='Lifetime(optional): 1h, 1d, 1w, 1m (hour/day/week/month). Default lifetime is unlimited')
parser.add_option("--password", "-p", help='Download password(optional)')

(options, args) = parser.parse_args()

if not options.file:
    parser.error('Specify --file')
elif not os.path.isfile(options.file):
    sys.exit('File does not exist')
else:
    try:
        upload_file = open(options.file, 'rb')
    except IOError:
        sys.exit('Could not open %s') % options.file
    filename = os.path.basename(options.file)

if options.onetime:
    one_time_download = 1
else:
    one_time_download = 0

if options.password:
    password = hashlib.new('<PASSWORD>')
    password.update(options.password)
    download_password = password.hexdigest()
else:
    download_password = ''

# lifetime (should be moved to the upload script on the server)
if options.lifetime:
    if options.lifetime == '1h':
        lifetime = datetime.now() + relativedelta(hours=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1d':
        lifetime = datetime.now() + relativedelta(days=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1w':
        lifetime = datetime.now() + relativedelta(weeks=1)
        expire = lifetime.strftime('%Y%m%d%H%M')
    elif options.lifetime == '1m':
        lifetime = datetime.now() + relativedelta(weeks=4)
        expire = lifetime.strftime('%Y%m%d%H%M')
    else:
        parser.error('Unknown lifetime')
else:
    expire = ''

class ProgressMeter(object):
    def __init__(self, microseconds=1000000):
        self.started_at = None
        self.timeout = microseconds
        self.previous_length = 0

    def __call__(self, param, current, total):
        if self.started_at is None:
            self.started_at = datetime.now()
        else:
            if datetime.now() - self.started_at > timedelta(microseconds=self.timeout):
                line = '{filename} {percent:.1%}'.format(
                    filename = param.name,
                    percent = current / float(total)
                )

                sys.stdout.write('\b' * self.previous_length)
                sys.stdout.write(line)
                sys.stdout.flush()

                self.previous_length = len(line)

                self.started_at = datetime.now()


half_a_second = 500000

postdata = {
    'uploaded': upload_file,
    'username': config.get('settings', 'username'),
    'password': config.get('settings', 'password'),
    'filename': filename,
    'download_password': <PASSWORD>_password,
    'one_time_download': one_time_download,
}
datagen, headers = multipart_encode(postdata,
    cb = ProgressMeter(half_a_second)
)

r = requests.post(config.get('settings', 'upload_url'), data=datagen, headers=headers)
print r.content
# parse results from server....

#    download_url = config.get('remote', 'url')+download_hash
#    if sys.platform == 'darwin':
#        os.system('echo %s | pbcopy' % download_url)
#    print download_url
{'fraction_non_alphanumeric': 0.13898217514854042, 'fraction_numerical': 0.009299922500645827, 'mean_word_length': 3.35, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3979689', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2382, 'n_words': 551}