summaryrefslogtreecommitdiffstats
path: root/fb
blob: 1fd5c34f185ad8146790c6ed9c71d84a32e33b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
#----------------------------------------------------
# Licensed under WTFPL v2
#   (see COPYING for full license text)
#
#----------------------------------------------------
# only works if useragent contains libcurl
#----------------------------------------------------

'''[cat |] %prog [file1 file2 ...]'''
__version__ = '0.4.0'
__desc__ = '''
Upload file to paste.xinu.at and copy URL to clipboard
or nopaste stdin
'''

from optparse import OptionParser
from subprocess import Popen, PIPE
import os
import sys
import netrc

def password():
    ''' netrc: machine paste.xinu.at password PASSWORD'''
    try:
        auth = netrc.netrc().authenticators('paste.xinu.at')
    except:
        return None
    if not auth:
        return None
    return auth[2]

def do_upload(file):
    pw = password()
    curl_args = []
    if pw:
        curl_args.append('-F')
        curl_args.append('password='+pw)
    url = Popen(merge([['curl', '-#', '-L', '-F', 'userfile=@'+file,
                      ], curl_args, ['http://paste.xinu.at/file/do_upload']]),
                stdout=PIPE).communicate()[0]
    print url
    Popen('echo -n "%s" | nohup &>/dev/null xclip' % url, shell=True)

def delete(id):
    pw = password()
    curl_args = []
    if not pw:
        print 'Please set up .netrc correctly'
        sys.exit(1)
    else:
        curl_args.append('-F')
        curl_args.append('password='+pw)
    Popen(merge([['curl', '-#', '-L'], curl_args,
                 ['http://paste.xinu.at/file/delete/'+id]]
               ),).communicate()[0]

def merge(seq):
    merged = []
    for s in seq:
        for x in s:
            merged.append(x)
    return merged

def read_stdin():
    if os.isatty(sys.stdin.fileno()):
        print '^C to exit, ^D to send'
    try:
        content = sys.stdin.read()
    except KeyboardInterrupt:
        sys.exit()
    if not content:
        sys.exit()
    tmpfile = Popen('mktemp', stdout=PIPE).communicate()[0]
    tmpfile = tmpfile.replace('\n', '')
    f = open(tmpfile, 'w')
    f.write(content)
    f.close()
    return tmpfile

def main():
    p = OptionParser(version=__version__,
                     usage=__doc__,
                     description=__desc__+password.__doc__)
    p.add_option('-d', '--delete', action='append', dest='delete',
                 help='delete file by ID', metavar='<ID>', default=None)
    options, args = p.parse_args()

    if options.delete:
        for id in options.delete:
            delete(id)
        sys.exit()

    tmpfiles = []
    if args:
        for arg in args:
            if arg.find('http://') != -1:
                tmpdir = Popen(['mktemp', '-d'], stdout=PIPE).communicate()[0]
                tmpdir = tmpdir.replace('\n', '')
                os.chdir(tmpdir)
                tmpfiles.append(tmpdir)
                Popen(['wget', '-q', arg]).communicate()[0]
                for file in os.listdir(tmpdir):
                    do_upload(file)
            else:
                do_upload(arg)
    else:
        tmpfile = read_stdin()
        tmpfiles.append(tmpfile)
        do_upload(tmpfile)

    os.chdir('/tmp')
    for path in tmpfiles:
        if os.path.isfile(path):
            os.remove(path)
        else:
            for file in os.listdir(path):
                os.remove(path+'/'+file)
            # FIXME: causes getcwd error
            #os.rmdir(path)
            # for some reason this works better
            Popen(['rmdir', path])

if __name__ == '__main__':
    main()