-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (111 loc) · 4.41 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
import sys, os
parent_folder_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(parent_folder_path)
sys.path.append(os.path.join(parent_folder_path, 'lib'))
sys.path.append(os.path.join(parent_folder_path, 'plugin'))
from flox import Flox
import urllib.request
import json
import webbrowser
####
# Copy to clipboard function (put) compatible with x64 from https://forums.autodesk.com/t5/maya-programming/ctypes-bug-cannot-copy-data-to-clipboard-via-python/td-p/9195866
import ctypes
from ctypes import wintypes
CF_UNICODETEXT = 13
user32 = ctypes.WinDLL('user32')
kernel32 = ctypes.WinDLL('kernel32')
OpenClipboard = user32.OpenClipboard
OpenClipboard.argtypes = wintypes.HWND,
OpenClipboard.restype = wintypes.BOOL
CloseClipboard = user32.CloseClipboard
CloseClipboard.restype = wintypes.BOOL
EmptyClipboard = user32.EmptyClipboard
EmptyClipboard.restype = wintypes.BOOL
GetClipboardData = user32.GetClipboardData
GetClipboardData.argtypes = wintypes.UINT,
GetClipboardData.restype = wintypes.HANDLE
SetClipboardData = user32.SetClipboardData
SetClipboardData.argtypes = (wintypes.UINT, wintypes.HANDLE)
SetClipboardData.restype = wintypes.HANDLE
GlobalLock = kernel32.GlobalLock
GlobalLock.argtypes = wintypes.HGLOBAL,
GlobalLock.restype = wintypes.LPVOID
GlobalUnlock = kernel32.GlobalUnlock
GlobalUnlock.argtypes = wintypes.HGLOBAL,
GlobalUnlock.restype = wintypes.BOOL
GlobalAlloc = kernel32.GlobalAlloc
GlobalAlloc.argtypes = (wintypes.UINT, ctypes.c_size_t)
GlobalAlloc.restype = wintypes.HGLOBAL
GlobalSize = kernel32.GlobalSize
GlobalSize.argtypes = wintypes.HGLOBAL,
GlobalSize.restype = ctypes.c_size_t
GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040
unicode = type(u'')
def put(s):
if not isinstance(s, unicode):
s = s.decode('mbcs')
data = s.encode('utf-16le')
OpenClipboard(None)
EmptyClipboard()
handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len(data) + 2)
pcontents = GlobalLock(handle)
ctypes.memmove(pcontents, data, len(data))
GlobalUnlock(handle)
SetClipboardData(CF_UNICODETEXT, handle)
CloseClipboard()
######
class ObsidianPlugins(Flox):
def paste(self, name, pluginId, repo):
self.show_msg("Copied to clipboard", "Help")
put(
"_" + name + "_ plugin (<obsidian://show-plugin?id=" + pluginId + "> | " + "<https://github.com/" + repo + ">)")
def open_in_browser(self, url):
webbrowser.open(url)
def query(self, query):
# using my own api here, just because(the raw json from gh would also work)
url = "https://api.obsidian.joethei.xyz/plugins"
data = urllib.request.urlopen(url).read().decode()
obj = json.loads(data)
var = filter(lambda x: self.args.lower() in x['name'].lower()
or self.args.lower() in x['author'].lower()
or self.args.lower() in x['description'].lower()
or self.args.lower() in x['id'].lower()
, obj)
for plugin in var:
self.add_item(title=plugin['name'],
subtitle=plugin['description'],
method=self.paste,
parameters=[plugin['name'], plugin['id'], plugin['repo']],
context=[plugin])
def context_menu(self, data):
plugin = data[0]
self.add_item(
title=plugin['name'],
subtitle=plugin['description'],
method=self.paste,
parameters=[plugin['name'], plugin['id'], plugin['repo']],
context=[plugin]
)
self.add_item(
title="GitHub Repository",
subtitle=plugin['repo'],
icon="github.png",
method=self.open_in_browser,
parameters=["https://github.com/" + plugin['repo']]
)
self.add_item(
title="Plugin ID",
subtitle=plugin['id'],
)
url = "https://api.obsidian.joethei.xyz/plugin/" + plugin['id'] + "/downloads"
data = urllib.request.urlopen(url).read().decode()
obj = json.loads(data)
if obj:
self.add_item(
title="Total Downloads",
subtitle=str(obj["downloads"])
)
if __name__ == "__main__":
ObsidianPlugins()