-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastie.py
executable file
·112 lines (88 loc) · 2.95 KB
/
pastie.py
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
111
112
#!/usr/bin/env python
# -*- coding: utf-8; -*-
import requests
# Declaring available languages
languages = {
'as' : 2,
'bash' : 13,
'c#' : 20,
'c' : 7,
'c++' : 7,
'css' : 8,
'diff' : 5,
'html' : 11,
'xml' : 11,
'java' : 9,
'js' : 10,
'perl' : 18,
'php' : 15,
'plain' : 6,
'py' : 16,
'rb': 3,
'sql' : 14,
'yaml' : 19,
}
def pastie(content, lang='plain', private=0):
# If lang is in languages, choose it else choose plain
parser_id = languages[lang] if lang in languages else languages['plain']
# Create a new session
s = requests.session()
# Creating post dict
post_dict = {}
post_dict['utf8'] = '✓'
post_dict['paste[authorization]'] = 'burger'
post_dict['paste[access_key]'] = ''
post_dict['paste[parser_id]'] = parser_id
post_dict['paste[body]'] = content
post_dict['paste[restricted]'] = private
rep = s.post('http://pastie.org/pastes', post_dict)
if 'paste-id' in rep.headers:
return rep.headers['paste-id']
else:
return False
if __name__ == '__main__':
import sys, argparse
# =================================================================================
# Setting argparse
# =================================================================================
parser = argparse.ArgumentParser()
# Private param
parser.add_argument("-p", "--private", help="Make the paste private", action="store_true")
# Language lexer
parser.add_argument("-l", "--lang", help="Set language lexer for content (default: plain)")
# Input file
parser.add_argument("file", nargs='?', help="Input file, if not specified, reading from stdin")
# Show lang list
parser.add_argument("--langlist", help="List available language lexers", action="store_true")
# =================================================================================
# Reading params
# =================================================================================
args = parser.parse_args()
# Lang list
if args.langlist:
print '================================================================'
print 'Available language lexers'
print '================================================================'
print '\n'.join(languages)
sys.exit(0)
# Input content
if args.file:
try:
f = open(args.file, 'r')
content = f.read()
except IOError:
print 'Error while reading input file'
sys.exit(1)
except Exception, e:
print str(e)
sys.exit(1)
else:
try:
content = sys.stdin.read().decode('utf8')
except KeyboardInterrupt:
sys.exit(1)
# Private param
private = 1 if args.private else 0
# Language lexser
lang = args.lang if args.lang else 'plain'
print 'http://pastie.org/' + pastie(content, lang, private)