diff --git a/.vuepress/components/GithubFeed.vue b/.vuepress/components/GithubFeed.vue new file mode 100644 index 0000000..1466b28 --- /dev/null +++ b/.vuepress/components/GithubFeed.vue @@ -0,0 +1,112 @@ + + + + + \ No newline at end of file diff --git a/github-feed.md b/github-feed.md new file mode 100644 index 0000000..72898e2 --- /dev/null +++ b/github-feed.md @@ -0,0 +1,5 @@ +--- +title: Github Feed Example +--- + + \ No newline at end of file diff --git a/package.json b/package.json index 6fb9d1b..5993e7e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "license": "ISC", "dependencies": { "@octokit/rest": "^15.4.0", + "async": "^2.6.0", "cors": "^2.8.4", + "cron": "^1.3.0", "dotenv": "^5.0.1", "express": "^4.16.3", "heroku-ssl-redirect": "0.0.4", diff --git a/server.js b/server.js index 3b42e4a..dd02bdc 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,23 @@ // server.js +// env variables +require('dotenv').config(); +// Async +var async = require("async"); +// Express etc var sslRedirect = require('heroku-ssl-redirect'); var express = require('express'); var path = require('path'); var serveStatic = require('serve-static'); var request = require('request'); var cors = require('cors'); +// Cron +var CronJob = require('cron').CronJob; +// Github API +var octokit = require('@octokit/rest')(); +// Redis +var redis = require("redis"), +redisClient = redis.createClient(process.env.REDISCLOUD_URL); app = express(); app.use(serveStatic(__dirname + "/.vuepress/dist")); @@ -39,7 +51,65 @@ app.get('/forward', function(req, res) { }); +app.get('/github-feed', function(req, res) { + + redisClient.zrevrange('events', 0, 9, function(err, reply) { + + var events = []; + + reply.forEach(function(item) { + events.push(JSON.parse(item)); + }); + + res.json(events); + + }); + +}); + var port = process.env.PORT || 8080; app.listen(port); console.log('server started '+ port); + +function updateGithubFeed() { + + octokit.activity.getEventsForOrg({ + org: 'lbryio', + per_page: 20, + page: 1 + }).then(function({data}) { + + async.eachSeries(data, function(item, callback) { + + var eventString = JSON.stringify(item); + + redisClient.zrank('events', eventString, function(err, reply) { + + if(reply == null) { + + redisClient.zadd('events', item.id, eventString, callback); + + } else { + + callback(); + + } + + }); + + }, function() { + + // Keep the latest 50 events + redisClient.zremrangebyrank('events', 0, -51); + + console.log('Updated Github feed'); + + }); + + }); + +} + +// Update Github feed every minute +new CronJob("0 * * * * *", updateGithubFeed, null, true, 'America/Los_Angeles'); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 0d5389c..529f71e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -297,6 +297,12 @@ async@^1.5.2: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" +async@^2.6.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4" + dependencies: + lodash "^4.14.0" + asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" @@ -1114,6 +1120,12 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" +cron@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/cron/-/cron-1.3.0.tgz#7e459968eaf94e1a445be796ce402166c234659d" + dependencies: + moment-timezone "^0.5.x" + cross-spawn@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982" @@ -3156,6 +3168,10 @@ lodash@^4.0.0, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@~4.17.4: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" +lodash@^4.14.0: + version "4.17.10" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7" + log-symbols@^2.1.0: version "2.2.0" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" @@ -3470,6 +3486,16 @@ mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkd dependencies: minimist "0.0.8" +moment-timezone@^0.5.x: + version "0.5.17" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.17.tgz#3c8fef32051d84c3af174d91dc52977dcb0ad7e5" + dependencies: + moment ">= 2.9.0" + +"moment@>= 2.9.0": + version "2.22.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad" + move-concurrently@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"