[Steem.js] Retrieve articles of one author | Steem.js로 한사람의 글을 순서대로 불러오기

in steemdev •  7 years ago  (edited)

GitHub : https://github.com/junn279/steemjs_example/blob/master/steem06.php
실제 구현된 페이지 : https://mediteam.us/author/junn

오늘의 함수는 steem.api.getDiscussionsByAuthorBeforeDate 이다. 이 함수는 순서대로 다음과 같은 변수를 필요로 한다.
To retrieve articles of one author, we have to use getDiscussionsByAuthorBeforeDate function. It needs variables as below.

  1. author - author의 ID (ID of author)

  2. startPermlink - 마지막 permlink 주소, 예를 들어 이미 5개를 받았고, 6-10 번째를 받고자 할 때, 넣어야하는 5번째 글의 permlink 값이다.
    Permlink value of last retrieved article. For exmple, if you got 5 posts and you want to get 5 more, this value is the permlink of the 5th article.

  3. date - 받을 자료들이 작성된 마지막 날짜, ('오늘'을 입력하면 '오늘' 이전에 쓴 글들을 검색한다. 즉 2017년 이전 글만 검색을 원한다면, 2016-12-31T23:55:55 를 넣어야 한다.)
    A date you want to start search, if you put 'today' and then you can get articles range of 'today'-'first article'. If you want to get articles created before 2017, put '2016-12-31T23:55:55'.

  4. limit - 한번에 받을 자료의 숫자 / number of articles you want to get.

var today = new Date();
today.setMinutes(today.getMinutes()-today.getTimezoneOffset());     //GMT +9:00 한국
var today_str = today.format('yyyy-mm-dd') + 'T' + today.format('HH:MM:ss');

이 부분이 '오늘' 날짜를 받아오는 방식이다. 여기서 둘째줄은 사실 큰 의미가 없다. 한국은 +9:00의 시차이기 때문에, 그러나 마이너스 시차일 경우 이야기가 달라져서 코드가 아마 꼭 필요할 것이다.
왜냐면, 스팀 서버에 저장되는 자료는 보니까 표준시 기준으로 작성된다. 즉 한국에서 밤10시에 작성한 글은 created time이 낮1시로 되어있게 되서 이것을 보정할 필요가 있다. Date.format을 쓸 경우 사용자의 지역 locale로 시간을 불러오기 때문에 반대로 그 시차를 빼줘야 서버 시간과 일체하는 시간이 나온다. (말로 쓰려니 더 헷갈린다. 한번 생각해보시길)

To define 'today', you need to calculate a time difference. As I know (and see), steem server record a creation time as GMT 0. So, if your time difference is plus (+) then no matter you put second line of source code. But if minus(-), then it is eseential.

함수는 다음과 같이 구성된다.
showPage라는 함수에서 author와 limit을 인자로 받는데, limit은 5로 전역변수로 설정, 이 경우 함수 내에서는 window.limit으로 접근이 가능하다(global 로 지정할 수 도 있다)

I put variables 'permlink' and 'limit' as global, and I can get this variables by 'window.limit or window.permlink' (or use 'global').

var limit = 5;
var last_permlink = '';

function getMorePage(author)
{
    $('#more_button').attr('disabled','true');  // More (더보기) 버튼 클릭시 : 자료를 받고 나서 다시 활성화 시킨다.
    var limit = window.limit+1;                 // 마지막 permlink를 포함하여 자료를 받아오기 떄문에 갯수는 n+1이 된다.
    showPage(author,limit);
}

function getFirstPage(author)
{
    $('#more_button').attr('disabled','true');  // More (더보기) 버튼 클릭시 : 자료를 받고 나서 다시 활성화 시킨다.
    var limit = window.limit;                   // 처음 n개의 자료를 가져옴/
    window.last_permlink = "";
    $('#aritcles_body').html('');
    showPage(author,limit);
}


function showPage(author,limit)
{
    var today = new Date();
    var today_str = today.format('yyyy-mm-dd') + 'T' + today.format('HH:MM:ss');
    console.log("start_last_permlink",window.last_permlink);
    steem.api.getDiscussionsByAuthorBeforeDate(author,window.last_permlink,today_str,limit, function(err, result) {
        //console.log(err, result);
        var arrArticles = new Array();
        for(var i=0;i<result.length;i++)
        {
            arrArticles[arrArticles.length] = result[i];
        }
        arrArticles.sort(compare);

        var i = 0;
        if(limit == window.limit + 1)i++;   //n+1로 들어올 경우 받아온 자료 중 0번째 자료는 지난 번의 마지막 자료다.
        for(i;i<arrArticles.length;i++)
        {
            //이 부분에서 글 하나하나를 html로 뿌려준다.
        }
        window.last_permlink = arrArticles[arrArticles.length-1].permlink;
        console.log("last_permlink",window.last_permlink);  //마지막 permlink를 전역변수에 저장
    });
}

이 함수를 통해 받아온 자료를 출력해주고, 맨 마지막엔 전역변수로 마지막 자료의 permlink를 전역변수에 저장, 그리고 그 다음부터 호출할 시에는 전역변수의 permlink를 이용해서 검색이 들어간다. 사실 함수로는 깔끔한 모양새는 아니나, 일단 생각나는대로 작성한 코드이다.

Through this function, show the articles and save last permlink at last. After then, search next articles using saved perlink. These functions are not 'cozy', becuase I made this as idea came to my mind.

https://mediteam.us/author/junn 에 들어가보면 어떻게 작동하는지 확인이 가능하다.

혹시 이해가 안되거나 문의사항이 있으시면 댓글에 달아주시면 됩니다.
If there is something you want to comment, please reply.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!