Search in sources :

Example 1 with ApiResult

use of org.mediawiki.api.ApiResult in project apps-android-commons by commons-app.

the class MediaDataExtractor method fetch.

/**
     * Actually fetch the data over the network.
     * todo: use local caching?
     *
     * Warning: synchronous i/o, call on a background thread
     */
public void fetch() throws IOException {
    if (fetched) {
        throw new IllegalStateException("Tried to call MediaDataExtractor.fetch() again.");
    }
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result = api.action("query").param("prop", "revisions").param("titles", filename).param("rvprop", "content").param("rvlimit", 1).param("rvgeneratexml", 1).get();
    processResult(result);
    fetched = true;
}
Also used : ApiResult(org.mediawiki.api.ApiResult)

Example 2 with ApiResult

use of org.mediawiki.api.ApiResult in project apps-android-commons by commons-app.

the class MethodAUpdater method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    //otherwise if user has typed something in that isn't in cache, search API for matching categories
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<String> categories = new ArrayList<>();
    //URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
    try {
        result = api.action("query").param("format", "xml").param("list", "search").param("srwhat", "text").param("srnamespace", "14").param("srlimit", catFragment.SEARCH_CATS_LIMIT).param("srsearch", filter).get();
        Timber.d("Method A URL filter %s", result);
    } catch (IOException e) {
        Timber.e(e, "IO Exception: ");
        //Return empty arraylist
        return categories;
    }
    ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
    for (ApiResult categoryNode : categoryNodes) {
        String cat = categoryNode.getDocument().getTextContent();
        String catString = cat.replace("Category:", "");
        categories.add(catString);
    }
    Timber.d("Found categories from Method A search, waiting for filter");
    return new ArrayList<>(filterYears(categories));
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with ApiResult

use of org.mediawiki.api.ApiResult in project apps-android-commons by commons-app.

the class PrefixUpdater method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    //If user hasn't typed anything in yet, get GPS and recent items
    if (TextUtils.isEmpty(filter)) {
        ArrayList<String> mergedItems = new ArrayList<>(catFragment.mergeItems());
        Timber.d("Merged items, waiting for filter");
        return new ArrayList<>(filterYears(mergedItems));
    }
    //if user types in something that is in cache, return cached category
    if (catFragment.categoriesCache.containsKey(filter)) {
        ArrayList<String> cachedItems = new ArrayList<>(catFragment.categoriesCache.get(filter));
        Timber.d("Found cache items, waiting for filter");
        return new ArrayList<>(filterYears(cachedItems));
    }
    //otherwise if user has typed something in that isn't in cache, search API for matching categories
    //URL: https://commons.wikimedia.org/w/api.php?action=query&list=allcategories&acprefix=filter&aclimit=25
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<String> categories = new ArrayList<>();
    try {
        result = api.action("query").param("list", "allcategories").param("acprefix", filter).param("aclimit", catFragment.SEARCH_CATS_LIMIT).get();
        Timber.d("Prefix URL filter %s", result);
    } catch (IOException e) {
        Timber.e(e, "IO Exception: ");
        //Return empty arraylist
        return categories;
    }
    ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/allcategories/c");
    for (ApiResult categoryNode : categoryNodes) {
        categories.add(categoryNode.getDocument().getTextContent());
    }
    Timber.d("Found categories from Prefix search, waiting for filter");
    return new ArrayList<>(filterYears(categories));
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 4 with ApiResult

use of org.mediawiki.api.ApiResult in project apps-android-commons by commons-app.

the class TitleCategories method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<String> items = new ArrayList<>();
    //URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
    try {
        result = api.action("query").param("format", "xml").param("list", "search").param("srwhat", "text").param("srnamespace", "14").param("srlimit", SEARCH_CATS_LIMIT).param("srsearch", title).get();
        Timber.d("Searching for cats for title: %s", result);
    } catch (IOException e) {
        Timber.e(e, "IO Exception: ");
        //Return empty arraylist
        return items;
    }
    ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
    for (ApiResult categoryNode : categoryNodes) {
        String cat = categoryNode.getDocument().getTextContent();
        String catString = cat.replace("Category:", "");
        items.add(catString);
    }
    Timber.d("Title cat query results: %s", items);
    return items;
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with ApiResult

use of org.mediawiki.api.ApiResult in project apps-android-commons by commons-app.

the class MediaThumbnailFetchTask method doInBackground.

@Override
protected String doInBackground(String... params) {
    try {
        MWApi api = CommonsApplication.getInstance().getMWApi();
        ApiResult result = api.action("query").param("format", "xml").param("prop", "imageinfo").param("iiprop", "url").param("iiurlwidth", THUMB_SIZE).param("titles", params[0]).get();
        return result.getString("/api/query/pages/page/imageinfo/ii/@thumburl");
    } catch (Exception e) {
    // Do something better!
    }
    return null;
}
Also used : ApiResult(org.mediawiki.api.ApiResult)

Aggregations

ApiResult (org.mediawiki.api.ApiResult)11 IOException (java.io.IOException)7 MWApi (fr.free.nrw.commons.MWApi)6 ArrayList (java.util.ArrayList)4 RemoteException (android.os.RemoteException)2 Date (java.util.Date)2 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 PendingIntent (android.app.PendingIntent)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Bundle (android.os.Bundle)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Toast (android.widget.Toast)1 Contribution (fr.free.nrw.commons.contributions.Contribution)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1