Last active
August 9, 2017 09:19
-
-
Save malexandre/9e37a391aa82c323366ab4fc312d4b22 to your computer and use it in GitHub Desktop.
Gif or emojis ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var server = require('./server/server') | |
server.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node-gif", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "nodemon index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"engines": { | |
"node": "8.2.1" | |
}, | |
"author": "Marc ALEXANDRE (marc@malexandre.fr)", | |
"license": "DWTFYWT", | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"emojilib": "^2.2.7", | |
"express": "^4.15.4", | |
"giphy-api": "^1.2.6", | |
"nodemon": "^1.11.0", | |
"sqlite3": "^3.1.9" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bodyParser = require("body-parser") | |
var emojilib = require("emojilib") | |
var express = require('express') | |
var giphy = require('giphy-api')() | |
var sqlite3 = require('sqlite3') | |
var exports = module.exports = {} | |
exports.run = () => { | |
var db = new sqlite3.Database('favorites.db') | |
db.exec('CREATE TABLE IF NOT EXISTS Favorites (id TEXT PRIMARY KEY, type TEXT, meta BLOB)') | |
var app = express() | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.use(bodyParser.json()) | |
app.get('/api/:type/search/:query', (req, res) => { | |
var offset = parseInt(req.query.offset) || 0 | |
var nextPage = `/api/${req.params.type}/search/${req.params.query}?offset=${offset + 20}` | |
if (req.params.type === 'gifs') { | |
giphy.search({ | |
q: req.params.query, | |
limit: 20, | |
offset: offset | |
}).then(resGiphy => { | |
if (resGiphy.pagination.total_count <= resGiphy.pagination.count + resGiphy.pagination.offset) { | |
nextPage = undefined | |
} | |
res.json({ | |
items: resGiphy.data, | |
nextPage: nextPage | |
}) | |
}).catch(reason => console.error(reason)) | |
} | |
else if (req.params.type === 'emojis') { | |
var emojis = [] | |
Object.keys(emojilib.lib).forEach(key => { | |
var match = key.includes(req.params.query) | |
if (!match) { | |
match = emojilib.lib[key].keywords.find(word => word.includes(req.params.query)) !== undefined | |
} | |
if (match) { | |
emojis.push(emojilib.lib[key]) | |
} | |
}) | |
res.json({ | |
items: emojis.splice(offset, 20), | |
nextPage: results.length > 20 ? nextPag : undefined | |
}) | |
} | |
else { | |
res.end() | |
} | |
}) | |
app.post('/api/:type/favorite', (req, res) => { | |
if (!req.body.type || req.body.type !== req.params.type || !req.body.id || | |
Object.keys(req.body.meta || {}).length === 0) { | |
res.sendStatus(422) | |
return | |
} | |
db.run("INSERT OR REPLACE INTO Favorites VALUES (?, ?, ?)", | |
req.body.id, req.params.type, JSON.stringify(req.body.meta)) | |
res.sendStatus(200) | |
}) | |
app.get('/api/:type/favorite', (req, res) => { | |
var offset = parseInt(req.query.offset) || 0 | |
var nextPage = `/api/${req.params.type}/favorite?offset=${offset + 20}` | |
db.all("SELECT meta FROM Favorites WHERE type = ? LIMIT 21 OFFSET ?", req.params.type, offset, (err, rows) => { | |
res.json({ | |
items: (rows || []).map(favorite => JSON.parse(favorite.meta)), | |
nextPage: (rows || []).length > 20 ? nextPage : undefined | |
}) | |
}) | |
}) | |
var server = app.listen(8000) | |
process.on('SIGINT', () => { | |
db.close() | |
server.close() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment