-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Twit = require('twit')
const dotenv = require('dotenv')
const axios = require('axios')
const cheerio = require('cheerio')
dotenv.config()
var currentTrack = ''
const T = new Twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
})
const seratoPlaylistScrape = async () => {
const url = `https://serato.com/playlists/${process.env.SERATO_DISPLAY_NAME}/live`
try {
const { data } = await axios.get(url)
const $ = cheerio.load(data)
const results = $('div.playlist-trackname')
let currentSong = results.last().text()
let tweet = { status: `${currentSong.trim()}` }
return tweet
} catch (err) {
console.error(err)
}
}
const tweetIt = () => {
seratoPlaylistScrape().then((response) => {
console.log('- - - - - - - - - - - - - - -')
console.log('RESPONSE: ', response.status)
if (response.status !== currentTrack) {
// set currentTrack to newly scraped track if different
currentTrack = response.status
// callback to handle twitter response
let tweeted = (err, data, response) => {
if (err) {
console.log('ERROR: ', err.allErrors[0].message)
} else {
console.log(
'NEW TWEET SUCCESSFULLY POSTED @',
new Date().toLocaleString()
)
console.log("Twitter Response: ", response)
}
}
T.post('statuses/update', response, tweeted)
return
} else {
return
}
})
}
tweetIt()
setInterval(() => {
tweetIt()
}, 30000)
// add method for script to autorespond to track queries tweeted to the twitter account?