示例#1
0
  private void searchBingImages(String query, int offset, int count) {
    if (searching) {
      searching = false;
      requestQueue.cancelAll("search");
    }
    try {
      searching = true;
      String url;
      if (nextSearchBingString != null) {
        url = nextSearchBingString;
      } else {
        boolean adult;
        String phone = UserConfig.getCurrentUser().phone;
        adult =
            phone.startsWith("44")
                || phone.startsWith("49")
                || phone.startsWith("43")
                || phone.startsWith("31")
                || phone.startsWith("1");
        url =
            String.format(
                Locale.US,
                "https://api.datamarket.azure.com/Bing/Search/v1/Image?Query='%s'&$skip=%d&$top=%d&$format=json%s",
                URLEncoder.encode(query, "UTF-8"),
                offset,
                count,
                adult ? "" : "&Adult='Off'");
      }
      JsonObjectRequest jsonObjReq =
          new JsonObjectRequest(
              Request.Method.GET,
              url,
              null,
              new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                  nextSearchBingString = null;
                  try {
                    JSONObject d = response.getJSONObject("d");
                    JSONArray result = d.getJSONArray("results");
                    try {
                      nextSearchBingString = d.getString("__next");
                    } catch (Exception e) {
                      nextSearchBingString = null;
                      FileLog.e("tmessages", e);
                    }
                    for (int a = 0; a < result.length(); a++) {
                      try {
                        JSONObject object = result.getJSONObject(a);
                        String id = Utilities.MD5(object.getString("MediaUrl"));
                        if (searchResultKeys.containsKey(id)) {
                          continue;
                        }
                        MediaController.SearchImage bingImage = new MediaController.SearchImage();
                        bingImage.id = id;
                        bingImage.width = object.getInt("Width");
                        bingImage.height = object.getInt("Height");
                        bingImage.size = object.getInt("FileSize");
                        bingImage.imageUrl = object.getString("MediaUrl");
                        JSONObject thumbnail = object.getJSONObject("Thumbnail");
                        bingImage.thumbUrl = thumbnail.getString("MediaUrl");
                        searchResult.add(bingImage);
                        searchResultKeys.put(id, bingImage);
                      } catch (Exception e) {
                        FileLog.e("tmessages", e);
                      }
                    }
                  } catch (Exception e) {
                    FileLog.e("tmessages", e);
                  }
                  searching = false;
                  if (nextSearchBingString != null && !nextSearchBingString.contains("json")) {
                    nextSearchBingString += "&$format=json";
                  }
                  updateSearchInterface();
                }
              },
              new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                  FileLog.e("tmessages", "Error: " + error.getMessage());
                  nextSearchBingString = null;
                  searching = false;
                  updateSearchInterface();
                }
              }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
              Map<String, String> headers = new HashMap<>();
              String auth =
                  "Basic "
                      + Base64.encodeToString(
                          (BuildVars.BING_SEARCH_KEY + ":" + BuildVars.BING_SEARCH_KEY).getBytes(),
                          Base64.NO_WRAP);
              headers.put("Authorization", auth);
              return headers;
            }
          };
      jsonObjReq.setTag("search");
      requestQueue.add(jsonObjReq);
    } catch (Exception e) {
      FileLog.e("tmessages", e);
      nextSearchBingString = null;
      searching = false;
      updateSearchInterface();
    }
  }
示例#2
0
 private void searchGiphyImages(String query, int offset, final int count) {
   if (searching) {
     searching = false;
     requestQueue.cancelAll("search");
   }
   try {
     searching = true;
     String url =
         String.format(
             Locale.US,
             "https://api.giphy.com/v1/gifs/search?q=%s&offset=%d&limit=%d&api_key=141Wa2KDAfNfxu",
             URLEncoder.encode(query, "UTF-8"),
             offset,
             count);
     JsonObjectRequest jsonObjReq =
         new JsonObjectRequest(
             Request.Method.GET,
             url,
             null,
             new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                 try {
                   JSONArray result = response.getJSONArray("data");
                   try {
                     JSONObject pagination = response.getJSONObject("pagination");
                     int total_count = pagination.getInt("total_count");
                     giphySearchEndReached = searchResult.size() + result.length() >= total_count;
                   } catch (Exception e) {
                     FileLog.e("tmessages", e);
                   }
                   boolean added = false;
                   for (int a = 0; a < result.length(); a++) {
                     try {
                       JSONObject object = result.getJSONObject(a);
                       String id = object.getString("id");
                       if (searchResultKeys.containsKey(id)) {
                         continue;
                       }
                       added = true;
                       JSONObject images = object.getJSONObject("images");
                       JSONObject thumb = images.getJSONObject("downsized_still");
                       JSONObject original = images.getJSONObject("original");
                       MediaController.SearchImage bingImage = new MediaController.SearchImage();
                       bingImage.id = id;
                       bingImage.width = original.getInt("width");
                       bingImage.height = original.getInt("height");
                       bingImage.size = original.getInt("size");
                       bingImage.imageUrl = original.getString("url");
                       bingImage.thumbUrl = thumb.getString("url");
                       bingImage.type = 1;
                       searchResult.add(bingImage);
                       searchResultKeys.put(id, bingImage);
                     } catch (Exception e) {
                       FileLog.e("tmessages", e);
                     }
                   }
                   if (!added) {
                     giphySearchEndReached = true;
                   }
                 } catch (Exception e) {
                   FileLog.e("tmessages", e);
                 }
                 searching = false;
                 updateSearchInterface();
               }
             },
             new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                 FileLog.e("tmessages", "Error: " + error.getMessage());
                 giphySearchEndReached = true;
                 searching = false;
                 updateSearchInterface();
               }
             });
     jsonObjReq.setTag("search");
     requestQueue.add(jsonObjReq);
   } catch (Exception e) {
     FileLog.e("tmessages", e);
   }
 }