This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (131 loc) · 4 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const got = require('got')
const preview_html = '<html><body><img style="max-width: 100%;max-height: 100%;" src="%imageurl%"></body></html>'
module.exports = (pluginContext) => {
const logger = pluginContext.logger
const prefObj = pluginContext.preferences
const app = pluginContext.app
const pref = prefObj.get()
const shell = pluginContext.shell
const clipboard = pluginContext.clipboard
const api_key = pref.api_key
function search (query, res) {
const query_trim = query.trim()
if (query_trim.length === 0) {
return res.add({
id: 'https://pximg.xyz/browse/',
payload: 'open',
title: 'Browse',
desc: 'View all images on PXIMG."'
})
}
res.add({
id: 'https://pximg.xyz/browse/',
payload: 'open',
title: 'Browse',
desc: 'View all images on PXIMG."'
})
if (api_key === '') {
return res.add({
id: 'no_api_key',
payload: 'prefs',
title: 'No API Key Found',
desc: '<span style="color:#f0ad4e;">Please add your API key in Preferences.</span>',
icon: '#fa fa-exclamation-triangle'
})
}
if (query_trim.toLowerCase() == 'winnings') {
let subs = 'https://pximg.xyz/api/v2/me/giveaway/check?api_key=' + api_key
got(subs).then(response => {
let data = JSON.parse(response.body)
if (data.status == true) {
for (var k in data.Message) {
if (!data.Message.hasOwnProperty(k)) { continue }
var o = data.Message[k]
var d = {
id: o.licence_code,
payload: 'copy_to_clipboard',
title: '<b>' + o.title + '</b>',
desc: '<span style="color:#005a9c;">' + o.licence_code + ' - ' +
o.url + '</span>',
icon: '#fa fa-gift'
}
res.add(d)
}
}
})
return
} else {
res.add({
id: '/px winnings',
payload: 'set_query',
title: 'Check Giveaway Winnings',
desc: 'Check your licence codes for giveaways that you\'ve won."'
})
}
if (isNaN(query_trim)) {
return res.add({
'id': query_trim,
'payload': 'none',
'title': 'Invalid Image ID: <b>' + query_trim + '</b>',
'desc': '<span style="color:#a94442;">The image id should contain numbers only.</span>',
'icon': '#fa fa-exclamation-triangle'
})
}
let subs = 'https://pximg.xyz/api/v2/images/id/' + query_trim + '?api_key=' +
api_key
got(subs).then(response => {
let data = JSON.parse(response.body)
if (data.Status == true) {
data = {
id: data.Message.url,
payload: 'open',
title: 'Open Image <b>' + data.Message.public_hash + '</b>',
desc: '<span style="color:#005a9c;">( ' + data.Message.dimensions +
' ) [ ' + data.Message.uploader + ' ] [ ' + data.Message.type +
' ] [ ' + data.Message.uploaded_date + ' ]</span>',
icon: '#fa fa-picture-o',
preview: pref.show_preview
}
} else {
if (data.Status == false) {
data = {
'id': query_trim,
'payload': 'none',
'title': 'Image <b>' + query_trim + '</b>',
'desc': '<span style="color:#a94442;">' + data.Response + '</span>',
'icon': '#fa fa-exclamation-triangle'
}
}
}
res.add(data)
})
}
function execute (id, payload) {
if (payload == 'prefs') {
app.openPreferences('hain-plugin-pximg')
return
}
if (payload == 'set_query') {
app.setQuery(id)
return
}
if (payload == 'copy_to_clipboard') {
clipboard.writeText(id)
return
}
if (payload !== 'open') {
return
}
shell.openExternal(id)
}
function renderPreview (id, payload, render) {
var preview = preview_html.replace('%imageurl%', id)
render(preview)
}
return {
search,
execute,
renderPreview
}
}