#! /usr/bin/env python
# -*- coding: utf8 -*-

#Upload a code  to http://paste.ideaslabs.com
#@author Edgar GarcĂ­a <http://hunk.com.mx>
#@author David Valdez <http://gnuget.org>

#Copyright (c)  2008 David Valdez (http://gnuget.org) y Edgar Garcia (http://hunk.com.mx)
#Permission is hereby granted, free of charge, to any
#person obtaining a copy of this software and associated
#documentation files (the "Software"), to deal in the
#Software without restriction, including without limitation
#the rights to use, copy, modify, merge, publish,
#distribute, sublicense, and/or sell copies of the
#Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice
#shall be included in all copies or substantial portions of
#the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
#KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
#PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
#OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




import getopt
import urllib
import urllib2
import os
import sys
import string

class paste:
    """ This class permit upload a file to http://paste.ideaslabs.com"""
    def __init__(self):
        pass

    #help function
    def usage(self):
        print "Options:"
        print "paste -f --file file.ext"
        print "paste --file file.ext"
        print "paste --time -t  (hour,day,week,year)"
    
    #this function is executed when all options was parse
    def upload(self):        
        if self.file_name == "":
            stdin = sys.stdin.read()
            if stdin != "":
                self.file_name = "output"
                self.code = stdin
            else:
                print "you need select some file"
                exit(2)
        
        
        
        values = {
            'name':self.file_name,
            'language':self.language,
            'time':self.time,
            'code':self.code,
            'description':self.description,
            'status':self.status,
            'submit':'submit'
        }
        params = urllib.urlencode(values)
        
        #headers
        headers =   {
                        'Referer': 'http://paste',
                        'Content-type':'application/x-www-form-urlencoded',
                        'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050813 Epiphany/1.7.6',
                        'Accept-Language' : 'en',
                        'Accept-Charse' :   'utf-8'
                    }
        
        request = urllib2.Request("http://paste.ideaslabs.com/code/file",params,headers);
        print urllib2.urlopen(request).read()
    
    
    #set a file
    #to-do  check if the fil is a text file (don't can be  binary)
    def set_file(self,file_name):
        if os.path.exists(file_name):
            f = open(file_name)
            self.code = f.read()
            self.file_name = file_name
        else:
            print "the file called %s don't exists" % file_name
            sys.exit(2)
        
    #set a language
    #todo add more languages i just add my favorites :p 
    def set_language(self,language):
        language = string.lower(language)
        
        if language == "python":
            self.language = 60
        elif language == "php":
            self.language = 57
        elif language == "javascript":
            self.language = 43
        elif language == "mysql":
            self.language = 50
        elif language == "css":
            self.language = 22
        else:
            print "I still don't know that language sorry"
            sys.exit(2);        
    
    #set a time
    def set_time(self,time):
        time = string.lower(time)
        if time == "hour":
            self.time = 1
        elif time == "day":
            self.time = 2
        elif time == "week":
            self.time = 3
        elif time == "month":
            self.time = 4
    
    #main
    def main(self):   
        
        #parse options
        try:
            opts,args = getopt.getopt(sys.argv[1:],"hf:l:t:",["help","file=","language=","time="])
        except getopt.error, msg:
            print msg
            self.usage()
            sys.exit(0)
            
        #procesing options
        self.time = 1
        self.language =  1
        self.file_name = ""
        self.code = ""
        self.status = "1"
        self.description = "description"
        
        for o, a in opts:
            if o in ("-h","--help"):
                #self.usage()
                sys.exit(0)
            if o in ("-f","--file"):
                self.set_file(a)
            if o in ("-l","--language"):
                self.set_language(a)
            if o in ("-t","--time"):
                self.set_time(a)
                
        self.upload()

if __name__ == "__main__":
    paste = paste()
    paste.main()