#!/usr/bin/env python
# -*- coding: utf-8 -*-

# A simple hack that tries to upload a text file to paste.chebab.com

import sys, os

PAGE = "http://paste.chebab.com"

def paste( text ):
    import urllib
    # Upload
    post_data = "text=%s" % urllib.quote( text )
    post_data += "&add=Add"

    urlobj = urllib.urlopen( PAGE, data = post_data )
    
    # Look for 'Added <a href="?' (a success)
    for line in urlobj.readlines():
        if line.find( 'Added <a href="?' ):
            return True
    return False


# Main
if __name__ == '__main__':
    if len( sys.argv ) < 2:
        print "Submits texts files as paste to %s" % PAGE
        print
        print "Usage: chepaste TEXTFILE"
        print

    else:
        if not os.path.isfile( sys.argv[1] ):
            print "No such file..."
            sys.exit( 1 )

        else:
            text = ''.join( open( sys.argv[1], 'r' ).readlines() )

            if paste( text ):
                print "Uploaded :)"

            else:
                print "Failed to upload the text :("


