Example #1
1
  @Test
  public void accountPollsIncrease() {
    APICall apiCall =
        new APICall.Builder("getPolls")
            .param("includeVoters", "false")
            .param("account", Long.toUnsignedString(DAVE.getId()))
            .param("firstIndex", 0)
            .param("lastIndex", 100)
            .build();

    JSONObject jsonResponse = apiCall.invoke();
    Logger.logMessage("getPollsResponse:" + jsonResponse.toJSONString());
    JSONArray polls = (JSONArray) jsonResponse.get("polls");
    int initialSize = polls.size();

    APICall createPollApiCall =
        new TestCreatePoll.CreatePollBuilder().secretPhrase(DAVE.getSecretPhrase()).build();
    String poll = TestCreatePoll.issueCreatePoll(createPollApiCall, false);
    generateBlock();

    jsonResponse = apiCall.invoke();
    Logger.logMessage("getPollsResponse:" + jsonResponse.toJSONString());
    polls = (JSONArray) jsonResponse.get("polls");
    int size = polls.size();

    JSONObject lastPoll = (JSONObject) polls.get(0);
    Assert.assertEquals(poll, lastPoll.get("poll"));
    Assert.assertEquals(size, initialSize + 1);
  }
  public VineNotificationsResponse(JSONObject data, String baseURL) throws Exception {
    this.baseURL = baseURL;
    if (data != null) {
      nextPage = JSONUtil.getLong(data, "nextPage");
      previousPage = JSONUtil.getLong(data, "previousPage");
      size = JSONUtil.getLong(data, "size");
      anchor = JSONUtil.getLong(data, "anchor");
      count = JSONUtil.getLong(data, "count");

      JSONArray jsonNotifications = JSONUtil.getJSONArray(data, "records");
      notfications = new ArrayList<VineNotification>();
      if (jsonNotifications != null && jsonNotifications.size() > 0) {
        for (int i = 0; i < jsonNotifications.size(); i++) {
          try {
            JSONObject n = (JSONObject) jsonNotifications.get(i);
            if (n != null) {
              VineNotification notf = new VineNotification(n);
              notfications.add(notf);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
Example #3
0
  private void updateLastVersion() {
    if (!this.project_check_version) return;
    URL url = null;
    try {
      url = new URL(this.project_curse_url);
    } catch (Exception e) {
      this.log("Failed to create URL: " + this.project_curse_url);
      return;
    }

    try {
      URLConnection conn = url.openConnection();
      conn.addRequestProperty("X-API-Key", null);
      conn.addRequestProperty("User-Agent", this.project_name + " using FGUtilCore (by fromgate)");
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String response = reader.readLine();
      JSONArray array = (JSONArray) JSONValue.parse(response);
      if (array.size() > 0) {
        JSONObject latest = (JSONObject) array.get(array.size() - 1);
        String plugin_name = (String) latest.get("name");
        this.project_last_version = plugin_name.replace(this.project_name + " v", "").trim();
      }
    } catch (Throwable e) {
      this.log("Failed to check last version");
    }
  }
  /** ********* Client Unwrapping ********** */
  static ArrayList<LocWithCheckIns> unwrapRecentFriends(JSONArray jsonlocations) {
    JSONObject data;
    JSONArray jsonfriends;
    ArrayList<CheckIn> checkIns = new ArrayList<CheckIn>();
    ArrayList<LocWithCheckIns> locs = new ArrayList<LocWithCheckIns>();

    for (int i = 0; i < jsonlocations.size(); i++) {
      data = (JSONObject) jsonlocations.get(i);

      jsonfriends = (JSONArray) data.get("friends");

      /*for (int j = 0; j < jsonfriends.size(); j++)
          friends.add((CheckIn)jsonfriends.get(j));
      */

      for (int j = 0; j < jsonfriends.size(); j += 4) {
        CheckIn tempCI =
            new CheckIn(
                (String) jsonfriends.get(j),
                (String) jsonfriends.get(j + 1),
                (int) (long) (Long) jsonfriends.get(j + 2),
                (String) jsonfriends.get(j + 3));

        checkIns.add(tempCI);
      }

      ArrayList<CheckIn> temp = new ArrayList<CheckIn>(checkIns);

      locs.add(new LocWithCheckIns((String) data.get("location"), temp));

      checkIns.clear();
    }

    return locs;
  }
  static ArrayList<UserWithCheckIns> unwrapRecentLocs(JSONArray jsonfriendsWithLocs) {
    JSONObject data;
    JSONArray jsonlocs;
    ArrayList<CheckIn> checkIns = new ArrayList<CheckIn>();
    ArrayList<UserWithCheckIns> friends = new ArrayList<UserWithCheckIns>();

    for (int i = 0; i < jsonfriendsWithLocs.size(); i++) {
      data = (JSONObject) jsonfriendsWithLocs.get(i);

      jsonlocs = (JSONArray) data.get("locations");

      for (int j = 0; j < jsonlocs.size(); j += 4) {
        // locs.add((CheckIn)jsonlocs.get(j));
        CheckIn tempCI =
            new CheckIn(
                (String) jsonlocs.get(j),
                (String) jsonlocs.get(j + 1),
                (int) (long) (Long) jsonlocs.get(j + 2),
                (String) jsonlocs.get(j + 3));

        checkIns.add(tempCI);
      }

      ArrayList<CheckIn> temp = new ArrayList<CheckIn>(checkIns);

      friends.add(new UserWithCheckIns((String) data.get("username"), temp));

      checkIns.clear();
    }

    return friends;
  }
Example #6
0
 private static void fetchMulitpleOutlinkMappings(
     HashMap<String, Set<String>> outlinkmapping,
     JSONArray arr,
     HashMap<String, JSONArray> inlinkmapping,
     Set<String> crawledDocuments)
     throws FileNotFoundException {
   // HashMap<String, Set<String>> outlinkmapping = new HashMap<String, Set<String>>();
   for (int i = 0; i < arr.size(); i++) {
     Set<String> urlset = new HashSet<String>();
     // System.out.println(arr.get(i));
     try {
       JSONObject obj = (JSONObject) arr.get(i);
       String id = (String) obj.get("_id");
       // System.out.println(id);
       JSONArray outlinks = new JSONArray();
       if (obj.containsKey("fields") && ((JSONObject) obj.get("fields")).containsKey("out_links"))
         outlinks = (JSONArray) ((JSONObject) obj.get("fields")).get("out_links");
       // System.out.println(outlinks);
       for (int j = 0; j < outlinks.size(); j++) {
         try {
           if (crawledDocuments.contains((String) outlinks.get(j)))
             urlset.add((String) outlinks.get(j));
           // System.out.println("done");
         } catch (Exception e) {
           System.out.println("Did not get the outlink " + e.toString());
         }
       }
       outlinkmapping.put(id, urlset);
       // System.out.println("done");
     } catch (Exception e) {
       System.out.println("Did not get the outlink " + e.toString());
     }
   }
 }
	public Boolean addFeedUser(String sessionKey,String token){
		RenrenApiClient apiClient = RenrenApiClient.getInstance();
		JSONArray feedInfo = new JSONArray();
		String renrenUserId;
		try {
			
			renrenUserId = String.valueOf(apiClient.getUserService().getLoggedInUser(new AccessToken(sessionKey)));
		} catch (Exception err){
			err.printStackTrace();
			return false;
		}
		try {
			feedInfo = apiClient.getFeedService().getFeed("10", Integer.parseInt(renrenUserId), 1, 30,new AccessToken(sessionKey)); //可能拿不到
		} catch (Exception err){
			err.printStackTrace();
			return false;
		}
		Set<String>	messages = new HashSet<String>();
		if (feedInfo != null && feedInfo.size()>0) {
			for (int i=0;i<feedInfo.size();i++)
			{
				JSONObject currentFeed = (JSONObject) feedInfo.get(i);
				assert (currentFeed != null);
				String message = (String) currentFeed.get("message");
				messages.add(message);		
			}
		}
		UserSpace t = new UserSpace(sessionKey,renrenUserId,messages,token);
		if (!up.offer(t)) return false;
		ApiInitListener.User.put(config.properties.irt.getUserIdByToken(token),t);
		
		return true;
	}
Example #8
0
  public JSONObject filterPlacesRandomly(
      JSONObject placesJSONObject, int finalPlacesNumberPerRequest, List<String> place_ids) {
    JSONObject initialJSON = (JSONObject) placesJSONObject.clone();
    JSONObject finalJSONObject = new JSONObject();
    int numberFields = placesJSONObject.size();
    int remainingPlaces = finalPlacesNumberPerRequest;
    int keywordsProcessed = 0;

    Set<String> keywords = initialJSON.keySet();

    for (String keyword : keywords) {
      JSONArray placesForKeyword = (JSONArray) initialJSON.get(keyword);
      JSONArray finalPlacesForKeyword = new JSONArray();
      int placesForKeywordSize = placesForKeyword.size();

      int maxPlacesToKeepInFinalJSON = remainingPlaces / (numberFields - keywordsProcessed);
      int placesToKeepInFinalJSON = Math.min(maxPlacesToKeepInFinalJSON, placesForKeywordSize);
      remainingPlaces -= placesToKeepInFinalJSON;

      for (int i = 0; i < placesToKeepInFinalJSON; i++) {
        int remainingPlacesInJSONArray = placesForKeyword.size();
        int randomElementPosInArray = (new Random()).nextInt(remainingPlacesInJSONArray);
        JSONObject temp = (JSONObject) placesForKeyword.remove(randomElementPosInArray);
        place_ids.add((String) temp.get("place_id"));
        finalPlacesForKeyword.add(temp);
      }

      finalJSONObject.put(keyword, finalPlacesForKeyword);
      keywordsProcessed++;
    }

    return finalJSONObject;
  }
Example #9
0
  private boolean read() {
    try {
      final URLConnection conn = this.url.openConnection();
      conn.setConnectTimeout(5000);

      if (this.apiKey != null) {
        conn.addRequestProperty("X-API-Key", this.apiKey);
      }
      conn.addRequestProperty("User-Agent", "Updater (by Gravity)");

      conn.setDoOutput(true);

      final BufferedReader reader =
          new BufferedReader(new InputStreamReader(conn.getInputStream()));
      final String response = reader.readLine();

      final JSONArray array = (JSONArray) JSONValue.parse(response);

      if (array.size() == 0) {
        this.plugin
            .getLogger()
            .warning("The updater could not find any files for the project id " + this.id);
        this.result = UpdateResult.FAIL_BADID;
        return false;
      }

      this.versionName =
          (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE);
      this.versionLink =
          (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE);
      this.versionType =
          (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE);
      this.versionGameVersion =
          (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.VERSION_VALUE);

      return true;
    } catch (final IOException e) {
      if (e.getMessage().contains("HTTP response code: 403")) {
        this.plugin
            .getLogger()
            .warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
        this.plugin
            .getLogger()
            .warning("Please double-check your configuration to ensure it is correct.");
        this.result = UpdateResult.FAIL_APIKEY;
      } else {
        this.plugin
            .getLogger()
            .warning("The updater could not contact dev.bukkit.org for updating.");
        this.plugin
            .getLogger()
            .warning(
                "If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
        this.result = UpdateResult.FAIL_DBO;
      }
      e.printStackTrace();
      return false;
    }
  }
 /**
  * find the smells related to one file, and update file object
  *
  * @param issue
  * @param filename
  */
 public static void addSmell2File(JSONObject issue, JSONObject file, String dir) {
   String version = (String) issue.get("affect");
   if (version == null || version.equals("")) {
     return;
   }
   version = StringUtil.formatIssueVersion(version);
   String extractedName = StringUtil.dir2pkg(dir);
   for (int i = 0; i < smells.size(); i++) {
     JSONObject smell = (JSONObject) smells.get(i);
     if (version.equals(smell.get("version"))) {
       JSONArray smellsPerVersion = (JSONArray) smell.get("smells");
       for (int j = 0; j < smellsPerVersion.size(); j++) {
         JSONObject smellPerVersion = (JSONObject) smellsPerVersion.get(j);
         String classname = (String) smellPerVersion.get("classname");
         // found the smells related to particular file
         if (classname.contains(extractedName)) {
           // add the smells to this file!!
           JSONObject tmpSmell = null;
           if (file.containsKey("smells")) {
             tmpSmell = (JSONObject) file.get("smells");
             int tmpSmellNum = Integer.parseInt(smellPerVersion.get("bco").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Concern_Overload", tmpSmellNum);
             }
             // exclude dependency cycle for now
             // tmpSmell.put("bdc", smellPerVersion.get("bdc"));
             tmpSmellNum = Integer.parseInt(smellPerVersion.get("buo").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Link_Overload", tmpSmellNum);
             }
             tmpSmellNum = Integer.parseInt(smellPerVersion.get("spf").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Scattered_Parasitic_Functionality", tmpSmellNum);
             }
           } else {
             tmpSmell = new JSONObject();
             int tmpSmellNum = Integer.parseInt(smellPerVersion.get("bco").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Concern_Overload", tmpSmellNum);
             }
             // exclude dependency cycle for now
             // tmpSmell.put("bdc", smellPerVersion.get("bdc"));
             tmpSmellNum = Integer.parseInt(smellPerVersion.get("buo").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Link_Overload", tmpSmellNum);
             }
             tmpSmellNum = Integer.parseInt(smellPerVersion.get("spf").toString());
             if (tmpSmellNum != 0) {
               tmpSmell.put("Scattered_Parasitic_Functionality", tmpSmellNum);
             }
             file.put("smells", tmpSmell);
           }
         }
       }
       return;
     }
   }
 }
Example #11
0
 protected double[] ParseJSONDoubleArray(JSONArray a) {
   if (a == null) return null;
   double out[] = new double[a.size()];
   for (int i = 0; i < a.size(); ++i) {
     try {
       out[i] = (Double) a.get(i);
     } catch (ClassCastException e) {
       out[i] = ((Long) a.get(i)).doubleValue();
     }
   }
   return out;
 }
Example #12
0
  public static String[] findSimilar(String idToGetRelated) {
    String IDofRelated = "";
    OkHttpClient client = new OkHttpClient();

    Request request =
        new Request.Builder()
            .url(
                "https://hackingedu.chegg.com/hacking-edu/catalog/relateditem/priced/getRelatedItems?catalogItemId="
                    + idToGetRelated)
            .get()
            .addHeader("accept", "application/json")
            .addHeader("content-type", "application/json")
            .addHeader(
                "authorization",
                "Basic TXlPYUhWOUFtN01QdmxuSmJVVVV2SXZyaVBnYmVlQUE6MG1zQVFWbk1wemNS MXNjNA==")
            .addHeader("cache-control", "no-cache")
            // .addHeader("postman-token", "2b21d3c9-023b-519e-4681-a7e20281879e")
            .build();

    Response response = null;

    try {
      response = client.newCall(request).execute();
      // System.out.println(response.toString());
    } catch (Exception e) {
      System.out.println(e);
    }
    String myResponse = "";
    try {
      myResponse = response.body().string();
    } catch (Exception e) {
      System.out.println(e);
    }
    // System.out.println("MY RESPONSE " + myResponse);

    /*JSONObject jObject = new JSONObject(new String(myResponse));
    JSONArray jArray = jObject.getJSONArray("result");*/

    Object obj = JSONValue.parse(myResponse);
    JSONArray array = (JSONArray) obj;
    // System.out.println("ARRAY: " + array.toString());
    String[] IDlist = new String[array.size()];
    for (int i = 0; i < array.size(); i++) {
      IDlist[i] = (String) ((JSONObject) array.get(i)).get("catalogItemIdTo");
    }
    // IDofRelated = myResponse;

    // String [] IDlist = new String [response.result.length];
    //   for(int i=0; i<response.result.length; i++){
    //     IDlist [i] = response.result[i].responseContent.docs[0].id;
    //   }
    return IDlist;
  }
 private JSONObject getUserMarkFromPhoto(String userId, String photoId) {
   JSONArray marksArray = getAllMarksFromPhoto(photoId);
   if (marksArray != null && marksArray.size() != 0) {
     for (int i = 0; i < marksArray.size(); i++) {
       JSONObject tag = (JSONObject) marksArray.get(i);
       if (tag.get("id").equals(userId)) {
         tag.put("marksNum", marksArray.size());
         return tag;
       }
     }
   }
   return null;
 }
Example #14
0
  /**
   * Returns a Set of tags from a given JSON data array
   *
   * @param data_array JSONArray containing the set of data per response
   * @return Set<String> containing the collection of tags
   */
  public Set<String> getTagSet(JSONArray data_array) throws IOException {
    Set<String> tag_set = new HashSet<String>();
    JSONObject temp;
    JSONArray tag_array;

    for (int i = 0; i < data_array.size(); i++) {
      temp = (JSONObject) data_array.get(i);
      tag_array = (JSONArray) temp.get("tags");

      for (int j = 0; j < tag_array.size(); j++) {
        tag_set.add((String) tag_array.get(j));
      }
    }

    return tag_set;
  }
  public String validateUpdatedData(String result) {
    // System.out.println("RESULT------------------>" + result);
    String status = "0";
    try {
      JSONParser parser = new JSONParser();
      Object jsonObject = parser.parse(result);

      JSONArray jsonArray = (JSONArray) jsonObject;

      System.out.println("Bookmark SIZEEEEE" + jsonArray.size());

      JSONObject jsonObjected = (JSONObject) parser.parse(jsonArray.get(0).toString());

      objTickerNewsBE.setNewsId(jsonObjected.get("id").toString());
      objTickerNewsBE.setImage(jsonObjected.get("image").toString());
      objTickerNewsBE.setContent(jsonObjected.get("content").toString());
      objTickerNewsBE.setSource(jsonObjected.get("publisher").toString());
      objTickerNewsBE.setDate(jsonObjected.get("date").toString());
      objTickerNewsBE.setTag(jsonObjected.get("tag").toString());
      objTickerNewsBE.setTitle(jsonObjected.get("title").toString());
      objTickerNewsBE.setUrl(jsonObjected.get("link").toString());

    } catch (Exception e) {
      e.printStackTrace();
    }
    return status;
  }
Example #16
0
  public Object sendSingleRequest(String category, String action, JSONObject parameters) {
    // If the connection is busy, sleep 1ms at a time until it isn't
    synchronized (this) {

      //		}(busy)
      //			try {
      //				Thread.sleep(1);
      //			} catch (InterruptedException e) {
      //				// TODO Auto-generated catch block
      //				e.printStackTrace();
      //			}
      //
      //		busy = true;
      this.addRequest(category, action, parameters);
      //		if (!(new String(category).equals("Chat") ||
      //				new String(action).equals("getLineChanges") ||
      //				new String(action).equals("getLineLocks"))){
      //			System.out.println(category.toString() + action.toString() + parameters.toString());
      //		}

      JSONArray fullResponse = (JSONArray) this.sendRequestBuffer();
      busy = false;

      Object out = null;
      if (fullResponse != null && fullResponse.size() > 0) {
        JSONObject singleResponse = (JSONObject) fullResponse.get(0);
        return (Object) singleResponse.get("result");
      }
    }
    return null;
  }
 @Test
 public void testGetMovie() {
   JSONArray json = client.getMovie(SIN_CITY_MOVIE_ID);
   assertNotNull(json);
   assertEquals(1, json.size());
   assertEquals("Sin City", ((JSONObject) json.get(0)).get("name"));
 }
  public void parseJSONFeedback(String JSON) {
    String answer, patientID;
    int questionID, questionnaire;
    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(JSON);
      JSONArray array = (JSONArray) obj;
      for (int i = 0; i < array.size(); i++) {
        JSONObject everyQuestion = (JSONObject) array.get(i);

        answer = everyQuestion.get("Answer").toString();
        questionID = Integer.parseInt(everyQuestion.get("QuestionID").toString());
        patientID = everyQuestion.get("PatientID").toString();
        questionnaire = Integer.parseInt(everyQuestion.get("QuestionnaireID").toString());
        FeedbackFunctionality addToTable =
            new FeedbackFunctionality(questionnaire, patientID, questionID, answer);
        try {
          addToTable.addfeedback();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
    } catch (ParseException pe) {
    }
  }
  /*
   * [{"IDpatient": "12-09-1000.Bayoumy","firstname": "Mohamed","lastname": "Bayoumy","dateofbirth": "12-09-1000"},
   * {"IDpatient": "12-02-1500.Omran","firstname": "Ahmed","lastname": "Omran","dateofbirth": "12-02-1500"}]
   *
   */
  public void parseJSONPatient(String JSON) {
    String IDpatient, firstname, lastname, dateofbirth;
    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(JSON);
      JSONArray array = (JSONArray) obj;
      for (int i = 0; i < array.size(); i++) {
        JSONObject everyQuestion = (JSONObject) array.get(i);

        IDpatient = everyQuestion.get("IDPatient").toString();
        firstname = everyQuestion.get("firstname").toString();
        lastname = everyQuestion.get("lastname").toString();
        dateofbirth = everyQuestion.get("dateofbirth").toString();

        FeedbackFunctionality addToTable =
            new FeedbackFunctionality(IDpatient, firstname, lastname, dateofbirth);
        try {
          addToTable.addpatients();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
    } catch (ParseException pe) {
    }
  }
Example #20
0
  public String printNiceByPost(int pageNum, JSONArray data_array) {
    String result = "";
    JSONObject current;

    for (int i = 0; i < data_array.size(); i++) {
      current = (JSONObject) data_array.get(i);

      // get user name
      JSONObject user = (JSONObject) current.get("user");
      String userName = (String) user.get("username");

      // get location
      JSONObject loc = (JSONObject) current.get("location");
      String locName = "";
      Double locLatitude;
      Double locLongitude;
      if (loc != null) {
        locName = (String) loc.get("name");
        locLongitude = (Double) loc.get("longitude");
        locLatitude = (Double) loc.get("latitude");
      } else {
        continue;
      }

      result += String.format("----Post %d, %d-----\n", pageNum, i + 1);
      result += String.format("username = %s\n", userName);
      result += String.format("location = %s\n", locName);
      result += String.format("longitude = %f\n", locLongitude);
      result += String.format("latitude = %f\n\n", locLatitude);
    }

    return result;
  }
Example #21
0
  private static HashMap<String, Boolean> fetchTopDocumentsFromFile(
      String location, String fileext, String query) throws FileNotFoundException, IOException {

    HashMap<String, Boolean> validurls = new HashMap<String, Boolean>();
    int count = 0;
    File[] files = new File(location).listFiles();
    for (int i = 0; i < files.length; i++) {
      if (!files[i].isFile() || !files[i].getName().endsWith(".json")) continue;
      System.out.println(files[i]);
      JSONParser parser = new JSONParser();
      Object obj = null;
      try {
        obj = parser.parse(new FileReader(files[i]));
        // System.out.println(obj);
      } catch (ParseException e1) {
        System.out.println("Could not parse the file " + files[i] + " as json");
      }
      JSONObject json = null;
      try {
        json = (JSONObject) obj;
        JSONArray arr = (JSONArray) ((JSONObject) json.get("hits")).get("hits");
        for (int j = 0; j < arr.size(); j++) {
          if (count == 1000) break;
          JSONObject o = (JSONObject) arr.get(j);
          validurls.put((String) o.get("_id"), true);
          count++;
        }
      } catch (Exception e) {
        System.out.println("json object could not be created " + e.toString());
      }
      if (count == 1000) break;
    }
    return validurls;
  }
 /* Returns List of Wikipedia page-ids of pages the string 'anchor' points to in Wikipedia */
 public List<Long> getPages(String anchor) {
   db.requestStart();
   List<Long> PageCollection = new ArrayList<Long>();
   BasicDBObject query = new BasicDBObject();
   query.put("anchor", anchor);
   BasicDBObject fields = new BasicDBObject("pages", true).append("_id", false);
   DBObject obj =
       table.findOne(query, fields); // System.out.println("num of results = "+curs.count());
   if (obj != null) {
     JSONParser jp = new JSONParser();
     JSONArray jarr = null;
     try {
       jarr = (JSONArray) jp.parse(obj.get("pages").toString());
     } catch (ParseException e) {
       jarr = new JSONArray();
     }
     // System.out.println("Link Freq = "+o.get("anchPageFreq").toString());
     for (int i = 0; i < jarr.size(); i++) {
       JSONObject objects = (JSONObject) jarr.get(i);
       PageCollection.add((long) (objects.get("page_id")));
     }
   }
   db.requestDone();
   return PageCollection;
 } // End getPages()
  private void loadHistory() {
    StringBuilder jsonArrayString = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_PERSISTENCE_FILE))) {
      while (reader.ready()) {
        jsonArrayString.append(reader.readLine());
      }
    } catch (IOException e) {
      logger.error("Could not parse message.", e);
    }

    JSONArray jsonArray = new JSONArray();

    if (jsonArrayString.length() == 0) {
      return;
    }

    try {
      jsonArray = (JSONArray) MessageHelper.getJsonParser().parse(jsonArrayString.toString());
    } catch (ParseException e) {
      logger.error("Could not parse message.", e);
    }

    for (int i = 0; i < jsonArray.size(); i++) {
      JSONObject jsonObject = (JSONObject) jsonArray.get(i);
      Message message = MessageHelper.jsonObjectToMessage(jsonObject);
      messages.add(message);
    }
  }
 /* Returns map of Wikipedia page-ids to number of inlinks to those pages. Page ids are pages the string 'anchor' points to in Wikipedia */
 public Map<Long, Integer> getPagesMap(String anchor) {
   db.requestStart();
   Map<Long, Integer> PageCollection = new HashMap<Long, Integer>();
   BasicDBObject query = new BasicDBObject();
   query.put("anchor", anchor);
   BasicDBObject fields =
       new BasicDBObject("page_id", true)
           .append("pages", true)
           .append("page_freq", true)
           .append("anchor_freq", true)
           .append("_id", false);
   DBObject ans =
       table.findOne(query, fields); // System.out.println("num of results = "+curs.count());
   db.requestDone();
   if (ans != null) {
     JSONParser jp = new JSONParser();
     JSONArray jo = null;
     try { // System.out.println(ans.get("pages"));
       jo = (JSONArray) jp.parse(ans.get("pages").toString());
     } catch (ParseException e) {
       e.printStackTrace();
     } // System.out.println("Link Freq = "+o.get("anchPageFreq").toString());
     for (int i = 0; i < jo.size(); i++) {
       JSONObject object = (JSONObject) jo.get(i);
       Long pId = (long) (object.get("page_id"));
       Long pValue0 = (long) object.get("page_freq");
       int pValue = pValue0.intValue();
       if (PageCollection.containsKey(pId)) {
         pValue = PageCollection.get(pId) + pValue;
       }
       PageCollection.put(pId, pValue);
     }
   }
   return PageCollection;
 } // End getPagesMap()
 @Override
 public void run() {
   try {
     try {
       Peer peer = Peers.getAnyPeer(Peer.State.CONNECTED, true);
       if (peer == null) {
         return;
       }
       JSONObject response = peer.send(getUnconfirmedTransactionsRequest);
       if (response == null) {
         return;
       }
       JSONArray transactionsData = (JSONArray) response.get("unconfirmedTransactions");
       if (transactionsData == null || transactionsData.size() == 0) {
         return;
       }
       processPeerTransactions(transactionsData, false);
     } catch (Exception e) {
       Logger.logDebugMessage("Error processing unconfirmed transactions from peer", e);
     }
   } catch (Throwable t) {
     Logger.logMessage("CRITICAL ERROR. PLEASE REPORT TO THE DEVELOPERS.\n" + t.toString());
     t.printStackTrace();
     System.exit(1);
   }
 }
Example #26
0
  private void loadMappings() {
    try {
      if (!Configuration.OFFLINE_MODE) {
        try {
          fetch(Configuration.versionfile);
          fetch(Configuration.jsonfile);
        } catch (final IOException ignored) {
          System.err.println(
              "Failed to downloaded version file and hooks. Attempting to run without latest hooks.");
        }
      }

      System.out.println(Configuration.jsonfile);
      BufferedInputStream in =
          new BufferedInputStream(new FileInputStream(getLocalInsertionsFile()));
      GzipCompressorInputStream gzip = new GzipCompressorInputStream(in);

      JSONParser parser = new JSONParser();
      JSONArray classes = (JSONArray) parser.parse(new BufferedReader(new InputStreamReader(gzip)));
      for (int i = 0; i < classes.size(); i++) {
        JSONObject classEntry = (JSONObject) classes.get(i);
        String name = (String) classEntry.get("name");
        mappings.put(name, classEntry);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #27
0
  @Test
  public void testImport() throws Exception {
    MemoryDatabase memDb = new MemoryDatabase("sample");
    memDb.start();

    final String jsonFileName = "/sample.json";
    memDb.importJSON(getClass(), jsonFileName);

    JSONParser jsonParser = new JSONParser();
    JSONArray tables =
        (JSONArray)
            jsonParser.parse(new InputStreamReader(getClass().getResourceAsStream(jsonFileName)));
    assertEquals(2, tables.size());

    Connection conn = memDb.getConnection();
    Statement stmt = conn.createStatement();

    JSONObject employee = (JSONObject) tables.get(0);
    ResultSet rs = stmt.executeQuery("SELECT * FROM \"employee\"");
    verifyTableEquals(employee, rs);
    rs.close();

    JSONObject team = (JSONObject) tables.get(1);
    rs = stmt.executeQuery("SELECT * FROM \"team\"");
    verifyTableEquals(team, rs);
    rs.close();

    stmt.close();
    conn.close();

    memDb.stop();
  }
Example #28
0
  public int[] parser(String json) {
    int[] data = null;
    JSONParser parser = new JSONParser();
    try {
      JSONArray jsow = (JSONArray) parser.parse(json);
      //			JSONArray jsow = (JSONArray)job;
      Iterator<String> it = jsow.iterator();
      data = new int[jsow.size()];
      int i = 0;
      //			System.out.println("jsow: "+jsow);
      while (it.hasNext()) {
        String tg = it.next();

        if (tg.contains(":")) {
          String[] arrtg = tg.split(":");
          data[i] = Integer.parseInt(arrtg[0]) * 3600 + Integer.parseInt(arrtg[1]) * 60;
        } else {
          // System.out.println("weight");
          data[i] = Integer.parseInt(tg);
        }
        // System.out.println(data[i]);
        i++;
      }
    } catch (Exception e) {
      System.err.println("parse1: " + e.toString());
    }

    return data;
  }
Example #29
0
  @RequestMapping(value = "/home")
  public String home(ModelMap modelMap) {
    HttpServletRequest request =
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    System.out.println("HomeController: passing through...");
    System.out.println("获取相关renren参数");
    String sessionKey = request.getParameter("xn_sig_session_key");
    String renrenUserId = request.getParameter("xn_sig_user");
    System.out.println("Session Key: " + sessionKey);
    System.out.println("renrenUserId: " + renrenUserId);
    if (sessionKey != null && renrenUserId != null) {
      RenrenApiClient apiClient = new RenrenApiClient(sessionKey);
      SessionKey sessionKeyReal = new SessionKey(sessionKey);
      // 获取用户信息
      JSONArray userInfo = apiClient.getUserService().getInfo(renrenUserId, sessionKeyReal);
      if (userInfo != null && userInfo.size() > 0) {
        JSONObject currentUser = (JSONObject) userInfo.get(0);
        if (currentUser != null) {
          String userName = (String) currentUser.get("name");
          String userHead = (String) currentUser.get("headurl");
          request.setAttribute("userName", userName);
          request.setAttribute("userHead", userHead);
        }
      }
      JSONArray friends = apiClient.getFriendsService().getFriends(2, 80, sessionKeyReal);
      String friendsStr = friends.toJSONString().replace("\"", "\'");
      request.setAttribute("friends", friendsStr);
    }

    // 测试commit

    modelMap.addAttribute("welcome", "Walter's APP");
    return "home";
  }
Example #30
0
  @Override
  public void loadPage(Map<String, List<String>> parameterHolder) throws KettlePageException {
    try {
      JSONArray fields = XMLHandler.getPageRows(parameterHolder, fid("fields"));
      int nrfields = fields.size();

      allocate(nrfields);

      for (int i = 0; i < nrfields; i++) {
        JSONObject field = (JSONObject) fields.get(i);

        fieldName[i] = (String) field.get("fieldName");
        fieldType[i] = (String) field.get("fieldType");
        fieldFormat[i] = (String) field.get("fieldFormat");
        currency[i] = (String) field.get("currency");
        decimal[i] = (String) field.get("decimal");
        group[i] = (String) field.get("group");
        value[i] = (String) field.get("value");

        fieldLength[i] = Const.toInt(String.valueOf(field.get("fieldLength")), -1);
        fieldPrecision[i] = Const.toInt(String.valueOf(field.get("fieldPrecision")), -1);
        setEmptyString[i] = Const.toBoolean(String.valueOf(field.get("setEmptyString")), false);
      }

      rowLimit = XMLHandler.getPageValue(parameterHolder, fid("limit"));
    } catch (Exception e) {
      throw new KettlePageException(
          "Unexpected error reading step information from the repository", e);
    }
  }