-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgur.py
61 lines (45 loc) · 1.54 KB
/
imgur.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
# -*- coding: utf-8 -*-
#
# Upload images to imgur for use in weechat
#
# Usage: /imgur PATH
#
# History:
#
# Version 1.0.0: initial release
#
import os
import os.path
import requests
import weechat
URL = "https://api.imgur.com/3/image"
CLIENT_ID = "client_id"
def get_client_id():
imgur_token = weechat.config_get_plugin(CLIENT_ID)
if imgur_token.startswith("${sec.data"):
imgur_token = weechat.string_eval_expression(imgur_token, {}, {}, {})
return imgur_token
def imgur(data, buf, args):
filepath = os.path.expanduser(args)
contents = open(filepath).read()
files = {"image": contents}
client_id = get_client_id()
if len(client_id) == 0 or " " in client_id:
raise Exception("Need to set imgur client id")
headers = {"Authorization": "Client-ID %s" % client_id}
response = requests.post(URL, files=files, headers=headers)
data = response.json()
image_url = data["data"]["link"]
weechat.buffer_set(buf, "input", image_url)
weechat.buffer_set(buf, "input_pos", str(len(image_url)))
return weechat.WEECHAT_RC_OK
def main():
if not weechat.register("imgur", "Keith Smiley", "1.0.0", "MIT",
"Upload an image to imgur", "", ""):
return weechat.WEECHAT_RC_ERROR
if not weechat.config_get_plugin(CLIENT_ID):
weechat.config_set_plugin(CLIENT_ID, "Set imgur client ID")
weechat.hook_command("imgur", "Pass the current buffer to urlview", "",
"", "filename", "imgur", "")
if __name__ == "__main__":
main()