Created
October 19, 2016 10:30
-
-
Save bookercodes/ba6458b7f94993e75ae677d215eefc15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
require('dotenv').config() | |
const db = require('sequelize-context') | |
const axios = require('axios') | |
db | |
.connect( | |
'./db/models/*.js', | |
process.env.DB_SCHEMA, | |
process.env.DB_USERNAME, | |
process.env.DB_PASSWORD, { | |
}) | |
.then(function () { | |
return db | |
.models | |
.talk | |
.findAll() | |
}) | |
.then(function (talks) { | |
const promises = talks.map(function (talk) { | |
return new Promise(function (resolve, reject) { | |
axios | |
.get(`https://api.wistia.com/v1/stats/medias/${talk.wistiaMediaId}.json?api_password=${process.env.WISTIA_PASSWORD}`) | |
.then(response => { | |
return db | |
.models | |
.talk | |
.update({ | |
views: response.data.play_count | |
}, { | |
where: { | |
id: talk.id | |
} | |
}) | |
}) | |
.then(resolve) | |
}) | |
}) | |
return Promise.all(promises) | |
}) | |
.then(function () { | |
console.log('success') | |
}) | |
.catch(function (error) { | |
console.error('error', error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem here is that you might get partial updates :(