forked from condemil/gist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitio.py
43 lines (34 loc) · 1.23 KB
/
gitio.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
from urllib.error import URLError, HTTPError
from urllib.parse import urlencode
from urllib.request import urlopen
try:
import sublime
import sublime_plugin
except ImportError:
from test.stubs import sublime
from test.stubs import sublime_plugin
gitio_url = 'https://git.io/create'
caption = 'GitHub URL:'
def gitio(req_url):
data = urlencode({'url': req_url}).encode()
try:
response = urlopen(gitio_url, data)
body = response.read().decode()
if response.status == 200:
return None, 'https://git.io/{}'.format(body)
return body, None
except HTTPError as e:
return e.read().decode(), None
except URLError:
return 'Gist: Error contacting git.io', None
class GitioCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel(caption, '', self.on_done, None, None)
def on_done(self, req_url):
err, short_url = gitio(req_url)
if err:
sublime.error_message(err)
self.view.window().show_input_panel(caption, req_url, self.on_done, None, None)
else:
sublime.set_clipboard(short_url)
sublime.status_message('Gist: Copied to Clipboard! ' + short_url)