ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • twit4cpp - 트위터 Twitter API : Streaming API (UserStreams) 예제
    Programming/twitterAPI 2010. 12. 6. 16:59
    반응형
    [Programming/twit4cpp] - twit4cpp - 트위터 Twitter API : tweets api 사용 예제
    [Programming/twit4cpp] - twit4cpp - 트위터 Twitter API : 다이렉트메시지(DirectMessages) 예제
    [Programming/twit4cpp] - twit4cpp - 트위터 Twitter API : 타임라인(Timeline) 가져오기 예제
    [Programming/twit4cpp] - twit4cpp - 트위터 Twitter API : XAuth 인증 예제
    [Programming/twit4cpp] - twit4cpp - 트위터 Twitter API : OAuth 인증 예제

    #include <twit/Twitter.h>
    #include <stdio.h>
    
    #if defined(WIN32)||defined(_WIN32)
    #  if _DEBUG
    #     pragma comment(lib,"../../../lib/twit4cppd.lib")
    #  else
    #     pragma comment(lib, "../../lib/twit4cpp.lib")
    #  endif
    #endif
    
    using namespace std;
    using namespace Twit;
    
    // IUserStream 인터페이스를 상속받아 스트리밍 데이터 처리 클래스 구현
    class UserStreamImpl:public IUserStream
    {
    	virtual void onFriendList(Value::IDs userIDs)
    	{
    		puts(__FUNCTION__);
    		for(size_t i=0; i<userIDs.size(); i++)
    		{
    			puts(userIDs[i].toString().c_str());
    		}
    	}
    
    	virtual void onStatus(Value::Status status)
    	{
    		puts(__FUNCTION__);
    		puts(status.getUser()->getScreenName().c_str());
    		wprintf(L"text : %s\n", status.getText().c_str());
    	}
       virtual void onDeleteStatus         (Value::UniqueID userId, Value::UniqueID statusId)
       {
          puts(__FUNCTION__);
          printf("userid:%s\n", userId.toString().c_str());
          printf("statusid:%s\n", statusId.toString().c_str());
       }
       virtual void onDeleteDirectMessage  (Value::UniqueID userId, Value::UniqueID dmId)
       {
          puts(__FUNCTION__);
          printf("userid:%s\n", userId.toString().c_str());
          printf("dmid:%s\n", dmId.toString().c_str());
       }
       virtual void onDirectMessage(Value::DirectMessage dm)
       {
          puts(__FUNCTION__);
          printf("recipient:%s\n",dm.getRecipientScreenName().c_str());
          printf("sender:%s\n", dm.getSenderScreenName().c_str());
       }
       virtual void onUserUpdate(Value::User user)
       {
          puts(__FUNCTION__);
          printf("userId:%s\n", user.getId().toString().c_str());
       }
       virtual void onFollow(Value::User source, Value::User target)
       {
          puts(__FUNCTION__);
    	  printf("source : %s\n", source.getScreenName().c_str());
    	  printf("target : %s\n", target.getScreenName().c_str());
       }
       virtual void onUnfollow(Value::User source, Value::User target)
       {
          puts(__FUNCTION__);
    	  printf("source : %s\n", source.getScreenName().c_str());
    	  printf("target : %s\n", target.getScreenName().c_str());
       }
    
       virtual void onBlock    (Value::User source, Value::User target)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
       }
       virtual void onUnblock  (Value::User source, Value::User target)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
       }
    
       virtual void onFavorite(Value::User source, Value::User target, Value::Status targetObject)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
          printf("object:%s\n", targetObject.getId().toString().c_str());
       }
       virtual void onUnfavorite(Value::User source, Value::User target, Value::Status targetObject)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
          printf("object:%s\n", targetObject.getId().toString().c_str());
       }
       virtual void onListMemberAdded(Value::User owner, Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("owner:%s\n", owner.getScreenName().c_str());
          printf("fullname:%s\n", targetObject.getFullName().c_str());
       }
       virtual void onListMemberRemoved (Value::User owner, Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("owner:%s\n", owner.getScreenName().c_str());
          printf("fullname:%s\n", targetObject.getFullName().c_str());
       }
       virtual void onListDestroyed  (Value::User owner,Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("owner:%s\n", owner.getScreenName().c_str());
          printf("fullname:%s\n", targetObject.getFullName().c_str());
       }
       virtual void onListCreated    (Value::User owner, Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("owner:%s\n", owner.getScreenName().c_str());
          printf("object:%s\n", targetObject.getFullName().c_str());
       }
    
       virtual void onListUserSubscribed   (Value::User source, Value::User target, Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
          printf("object:%s\n", targetObject.getFullName().c_str());
       }
       virtual void onListUserUnsubscribed (Value::User source, Value::User target, Value::List targetObject)
       {
          puts(__FUNCTION__);
          printf("source:%s\n", source.getScreenName().c_str());
          printf("target:%s\n", target.getScreenName().c_str());
          printf("object:%s\n", targetObject.getFullName().c_str());
       }
    };
    
    int main(int argc, char* argv[])
    {
       /* twit4cpp Sample Application */
       try
       {
          string
             customerKey("2rBtCAh5o10zMlS2FbRYAA"),
             customerSecert("MoZvHvDGbi6zoPu6Q1SZHsalGynP8qWt9dlB1NKlvQg");
    
    	  UserStreamImpl us;
          Twitter twit(customerKey, customerSecert);
    	  twit.setAccessToken(string("access_token"),string("access_token_secret"));
          
    	  Error err;
    	  if(!twit.UserStream.start(us, err))
    	  {
    		  printf("error userstream : %s\n", err.getMessage().c_str());
    	  }
         while(1)
         {
    		 // do something...
    		 Sleep(10000);
    		 // twit.UserStream.stop();
         }
       }
       catch(const Error& e)
       {
          printf("code : %d\n", e.getCode());
          printf("message : %s\n", e.getMessage().c_str());
       }   
    	return 0;
    }
    

    소스 & 샘플 다운로드

    반응형
Designed by Tistory.