Last active
July 29, 2018 12:50
-
-
Save Shikugawa/96ff7f2827df2fa23632edd88be1a341 to your computer and use it in GitHub Desktop.
image2slack
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
const fs = require('fs'); | |
const twitter = require('twitter'); | |
const redis = require('redis'); | |
class RedisClient { | |
constructor() { | |
this.client = redis.createClient(); | |
this.key = "medium"; | |
} | |
deleteDuplicateQueue(obj) { | |
const medium = obj.medium; | |
const redisMedium = this.client.lrange(this.key, 0, -1); | |
obj.medium = medium.filter(elem => !redisMedium.includes(elem)); | |
} | |
destroyAllQueue() { | |
for (let index = 0; index < this.client.llen(this.key); index++) { | |
this.client.lpop(this.key); | |
} | |
} | |
insertQueue(tweets) { | |
tweets.forEach((value, index) => { | |
this.client.lpush(this.key, value); | |
}); | |
} | |
} | |
const authorize = async () => { | |
const readFileAsync = path => { | |
return new Promise((resolve, reject) => { | |
fs.readFile(path, 'utf8', (err, data) => { | |
if (err) { reject(err); return; } | |
resolve(data); | |
}); | |
}); | |
}; | |
const twitterConfig = JSON.parse(await readFileAsync('twitter_config.json')); | |
const twitterClient = new twitter(twitterConfig); | |
const slackClient = await JSON.parse(readFileAsync('slack_config.json')); | |
return { | |
'twitterClient': twitterClient, | |
'slackClient': slackClient | |
}; | |
}; | |
const getFavorites = async client => { | |
const fetchFavoriteAsync = () => { | |
return new Promise((resolve, reject) => { | |
/* | |
fetch 50 favorites per 10 minutes. | |
if fetched tweets are duplicated, destroy | |
*/ | |
client.get('favorites/list', { | |
screen_name: '9z0q', | |
count: 50 | |
}, (error, tweets, response) => { | |
if(error) reject(error); | |
resolve(tweets); | |
}); | |
}); | |
} | |
const redis = new RedisClient(); | |
const tweets = await fetchFavoriteAsync(); | |
const medium = tweets.map(elem => { | |
const media = elem.entities.media; | |
if(!media) return media; | |
}).flat; | |
redis.deleteDuplicateQueue({medium}); | |
redis.destroyAllQueue(); | |
redis.insertQueue(medium); | |
return medium; | |
}; | |
const postSlack = async params => { | |
const response = await fetch( | |
'https://slack.com/api/chat.postMessage', | |
{ | |
token: params.token, | |
channel: params.channel, | |
username: "XXX", | |
text: "", | |
attachments: [{ | |
fields: [{ | |
title: "XXX", | |
value: "XXX" | |
}], | |
image_url: params.image_url | |
}] | |
} | |
); | |
return response; | |
} | |
const {twitterClient, slackClient} = authorize(); | |
getFavorites(twitterClient).then(tweets => { | |
tweets.forEach((value, index) => { | |
postSlack({ | |
token: slackClient.token, | |
channel: slackClient.channel, | |
username: slackClient.username, | |
image_url: "XXX" | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment