Skip to content

Commit

Permalink
server.js & index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyjun authored Jul 22, 2021
1 parent 1303a5b commit 393f058
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Cat</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body></body>

<script src="https://cdn.socket.io/4.0.1/socket.io.min.js"></script>
<script>
const socket = io();
socket.on('tweet', function(tweet){
let target = document.querySelector("body");
let p = document.createElement('img');

p.setAttribute("src", tweet);
target.appendChild(p);
});
</script>
</html>
10 changes: 10 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"optionalDependencies": {
"node-tweet-stream": "^2.1.0"
},
"dependencies": {
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"socket.io": "^4.1.3"
}
}
63 changes: 63 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 8080;

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

const fetch = require('node-fetch');
const run = async (src) => {
let url = "NGROK PUBLIC_URL/?";
// plz input your public_url/?
// YOU MUST ADD /? TO END OF URL
url += src;
const response = await fetch(url);
const json = await response.json();

if(json['cat'] === 'false') return false;

return true;
}

const Twitter = require('node-tweet-stream');
const twitter = new Twitter({
consumer_key: "TWITTER API KEY",
consumer_secret: "TWITTER API KEY",
token: "TWITTER API KEY",
token_secret: "TWITTER API KEY"
});

twitter.track('cat');

//twitter.track('고양이');
//twitter.language('ko');

//twitter.track('ねこ');
//twitter.track('ネコ');
//twitter.track('猫');
//twitter.language('ja');

twitter.on('tweet', tweet => {
if(tweet.extended_entities === undefined) return;
let medias = tweet.extended_entities.media;

medias.forEach(media => {
if(media.type === 'photo')
{
run(media.media_url).then((ret => {
if(true === ret)
io.emit('tweet', media.media_url);
})).catch((err)=>{console.log(err)})
}
});
});

twitter.on('error', err => {
console.error(err);
});

http.listen(port, function(){
console.log('listening on *:' + port);
});

0 comments on commit 393f058

Please sign in to comment.