Guest User

Untitled

a guest
Jul 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package org.sorausagi.lingr;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.junit.Test;
  6. import org.sorausagi.lingr.LingrUtil.LingrSession;
  7.  
  8. public class LingrTest {
  9.  
  10. private static final String USER = "";
  11. private static final String PASSWORD = "";
  12.  
  13. @Test
  14. public void start() throws IOException {
  15. LingrSession session = createSessionAndGetRooms(USER, PASSWORD);
  16. System.out.println("\"lingrSession\":" + session);
  17. String counter = findKeyAndGetValue(LingrUtil.subscribe(session),
  18. "counter");
  19. System.out.println("------counter=" + counter);
  20. String observe = LingrUtil.observe(session, counter); // blockされる
  21. System.out.println(observe);
  22.  
  23. LingrUtil.post(session, "SoraUsagi", "受け取った!" + observe);
  24. }
  25.  
  26. public static LingrSession createSessionAndGetRooms(String user,
  27. String password) throws IOException {
  28. String response1 = LingrUtil
  29. .createSessionAndGetResponse(user, password);
  30. if (response1.contains("\"status\":\"error\"")) {
  31. throw new RuntimeException(response1);
  32. }
  33. String session = findKeyAndGetValue(response1, "session");
  34. String publicId = findKeyAndGetValue(response1, "public_id");
  35. String nickname = findKeyAndGetValue(response1, "nickname");
  36. String response2 = LingrUtil.getRooms(session);
  37. String[] rooms = findKeyAndGetValues(response2, "rooms");
  38. return new LingrSession(session, publicId, nickname, rooms);
  39. }
  40.  
  41. LingrSession state;
  42.  
  43. static String[] findKeyAndGetValues(String string, String key) {
  44. int indexOf1 = string.indexOf("\"" + key + "\":[");
  45. int indexOf2 = string.indexOf(']', indexOf1);
  46. String[] _rooms = string.substring(
  47. indexOf1 + key.length() + 2/* two quote */+ 1/* semicolon */
  48. + 1/* open square */, indexOf2 - 1/* close square */)
  49. .split("\\,");
  50. String[] rooms = new String[_rooms.length];
  51. for (int i = 0; i < _rooms.length; i++) {
  52. rooms[i] = _rooms[i].replaceAll("\"", "");
  53. }
  54. return rooms;
  55. }
  56.  
  57. static String findKeyAndGetValue(String string, String key) {
  58. int indexOf1 = string.indexOf("\"" + key + "\"");
  59. int indexOf2 = string.indexOf(',', indexOf1);
  60. if (indexOf2 == -1) {
  61. indexOf2 = string.indexOf('}', indexOf1);
  62. ;
  63. }
  64. String value = string.substring(indexOf1 + key.length() + 3/*two quotes and semicolon*/, indexOf2);
  65. return value.replaceAll("\"", "");
  66. }
  67. }
Add Comment
Please, Sign In to add comment