Author Topic: [python] A simple pastebin uploader  (Read 1255 times)

0 Members and 1 Guest are viewing this topic.

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
[python] A simple pastebin uploader
« on: July 19, 2015, 12:08:18 am »
I know there are plenty. I know they can do much. But I like to roll my own minimalistic solutions.

It can read from files, stdin (until an EOL is issued) and soon maybe the clipboard.
Pretty easy to use, you just need a pastebin API key (more info here), a terminal on some
Unix and something to paste.
EDIT: Of course you'll need requests, too.
To install,
Code: [Select]
$ sudo pip install requestsEDIT2:
To use the optional clipboard dependency, run
Code: [Select]
$ sudo pip install clipboard(I might write my own tho, if someone wants).

(Or just download and install from pypi).

EDIT: cleaned and updated the code a bit.
EDIT3: Incorporated kenjoe41's suggestions. Thank you.
Here's the code:
Code: (Python) [Select]
import argparse
import sys

import requests

HAS_CLIPBOARD = True
try:
    import clipboard
except ImportError:
    HAS_CLIPBOARD = False


def post(string, form, expiry):
    post_dict = {'api_dev_key': '51652104665c18834023dae5d21d2a56',
                 'api_option': 'paste',
                 'api_paste_code': string
                 }
    if form:
        post_dict['api_paste_format'] = form
    if expiry:
        post_dict['api_paste_expiry_date'] = expiry
    return requests.post('http://www.pastebin.com/api/api_post.php', post_dict)


def get_input(source):
    if source != 'stdin':
        with open(source, 'r') as f:
            return f.read()
    return sys.stdin.read()


if __name__ == '__main__':
    ARG_PARSER = argparse.ArgumentParser(
        description='Post something to pastebin.com')
    ARG_PARSER.add_argument('-s', default='stdin',
                            help='The source of the paste (default: stdin).')
    ARG_PARSER.add_argument('-f', help='The format (default: none.')
    ARG_PARSER.add_argument('-e', help='The expiry time (default:none).')
    ARG_PARSER.add_argument('-c', help='Use clipboard as source',
                            action='store_true')
    ARGS = ARG_PARSER.parse_args()
    if not ARGS.c:
        input_string = get_input(ARGS.s)
    elif HAS_CLIPBOARD:
        input_string = clipboard.paste()
    else:
        print 'You don\'t have https://pypi.python.org/pypi/clipboard!'
        sys.exit()
    print '\nuploading...'
    print post(input_string, ARGS.f, ARGS.e).content
« Last Edit: August 12, 2015, 04:33:24 pm by TheWormKill »
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [python] A simple pastebin uploader
« Reply #1 on: August 12, 2015, 01:03:28 am »
Lets go through your code slowly and see what is wrong. Will attach further reading where possible to help you understand where i am coming from.

First, at all times when writing code to be executed as a script, use sys.exit() instead of exit(). The exit() function is meant to be used in the python's interactive shell: http://stackoverflow.com/a/6501134

Second, There are some garbage collection issues in here. Pay attention to line 27:
Code: (python) [Select]
return open(source, 'r').read()The opened file mostly becomes garbage after this call is executed and never collected till the program ends. This isn't good when it comes to big projects and in software engineering in general. It will become detrimental for programs that run for too long. It eats alot of memory. Later programming ventures might end into memory leaks, bugs and vulnerabilities so get in the habit of cleaning up.http://stackoverflow.com/a/7409814 and http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/ Modern python programmers usually use the with statement that automatically closes the file:
Code: (python) [Select]
with open("filename.txt", rb) as f:
                                 content=f.read()
Read up:http://effbot.org/zone/python-with-statement.htm
You could also write/define an atexit function and call cleanup functions for that file but that might be overkill though good to learn.

We could use an option to upload an already written file so a file argument couldn't be that bad.

It is a nice little script to practice writting python version portable code. Look into the future's print_function, etc.

Here is one i wrote for ghostbin.com, read through for comparison and stuff: https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py

If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: [python] A simple pastebin uploader
« Reply #2 on: August 12, 2015, 04:28:22 pm »
Lets go through your code slowly and see what is wrong. Will attach further reading where possible to help you understand where i am coming from.

First, at all times when writing code to be executed as a script, use sys.exit() instead of exit(). The exit() function is meant to be used in the python's interactive shell: http://stackoverflow.com/a/6501134

Second, There are some garbage collection issues in here. Pay attention to line 27:
Code: (python) [Select]
return open(source, 'r').read()The opened file mostly becomes garbage after this call is executed and never collected till the program ends. This isn't good when it comes to big projects and in software engineering in general. It will become detrimental for programs that run for too long. It eats alot of memory. Later programming ventures might end into memory leaks, bugs and vulnerabilities so get in the habit of cleaning up.http://stackoverflow.com/a/7409814 and http://mortoray.com/2011/03/30/why-garbage-collection-is-not-necessary-and-actually-harmful/ Modern python programmers usually use the with statement that automatically closes the file:
Code: (python) [Select]
with open("filename.txt", rb) as f:
                                 content=f.read()
Read up:http://effbot.org/zone/python-with-statement.htm
You could also write/define an atexit function and call cleanup functions for that file but that might be overkill though good to learn.

We could use an option to upload an already written file so a file argument couldn't be that bad.

It is a nice little script to practice writting python version portable code. Look into the future's print_function, etc.

Here is one i wrote for ghostbin.com, read through for comparison and stuff: https://github.com/kenjoe41/ghostpaste/blob/master/ghostpaste/ghostpaste.py

Thank you for your reply! While I try to write clean code, I actually thought that GC will delete a file handle that isn't assigned to a line. And I obv didn't know about the exit() issue. Thanks and +1.
« Last Edit: August 12, 2015, 04:31:26 pm by TheWormKill »
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker