Skip to content

Instantly share code, notes, and snippets.

@hibiyasleep
Last active May 4, 2017 08:44
Show Gist options
  • Save hibiyasleep/a29a1099147733d9d1e526d6671fa9c9 to your computer and use it in GitHub Desktop.
Save hibiyasleep/a29a1099147733d9d1e526d6671fa9c9 to your computer and use it in GitHub Desktop.
calcium-tweet
#!/usr/bin/env node
'use strict'
const calcium = require('calcium')
const moment = require('moment')
const fs = require('fs')
const T = require('twit')
const rc = JSON.parse(fs.readFileSync('/home/hibiya/.calciumrc').toString())
const SAT = moment('2016-11-16 08:10')
let d = new Date()
let today = d.getDate()
let hour = d.getHours()
// 0 6 12 18 +0
// | | | | |
// ============ lunch
// ====== dinner
// ===== lunch (do date+=1, or do not tweet in this time)
let type = (hour <= 12 || 18 < hour)? 'lunch' : 'dinner'
let typeL = {
lunch: '점심',
dinner: '저녁'
}[type]
const getFeature = function(account, feature) {
return account.feature && account.feature.indexOf(feature) !== -1
}
for(let id in rc.accounts) {
let account = rc.accounts[id]
let t = new T(account.key)
if(account.timesIn && account.timesIn.indexOf(hour) == -1)
continue
calcium.get(account.school, (e, d) => {
let tweet = ''
let now
let m = moment()
if(e)
return console.error(e)
if(!d[today] || !d[today][type])
return
now = d[today][type]
if(getFeature(account, 'private'))
tweet += `@_ @${id} `
if(getFeature(account, 'gosam'))
tweet += `[${moment(SAT.diff(moment())).format("DDD일 H시간 m분 s초")} ${typeL}]`
else
tweet += `[${m.format('M월 D일')} ${typeL}] ${hour}`
tweet += '\n'
if(account.filter) {
let filter = new RegExp(account.filter, 'gi')
now = now.map( m => m.replace(filter, '') )
}
tweet += now.join('\n')
if(tweet.length > 140) {
tweet = `${tweet.slice(0, 132)}..\n(${now.length}개)`
}
t.post('statuses/update', {
status: tweet,
in_reply_to_status_id: account.replyTo || ''
}, (e, d) => {
if(e) console.error(e)
})
})
}
@mu-hun
Copy link

mu-hun commented Apr 29, 2017

혹시 /home/hibiya/.calciumrc json 파일 구조를 보여주실 수 있으신가요?

@hibiyasleep
Copy link
Author

늦어서 죄송합니다. 설명을 위해 YAML 형식으로 작성해뒀으며, 필수가 아닌 값은 모두 선택적입니다.

accounts:
  screen_name: # 트위터 ID를 key로
    school: 'SCHOOL_CODE_HERE' # 필수
    feature: [...] # 옵션 string들의 배열
    # private: 트윗 맨 앞에 '@_ @screen_name'을 붙입니다.
    # gosam: 수능 카운트다운(...)
    filter: 'regex string' # /g로 매치하여 공백으로 replace합니다.
    timesIn: [12] # 현재 시간이 지정된 시(時)일 때만 트윗합니다.
    key: # 필수
      consumer_key: ~~~
      consumer_secret: ~~~
      access_token: ~~~
      access_token_secret: ~~~
  # (다른 계정들...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment