-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
88 lines (74 loc) · 2.29 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
'use strict';
const got = require('got');
const _ = require('lodash');
const co = require('co');
const fs = require('fs');
const path = require('path');
const ncp = require("copy-paste");
const DESC = 'Search a Gif';
const query_url = 'http://api.giphy.com/v1/gifs/search?q=';
const api_key = '&api_key=dc6zaTOxFJmzC';
function* queryGiphy(query, limit) {
const query_enc = encodeURIComponent(query);
const url = query_url + query_enc + api_key + `&limit=${limit}`;
let result = (yield got(url)).body;
result = JSON.parse(result);
if (result['data']) {
return result['data'].map(x => x);
}
return null;
}
module.exports = (context) => {
const preferences = context.preferences;
let queryLimit = preferences.get().queryLimit;
const shell = context.shell;
let html = 'sdqsdsqsd';
function startup() {
html = fs.readFileSync(path.join(__dirname, 'preview.html'), 'utf8');
preferences.on('update', pref => { queryLimit = pref.queryLimit; });
}
function* search(query, res) {
const query_trim = query.trim();
if (query_trim.length === 0)
return;
const query_enc = encodeURIComponent(query);
if (query_enc) {
let results = yield* queryGiphy(query_trim, queryLimit);
results = _.reject(results, (x) => x === query_trim);
results = results.map((x) => {
return {
id: x['images']['original']['url'],
payload: 'open',
title: x['slug'],
desc: x['images']['original']['url'],
icon: x['images']['fixed_width_small_still']['url'],
preview: true
};
});
return res.add(results);
} else {
return res.add({
id: `http://giphy.com/search/${query_enc}`,
payload: 'goto',
title: 'Search ' + query,
desc: DESC
});
}
}
function execute(id, payload) {
if (payload !== 'open')
return;
if (payload === 'goto') {
return shell.openExternal(id['images']['original']['url']);
}
ncp.copy(id, function() {
context.toast.enqueue('Pasted to clipboard !');
})
}
function renderPreview(id, payload, render) {
var preview = html.replace('%picture%', id);
preview = preview.replace('%url%', id);
render(preview);
}
return { startup, search: co.wrap(search), execute, renderPreview };
};