summaryrefslogtreecommitdiffstats
path: root/fb-client/fb-0.5.3
blob: 98a5256839383b0d1c879c5302d55767c96e9b4d (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
#!/usr/bin/env python
#----------------------------------------------------
# Author:       Florian "Bluewind" Pritz <flo@xssn.at>
#
# Licensed under WTFPL v2
#   (see COPYING for full license text)
#
#----------------------------------------------------
# only works if useragent contains libcurl
# Dependencies: python, curl
# Optional: xclip
#----------------------------------------------------

'''[cat |] %prog [options] [file1 file2 ...]'''
__version__ = '0.5.3'
__desc__ = '''
Upload/nopaste file/stdin to paste.xinu.at and copy URL to clipboard.
~/.netrc: machine paste.xinu.at password PASSWORD
'''

from optparse import OptionParser
from subprocess import Popen, PIPE
from urllib2 import urlopen
import os
import sys
import netrc
import re

def do_upload(file, extension=None):
    curl_args = []
    if extension:
        curl_args.append('-F')
        curl_args.append('extension=%s' % extension)

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

def get(id):
    print urlopen('http://paste.xinu.at/%s' % id).read()

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

def read_stdin(tmpfile):
    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 = tmpfile.replace('\n', '')
    f = open(tmpfile, 'w')
    f.write(content)
    f.close()
    return tmpfile

def main():
    p = OptionParser(version=__version__,
                     usage=__doc__,
                     description=__desc__)
    p.add_option('-d', '--delete', action='store_true', dest='delete',
                 help='delete IDs', default=False)
    p.add_option('-e', '--extension', action='store', dest='extension',
                 help='extension for default highlighting (e.g. "diff")', default=None)
    p.add_option('-g', '--get', action='store_true', dest='get',
                 help='Download File IDs and output to stdout (use with care!)', default=False)
    options, args = p.parse_args()

    tmpfiles = []
    tmpdir = Popen(['mktemp', '-d'], stdout=PIPE).communicate()[0]
    tmpdir = tmpdir.replace('\n', '')
    if args:
        for arg in args:
            if options.delete:
                Popen(['curl', '-#', '-n', '-L', 'http://paste.xinu.at/file/delete/%s' % arg]).communicate()[0]
                continue

            if options.get:
                get(arg)
                continue

            if re.match('[a-z]+://.+', arg):
                os.chdir(tmpdir)
                tmpfiles.append(tmpdir)
                Popen(['curl', '-#', '-O', arg]).communicate()[0]
                for file in os.listdir(tmpdir):
                    do_upload(file, extension=options.extension)
            else:
                do_upload(arg, extension=options.extension)
    else:
        tmpfile = read_stdin(tmpdir+"/stdin")
        tmpfiles.append(tmpfile)
        do_upload(tmpfile, extension=options.extension)

    os.chdir('/tmp')
    for path in tmpfiles:
        Popen(['rm', '-rf', path])
    Popen(['rm', '-rf', tmpdir])

if __name__ == '__main__':
    main()