티스토리 rss 데이터 출력하기
티스토리의 최신글 목록 (기본제공되는 RSS 데이터)을 작업중인 홈페이지에 넣어봄..
제공되는 rss의 형태는 다음과 같다.
<title>MIT에서 공개한 파이썬 무료강좌 목록</title>
<link>http://luckey.tistory.com/396</link>
<description>MIT에서 공개한 파이썬 무료강좌 목록 제 01강 - 연산이란 - 데이터 타입, 연산자 및 변수 소개 제 02강 - 연산자와 피연산자 - 분기문, 조건문 그리고 반복문 제 03강 - 공통 코드 패턴, 반복 프로그램 제 04강 - 기능을 통한 분해 및 추상화, 재귀 소개 제 05강 - 부동 소수점, 계통적 명세화, 루트 찾기 제 06강 - 이분법, 뉴턴/랩슨, 그리고 리스트 소개 제 07강 - 리스트와 가변성, 딕셔너리, 의사코드, 그리고 효율성 소개..</description>
<category>etc</category>
<author>luckey</author>
<guid>http://luckey.tistory.com/396</guid>
<comments>http://luckey.tistory.com/396#entry396comment</comments>
<pubDate>Mon, 18 Jan 2016 10:28:24 +0900</pubDate>
</item>
먼저 cross domain 오류가 발생되기 때문에 아래 Script 먼저 호출해주고
<script type="text/javascript">
$(function () {
var arrayTitle, pubDate, categorys;
var urlStr = 'http://luckey.tistory.com/rss';
$.ajax({
url: urlStr,
type: 'GET',
success: function (res) {
var myXml = res.responseText;
myXml = $.parseXML(myXml);
myXml = $(myXml);
//데이터는 item 안에 들어 있음
arrayTitle = $(myXml.find('item'));
if (arrayTitle.length > 1) {
for (var i = 0; i < arrayTitle.length; i++) {
//날짜 출력을 위한 설정
//<pubDate>Mon, 18 Jan 2016 10:28:24 +0900</pubDate>
pubDate = new Date($(arrayTitle[i]).find('pubDate').text());
if (pubDate.getMonth() + 1 < 10) {
printDate = pubDate.getFullYear() + '-0' + (pubDate.getMonth() + 1) + '-' + pubDate.getDate();
} else {
printDate = pubDate.getFullYear() + '-' + (pubDate.getMonth() + 1) + '-' + pubDate.getDate();
}
//tag가 등록되어 있으면 모두 category에 통합되어 나온다.
categorys = $(arrayTitle[i]).find('category');
$('#tempList').append('<p class="title"><span>' + $(categorys[0]).text() + '</span> <a href="' + $(arrayTitle[i]).find('link').text() + '" target="_blank">' + $(arrayTitle[i]).find('title').text() + '</a> <span>' + printDate + '</span></p>');
$('#tempList').append('<p class="conts">' + $(arrayTitle[i]).find('description').text().substring(0, 100) + '...</p>');
}
}
}
});
});
</script>
<div id="tempList" style="width: 90%; margin: 0 auto;"></div>