summarylogtreecommitdiffstats
path: root/hastebin
blob: af4343a0bb315c36e6c23b274da2360df0b94508 (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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Project: hastebin
# Author: kevr <kevin.morris@codestruct.net>
# Description: A tool which uploads data from stdin to hastebin.com
# Copyright (C) 2017 Kevin Morris
import sys
import requests
import json
from select import select

## Voila hardcoded settings~
url = "https://hastebin.com"
timeout = 5 # Change this to any http request timeout you desire

## Immediately timed out select poll
def has_data(fd):
    return select([fd], [], [], 0.0) == ([fd], [], [])

def quit(code, msg):
    print(msg)
    return code

## Sorry for the C-style functions, I prefer them
def main():
    if not has_data(sys.stdin):
        return quit(1, "hastebin: no data given via stdin")

    try:
        stdin = sys.stdin.read()
    except UnicodeDecodeError:
        return quit(2, "hastebin: an error occured reading stdin")
        
    response = requests.post("%s/documents" % url, headers={
      "Accept": "application/json"}, data=stdin)
    if response.status_code != requests.codes.ok:
      return quit(3, "hastebin: error submitting POST data to hastebin")

    content = response.content.decode("UTF-8").rstrip()
    data = json.loads(content)

    print("%s/%s" % (url, data["key"]))

    return 0

# main execution
if __name__ == "__main__":
    e = main()
    exit(e)