Search in sources :

Example 1 with JSONObject

use of weibo4j.org.json.JSONObject in project twitter-2-weibo by rjyo.

the class Comment method constructWapperComments.

public static CommentWapper constructWapperComments(Response res) throws WeiboException {
    //asJSONArray();
    JSONObject json = res.asJSONObject();
    try {
        JSONArray comments = json.getJSONArray("comments");
        int size = comments.length();
        List<Comment> comment = new ArrayList<Comment>(size);
        for (int i = 0; i < size; i++) {
            comment.add(new Comment(comments.getJSONObject(i)));
        }
        long previousCursor = json.getLong("previous_curosr");
        long nextCursor = json.getLong("next_cursor");
        long totalNumber = json.getLong("total_number");
        String hasvisible = json.getString("hasvisible");
        return new CommentWapper(comment, previousCursor, nextCursor, totalNumber, hasvisible);
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Example 2 with JSONObject

use of weibo4j.org.json.JSONObject in project twitter-2-weibo by rjyo.

the class CallbackServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:type");
    if (request.getParameter("denied") != null) {
        response.sendRedirect("/");
        return;
    }
    HttpSession session = request.getSession(false);
    String loginUser = (String) session.getAttribute(Keys.SESSION_LOGIN_USER);
    String oauthVerifier = request.getParameter("oauth_verifier");
    DBHelper helper = (DBHelper) request.getAttribute(Keys.REQUEST_DB_HELPER);
    if (r.is(":type", "weibo.jsp")) {
        String code = request.getParameter("code");
        if (code != null) {
            T2WUser tid = helper.findOneByUser(loginUser);
            if (tid.getToken() == null) {
                // send for the first time
                session.setAttribute(Keys.SESSION_PROMPT_TWEET, "You are ready to go! Do you want to tweet about this service and share it with your friends?");
            }
            Oauth oauth = new Oauth();
            try {
                AccessToken token = oauth.getAccessTokenByCode(code);
                tid.setToken(token.getAccessToken());
                Weibo weibo = new Weibo();
                weibo.setToken(tid.getToken());
                Account am = new Account();
                try {
                    JSONObject obj = am.getUid();
                    String uid = obj.getString("uid");
                    tid.setWeiboUserId(uid);
                } catch (WeiboException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                helper.saveUser(tid);
            } catch (WeiboException e) {
                log.error(e);
            }
        } else {
            log.error("Can't auth " + loginUser + " for Weibo. " + request.getQueryString());
        }
    } else if (r.is(":type", "twitter")) {
        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter t = factory.getInstance();
            twitter4j.auth.RequestToken req = (RequestToken) session.getAttribute(Keys.SESSION_REQUEST_TOKEN);
            twitter4j.auth.AccessToken accessToken = t.getOAuthAccessToken(req, oauthVerifier);
            session.removeAttribute(Keys.SESSION_REQUEST_TOKEN);
            if (accessToken != null) {
                t.setOAuthAccessToken(accessToken);
                User user = t.verifyCredentials();
                loginUser = user.getScreenName();
                T2WUser tid = helper.findOneByUser(loginUser);
                if (tid.getTwitterToken() == null) {
                    // save latest id for the first time. sync from that tweet
                    ResponseList<Status> tl = t.getUserTimeline();
                    if (tl.size() > 0) {
                        Status s = tl.get(0);
                        tid.setLatestId(s.getId());
                    }
                }
                tid.setTwitterToken(accessToken.getToken());
                tid.setTwitterTokenSecret(accessToken.getTokenSecret());
                helper.saveUser(tid);
                session.setAttribute(Keys.SESSION_LOGIN_USER, loginUser);
            }
        } catch (TwitterException e) {
            log.error("Twitter Exception", e);
            throw new RuntimeException(e);
        }
    }
    String requestUrl = (String) session.getAttribute(Keys.SESSION_REQUEST_URL);
    if (requestUrl != null) {
        session.removeAttribute(Keys.SESSION_REQUEST_URL);
        response.sendRedirect(requestUrl);
    } else {
        response.sendRedirect("/u/" + loginUser);
    }
}
Also used : Account(weibo4j.Account) T2WUser(h2weibo.model.T2WUser) HttpSession(javax.servlet.http.HttpSession) DBHelper(h2weibo.model.DBHelper) JSONException(weibo4j.org.json.JSONException) twitter4j(twitter4j) Oauth(weibo4j.Oauth) Weibo(weibo4j.Weibo) WeiboException(weibo4j.model.WeiboException) T2WUser(h2weibo.model.T2WUser) JSONObject(weibo4j.org.json.JSONObject) AccessToken(weibo4j.http.AccessToken) RequestToken(twitter4j.auth.RequestToken) HttpServletRouter(h2weibo.HttpServletRouter)

Example 3 with JSONObject

use of weibo4j.org.json.JSONObject in project twitter-2-weibo by rjyo.

the class InitServlet method createJedisPool.

public JedisPool createJedisPool() {
    JedisPool jedisPool;
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.testOnBorrow = true;
    config.testWhileIdle = true;
    config.maxActive = 25;
    config.maxIdle = 0;
    config.minIdle = 0;
    log.debug("Jedis pool created.");
    try {
        String services = System.getenv("VCAP_SERVICES");
        if (services != null) {
            JSONObject obj = new JSONObject(services);
            obj = obj.getJSONArray("redis-2.2").getJSONObject(0).getJSONObject("credentials");
            String hostname = obj.getString("hostname");
            int port = obj.getInt("port");
            String password = obj.getString("password");
            jedisPool = new JedisPool(config, hostname, port, 0, password);
        } else {
            jedisPool = new JedisPool(config, "localhost");
            log.info("Using localhost Redis server");
        }
        return jedisPool;
    } catch (JSONException e) {
        log.error("Failed to init", e);
        return null;
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) ServletConfig(javax.servlet.ServletConfig) JSONException(weibo4j.org.json.JSONException) JedisPool(redis.clients.jedis.JedisPool) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool)

Example 4 with JSONObject

use of weibo4j.org.json.JSONObject in project twitter-2-weibo by rjyo.

the class Oauth method ts.

/*
	 * 处理解析后的json解析
	 */
public String ts(String json) {
    try {
        JSONObject jsonObject = new JSONObject(json);
        access_token = jsonObject.getString("oauth_token");
        user_id = jsonObject.getString("user_id");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return access_token;
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONException(weibo4j.org.json.JSONException)

Example 5 with JSONObject

use of weibo4j.org.json.JSONObject in project twitter-2-weibo by rjyo.

the class Status method constructWapperStatus.

public static StatusWapper constructWapperStatus(Response res) throws WeiboException {
    //asJSONArray();
    JSONObject jsonStatus = res.asJSONObject();
    JSONArray statuses = null;
    try {
        if (!jsonStatus.isNull("statuses")) {
            statuses = jsonStatus.getJSONArray("statuses");
        }
        if (!jsonStatus.isNull("reposts")) {
            statuses = jsonStatus.getJSONArray("reposts");
        }
        int size = statuses.length();
        List<Status> status = new ArrayList<Status>(size);
        for (int i = 0; i < size; i++) {
            status.add(new Status(statuses.getJSONObject(i)));
        }
        long previousCursor = jsonStatus.getLong("previous_curosr");
        long nextCursor = jsonStatus.getLong("next_cursor");
        long totalNumber = jsonStatus.getLong("total_number");
        String hasvisible = jsonStatus.getString("hasvisible");
        return new StatusWapper(status, previousCursor, nextCursor, totalNumber, hasvisible);
    } catch (JSONException jsone) {
        throw new WeiboException(jsone);
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) JSONArray(weibo4j.org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(weibo4j.org.json.JSONException)

Aggregations

JSONObject (weibo4j.org.json.JSONObject)9 JSONException (weibo4j.org.json.JSONException)8 JSONArray (weibo4j.org.json.JSONArray)5 ArrayList (java.util.ArrayList)3 Date (java.util.Date)2 HttpServletRouter (h2weibo.HttpServletRouter)1 DBHelper (h2weibo.model.DBHelper)1 T2WUser (h2weibo.model.T2WUser)1 Iterator (java.util.Iterator)1 ServletConfig (javax.servlet.ServletConfig)1 HttpSession (javax.servlet.http.HttpSession)1 GenericObjectPool (org.apache.commons.pool.impl.GenericObjectPool)1 JedisPool (redis.clients.jedis.JedisPool)1 twitter4j (twitter4j)1 RequestToken (twitter4j.auth.RequestToken)1 Account (weibo4j.Account)1 Oauth (weibo4j.Oauth)1 Weibo (weibo4j.Weibo)1 AccessToken (weibo4j.http.AccessToken)1 WeiboException (weibo4j.model.WeiboException)1