public static void getDescriptorIDsByTagID()

in api-reference-examples/java/te-tag-query/com/facebook/threatexchange/Net.java [103:207]


  public static void getDescriptorIDsByTagID(
    String tagID,
    boolean verbose,
    boolean showURLs,
    String since, // maybe null
    String until, // maybe null
    int pageSize,
    boolean includeIndicatorInOutput,
    IDProcessor idProcessor
  ) {
    String pageLimit = Integer.toString(pageSize);
    String startURL = TE_BASE_URL
      + "/" + tagID + "/tagged_objects"
      + "/?access_token=" + APP_TOKEN
      + "&limit=" + pageLimit;
    if (since != null) {
      startURL += "&tagged_since=" + since;
    }
    if (until != null) {
      startURL += "&tagged_until=" + until;
    }

    String nextURL = startURL;

    int pageIndex = 0;
    do {
      if (showURLs) {
        System.out.println("URL:");
        System.out.println(nextURL);
      }
      try (InputStream response = new URL(nextURL).openConnection().getInputStream()) {

        // Format we're parsing:
        // {
        //   "data": [
        //     {
        //       "id": "9915337796604770",
        //       "type": "THREAT_DESCRIPTOR",
        //       "name": "7ef5...aa97"
        //     }
        //     ...
        //   ],
        //   "paging": {
        //     "cursors": {
        //       "before": "XYZIU...NjQ0h3Unh3",
        //       "after": "XYZIUk...FXNzVNd1Jn"
        //     },
        //     "next": "https://graph.facebook.com/v3.1/9999338387644295/tagged_objects?access_token=..."
        //   }
        // }

        JSONObject object = (JSONObject) new JSONParser().parse(new InputStreamReader(response));
        JSONArray data = (JSONArray) object.get("data");
        JSONObject paging = (JSONObject) object.get("paging");
        if (paging == null) {
          nextURL = null;
        } else {
          nextURL = (String) paging.get("next");
        }

        int numItems = data.size();

        List<String> ids = new ArrayList<String>();
        for (int i = 0; i < numItems; i++) {
          JSONObject item = (JSONObject) data.get(i);

          String itemID = (String) item.get("id");
          String itemType = (String) item.get("type");
          String itemText = (String) item.get("name");
          if (!itemType.equals(Constants.THREAT_DESCRIPTOR)) {
            continue;
          }

          if (verbose) {
            SimpleJSONWriter w = new SimpleJSONWriter();
            w.add("id", itemID);
            w.add("type", itemType);
            if (includeIndicatorInOutput) {
              w.add("indicator", itemText);
            }

            System.out.println(w.format());
            System.out.flush();
          }
          ids.add(itemID);
        }

        if (verbose) {
          SimpleJSONWriter w = new SimpleJSONWriter();
          w.add("page_index", pageIndex);
          w.add("num_items_pre_filter", numItems);
          w.add("num_items_post_filter", ids.size());
          System.out.println(w.format());
          System.out.flush();
        }

        idProcessor.processIDs(ids);

        pageIndex++;
      } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(1);
      }
    } while (nextURL != null);
  }