Toggle navigation
Toggle navigation
This project
Loading...
Sign in
이해님
/
term-project
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
haenim
2020-12-02 04:06:39 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f848f7e7422eb13def4df656259b26e838aa5147
f848f7e7
1 parent
ad166ce8
get user's timeline by twitter api
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
74 deletions
router/main.js
server.js
twitter-controller.js
views/index.html
views/timeline.html
router/main.js
View file @
f848f7e
module
.
exports
=
function
(
app
)
{
app
.
get
(
'/'
,
function
(
req
,
res
){
//index.html 가져오기
res
.
render
(
'index.html'
)
});
app
.
get
(
'/timeline/:id'
,
function
(
req
,
res
){
res
.
render
(
'timeline.html'
)
});
// app.get('/timeline/:id',function(req,res){
// res.render('timeline.html')
// });
}
...
...
server.js
View file @
f848f7e
var
express
=
require
(
'express'
);
var
express
=
require
(
'express'
);
//express 모듈 불러오기
var
app
=
express
();
const
tweetsController
=
require
(
'./twitter-controller'
);
var
router
=
require
(
'./router/main'
)(
app
);
// 라우터 모듈인 main.js 를 불러와서 app 에 전달해줍니다.
app
.
set
(
'views'
,
__dirname
+
'/views'
);
//서버가 읽을 수 있도록 HTML 의 위치를 정의해줍니다.
app
.
set
(
'view engine'
,
'ejs'
);
//서버가 HTML 렌더링을 할 때, EJS 엔진을 사용하도록 설정합니다.
app
.
engine
(
'html'
,
require
(
'ejs'
).
renderFile
);
app
.
get
(
'/timeline/:screen_name'
,
tweetsController
.
getUserTweets
);
// '/timeline/:screen_name'형식의 url이 들어오면 뒤의 함수를 실행시킴
var
server
=
app
.
listen
(
3000
,
function
(){
//3000 포트 사용
console
.
log
(
"Express server has started on port 3000"
)
console
.
log
(
"Express server has started on port 3000"
)
;
})
app
.
use
(
express
.
static
(
'public'
));
...
...
twitter-controller.js
0 → 100644
View file @
f848f7e
const
Twitter
=
require
(
'twitter'
);
const
client
=
new
Twitter
({
consumer_key
:
'Q4xyL4HBupStqkUf3FaKeDlSL'
,
consumer_secret
:
'xB9ROWLAPlPW7tntBUsFgVZd9qcaCSDDAo5fFCH1qWg7oAwJLO'
,
access_token_key
:
'1330868660072660992-0l3jauBmdEP16hXPviH5W1DMS46X9B'
,
access_token_secret
:
'jx5xtDHam5SUTSndp7uVqsTpbSJiD4OIKL8IYKg1ZtTSZ'
});
exports
.
getUserTweets
=
async
function
(
req
,
res
){
try
{
let
data
=
client
.
get
(
'statuses/user_timeline'
,
req
.
params
,
function
(
error
,
tweets
,
response
){
//트위터 api에서 유저의 타임라인을 가져옴 req.params에 유저 아이디가 들어있음
if
(
!
error
){
console
.
log
(
tweets
);
//가져온 타임라인 내용 콘솔창에 출력
res
.
render
(
'timeline.html'
,
tweets
);
//timeline.html 화면에 뿌려줌 그리고 tweets값을 저 페이지로 보냄
}
});
//아이디를 토대로 타임라인 가져오기
}
catch
(
err
){
//에러 발생하면 실행
console
.
log
(
err
);
res
.
sendStatus
(
500
);
}
}
\ No newline at end of file
views/index.html
View file @
f848f7e
...
...
@@ -23,8 +23,8 @@
<button
class=
"btn-search"
type=
"button"
onclick=
"movePage()"
>
SEARCH
</button>
</div>
<script
type =
"text/javascript"
>
function
movePage
(){
//페이지 이동을 위한 함수
location
.
href
=
"/timeline/"
+
document
.
getElementById
(
'search'
).
value
function
movePage
(){
//페이지 이동을 위한 함수
search버튼을 누르면 실행됨
location
.
href
=
"/timeline/"
+
document
.
getElementById
(
'search'
).
value
//url을 이렇게 변경함
}
</script>
</div>
...
...
views/timeline.html
View file @
f848f7e
...
...
@@ -3,76 +3,10 @@
<body>
<script>
alert
(
"Hello, world!"
);
</script>
<script
src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"
></script>
<script
src=
"http://oauth.googlecode.com/svn/code/javascript/oauth.js"
></script>
<script
src=
"http://oauth.googlecode.com/svn/code/javascript/sha1.js"
></script>
<script
type=
"text/javascript"
>
$
(
function
(){
fn_twitterTimeline
();
});
function
fn_twitterTimeline
(){
var
twitterPrm
=
{
api
:
'https://api.twitter.com/1.1/statuses/user_timeline.json'
,
count
:
100
,
callback
:
"fn_makedocTwitter"
,
consumerKey
:
"Q4xyL4HBupStqkUf3FaKeDlSL"
,
consumerSecret
:
"xB9ROWLAPlPW7tntBUsFgVZd9qcaCSDDAo5fFCH1qWg7oAwJLO"
,
accessToken
:
"1330868660072660992-0l3jauBmdEP16hXPviH5W1DMS46X9B"
,
tokenSecret
:
"zQjx5xtDHam5SUTSndp7uVqsTpbSJiD4OIKL8IYKg1ZtTSZ"
}
var
oauthMessage
=
{
method
:
"GET"
,
action
:
twitterPrm
.
api
,
parameters
:{
count
:
twitterPrm
.
count
,
callback
:
twitterPrm
.
callback
,
oauth_version
:
"1.0"
,
oauth_signature_method
:
"HMAC_SHA1"
,
oauth_consumer_key
:
twitterPrm
.
consumerKey
,
oauth_token
:
twitterPrm
.
accessToken
}
};
OAuth
.
setTimestampandNonce
(
oauthMessage
);
OAuth
.
SignatureMethod
.
sign
(
oauthMessage
,{
consumerSecret
:
twitterPrm
.
consumerSecret
,
tokenSecret
:
twitterPrm
.
tokenSecret
});
var
twJsonpath
=
OAuth
.
addToURL
(
oauthMessage
.
action
,
oauthMessage
.
parameters
);
$
.
ajax
({
type
:
oauthMessage
.
method
,
url
:
twJsonpath
,
dataType
:
"jsonp"
,
jsonp
:
false
,
cache
:
true
});
}
function
fn_makedocTwitter
(
data
){
<
DIV
id
=
"search"
><
/DIV
>
var
timeLine
=
''
;
var
text
;
var
dateObj
=
new
Data
;
for
(
var
i
=
0
,
len
=
data
.
length
;
i
<
len
;
i
++
){
text
=
data
[
i
].
text
;
text
=
text
.
replace
(
/
(
s
?
https
?
:
\/\/[
-_.!~*'()a-zA-Z0-9;
\/
?:@&=+$,%#
]
+
)
/gi
,
'<a href="$1">$1</a>'
);
text
=
text
.
replace
(
/@
(\w
+
)
/gi
,
'<a href="http://twiiter.com/search?q=%23$1">#$1</a>'
);
text
.
text
.
replace
(
/@
(\w
+
)
/gi
,
'<a href="http://twitter.com/$1">@$1</a>'
);
timeLine
+=
"<p>"
+
text
+
"</p><p>"
+
dateObj
+
"</p>"
;
}
$
(
'#div_snstwitter'
).
html
(
timeLine
);
}
console
.
log
(
tweets
);
</script>
...
...
Please
register
or
login
to post a comment